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

相關問題