为啥我收到远程通知时没有发送本地通知?
Posted
技术标签:
【中文标题】为啥我收到远程通知时没有发送本地通知?【英文标题】:Why doesn't my local notification gets sent when I receive a remote notification?为什么我收到远程通知时没有发送本地通知? 【发布时间】:2016-10-24 15:45:49 【问题描述】:我正在尝试在收到远程 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)
任何帮助将不胜感激。
【问题讨论】:
通知的有效负载是什么样的?您有通知负载、数据负载还是两者都有? 感谢您的回复,我都有,我在应用委托类中实现了接收本地通知的功能,似乎每次都会调用它。所以我的问题是如何在应用程序被杀死或在后台并且我收到远程通知时安排本地通知,因为当应用程序被杀死时,应用程序委托函数将不会被调用! 【参考方案1】:为了确保您的应用始终处理通知的显示方式(即使它未运行或在后台),您需要设置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
【讨论】:
我想使用本地通知的原因是 FCM 还不支持其通知上的操作按钮,他们建议使用本地通知。 每次didReceiveRemoteNotification
被触发时,我都会构建自己的本地通知,但是当应用程序未运行时,在后台/前台每次触发它的唯一方法是通过仅数据有效载荷。添加notification
告诉 FCM 处理通知。您是否尝试过仅数据负载?
嗯......好点,我会尝试,虽然,我现在放弃了,因为它不是一个真正的关键功能,但我会再试一次,谢谢你的评论
我花了一段时间才让它正常工作。从他们的文档中并没有 100% 清楚,但可以为 3 个主要应用状态中的每一个制作自己的本地通知
好的,有时间我会试试的,稍后再讨论……干杯!以上是关于为啥我收到远程通知时没有发送本地通知?的主要内容,如果未能解决你的问题,请参考以下文章