我試圖在收到遠程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