我试图在收到远程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)
}
任何帮助,将不胜感激。
什么是您的有效载荷通知样子?你有通知有效载荷,有效载荷还是两者? –
感谢您的回复,我有两个,我实现了在应用程序委托类中接收本地通知的函数,它似乎每次都会被调用。所以我的问题是如何安排一个本地通知,当应用程序被杀死或在后台,我收到一个远程通知,因为当应用程序被杀死时,应用程序委托函数将不会被调用! – TheeBen