1

我试图在收到远程FCM通知时发送位置通知,因为据我所知FCM通知不支持操作按钮。但是本地通知有时只会发送,在我看来它是随机的。尽管我总是得到远程通知。我想在所有三种状态中获得本地通知,前景,背景或当应用程序被杀害。为什么我收到远程通知时不会发送本地通知?

这里就是我在didFinishLoadingWithOptions添加按钮:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 


    let incrementAction = UIMutableUserNotificationAction() 
    incrementAction.identifier = "First" 
    incrementAction.title = "First" 
    incrementAction.activationMode = UIUserNotificationActivationMode.foreground 
    incrementAction.isAuthenticationRequired = true 
    incrementAction.isDestructive = true 


    let decrementAction = UIMutableUserNotificationAction() 
    decrementAction.identifier = "second" 
    decrementAction.title = "second" 
    decrementAction.activationMode = UIUserNotificationActivationMode.foreground 
    decrementAction.isAuthenticationRequired = true 
    decrementAction.isDestructive = false 

    // Category 
    let counterCategory = UIMutableUserNotificationCategory() 
    counterCategory.identifier = "BOOKING_CATEGORY" 

    // A. Set actions for the default context 
    counterCategory.setActions([incrementAction, decrementAction], 
           for: UIUserNotificationActionContext.default) 

    // B. Set actions for the minimal context //No more than 2 
    counterCategory.setActions([incrementAction, decrementAction], 
           for: UIUserNotificationActionContext.minimal) 

    // iOS 9 support 
else if #available(iOS 9, *) { 

    UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: NSSet(object: counterCategory) as? Set<UIUserNotificationCategory>)) 
    UIApplication.shared.registerForRemoteNotifications() 
} 

} 

此处,我创建了本地通知:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 

    if UIApplication.shared.applicationState == UIApplicationState.active { 
     // Do something you want when the app is active 
    } else { 
     // Do something else when your app is in the background 
    } 

    // fire a local notification upon receiving FCM notification 
    let localNotification = UILocalNotification() 
    localNotification.alertTitle = "Notification Title" 
    localNotification.alertBody = "more details" 
    localNotification.alertAction = "ShowDetails" 
    localNotification.fireDate = NSDate().addingTimeInterval(5) as Date 
    localNotification.soundName = UILocalNotificationDefaultSoundName 
    localNotification.applicationIconBadgeNumber = 1 
    localNotification.category = "BOOKING_CATEGORY" 
    UIApplication.shared.scheduleLocalNotification(localNotification) 

    print("Push notification received bkg: \(userInfo)") 
    print("completed" , completionHandler) 
} 

任何帮助,将不胜感激。

+0

什么是您的有效载荷通知样子?你有通知有效载荷,有效载荷还是两者? –

+0

感谢您的回复,我有两个,我实现了在应用程序委托类中接收本地通知的函数,它似乎每次都会被调用。所以我的问题是如何安排一个本地通知,当应用程序被杀死或在后台,我收到一个远程通知,因为当应用程序被杀死时,应用程序委托函数将不会被调用! – TheeBen

回答

0

为了确保您的应用始终是如何处理的通知显示(即使它没有运行,或者是在背景中),你需要设置一个data有效载荷例如

{ 
    "to":"push_token", 
    "content_available": true, 
    "priority": "high", 
    "data": { 
     "title": "New Message", 
     "message": "Bob sent you a message", 
    } 
} 

设置一个notification有效载荷触发FCM来自动显示给最终用户代表客户端应用程序的设备的消息。如果您使用数据有效负载,您可以完全控制如何显示通知。

欲了解更多信息请查看https://firebase.google.com/docs/cloud-messaging/concept-options

+0

我想使用本地通知的原因是FCM不支持通知中的操作按钮,他们建议使用本地通知。 – TheeBen

+0

每次'didReceiveRemoteNotification'被触发时,我也会建立自己的本地通知,但是每当应用程序没有运行时,在后台/前台运行它的唯一方法是仅传递数据有效载荷。添加通知会告诉FCM处理通知。您是否尝试过仅有数据的有效载荷? –

+0

嗯...好点,我会尝试,虽然,我放弃了这一点,因为它不是一个关键的功能,但我会给这个镜头,感谢评论 – TheeBen

相关问题