簡體   English   中英

Firebase消息傳遞。 iOS應用在后台模式或關機時未收到通知

[英]Firebase messaging. iOS app not receiving notifications when in background mode or shut down

我在讓我的應用程序在后台或關機模式下接收通知時遇到問題。 我已按照Firebase指南了解如何在我的應用中實施firebase消息傳遞。 以前我使用過GCM(谷歌雲消息傳遞),但一切都運行良好,但自從升級到Firebase后,我無法使用它。 一旦我啟動我的應用程序,就會在后台或關機時發送我發送的所有通知(通過firebase控制台通知)。

我有:

  • 在Firebase控制台上創建項目
  • 使用正確的包ID將我的應用添加到firebase項目
  • 創建了APNS開發證書
  • 在Firebase控制台上將證書上傳到我的應用

值得一提的是我通過在我的Info.plist文件中將FirebaseAppDelegateProxyEnabled設置為NO來禁用調配。

相關代碼:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
  let types: UIUserNotificationType = [UIUserNotificationType.Badge, UIUserNotificationType.Alert, UIUserNotificationType.Sound]
    let settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
    application.registerUserNotificationSettings( settings )
    application.registerForRemoteNotifications()

    FIRApp.configure()
}

func connectToFcm() {
    FIRMessaging.messaging().connectWithCompletion { (error) in
        if (error != nil) {
            print("Unable to connect with FCM")
        } else {
            print("Connected to FCM.")
            self.refreshToken()
        }
    }
}

func refreshToken(){
    if let refreshedToken = FIRInstanceID.instanceID().token() {

        gcmToken = refreshedToken

        userDefaults.setValue(gcmToken, forKey: CONSTANTS.GCM_TOKEN)

        if(userDefaults.boolForKey("UserLoggedIn")){
            pushGcmToken() //push the token to server
        }
    }
}

func onTokenRefresh() {
    refreshToken()
    // Connect to FCM since connection may have failed when attempted before having a token.
    connectToFcm()
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    NSLog("didReceiveRemoteNotification \(userInfo)")

    FIRMessaging.messaging().appDidReceiveMessage(userInfo)

    handleRemoteNotification(userInfo)

}

func handleRemoteNotification(userInfo: [NSObject : AnyObject]){
    if let notification = userInfo["notification"] as? [NSObject : AnyObject]{

        let bodyNot = notification["body"] as! String
        var titleNot = "Ändring"
        var category = "UNIFIED_OTHER_CATEGORY"
        if(notification["title"] != nil){
            titleNot = (notification["title"] as! String == "Call" ? "Inkomande samtal" : notification["title"]) as! String
            category = "UNIFIED_CALL_CATEGORY"
        }

        let notis = UILocalNotification()
        notis.alertTitle = titleNot

        notis.alertBody = bodyNot
        notis.soundName = UILocalNotificationDefaultSoundName // play default sound
        notis.userInfo = ["UUID": "122" ] // assign a unique identifier to the notification so that we can retrieve it later
        notis.category = category
        notis.fireDate = NSDate()
        UIApplication.sharedApplication().scheduleLocalNotification(notis)
    }
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    NSLog("didRegisterForRemoteNotificationsWithDeviceToken \(deviceToken)")
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}

我甚至嘗試過調情。 發生同樣的事情。 我非常感謝任何幫助或暗示正確的方向。

我配置了下面的東西,它的工作正常,我的應用程序,

在info.plist中設置下面兩個鍵

<key>FIRMessagingAutoRegisterEnabledflag</key>
    <true/>
<key>FirebaseAppDelegateProxyEnabled</key>
    <false/>

在appdelegate中設置下面的測試通知開發模式的代碼

[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];

從firebase控制台下載GoogleService-Info.plist並將其放在您的應用程序上

您必須在JSON有效內容中添加“content-available”:true 否則,您將無法在后台模式下獲得推送通知。

"notification" : {
            "content-available" : true,
            "body" : "this is body",
            "title" : "this is title"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM