【问题标题】:Firebase messaging. iOS app not receiving notifications when in background mode or shut downFirebase 消息传递。 iOS 应用程序在后台模式下或关闭时未收到通知
【发布时间】:2017-01-13 16:22:20
【问题描述】:

我无法让我的应用在后台或关机模式下接收通知。我已按照 Firebase 指南了解如何在我的应用中实现 firebase 消息传递。以前我使用过 GCM(谷歌云消息传递)并且一切正常,但是自从升级到 Firebase 后我无法让它工作。一旦我启动我的应用程序,我在后台或关闭时发送的所有通知(通过 firebase 控制台通知)都会传递。

我有:

  • 在 Firebase 控制台上创建项目
  • 使用正确的包 ID 将我的应用添加到 firebase 项目
  • 创建了 APNS 开发证书
  • 已将证书上传到我在 Firebase 控制台上的应用

值得一提的是,我通过在 Info.plist 文件中将 FirebaseAppDelegateProxyEnabled 设置为 NO 来禁用 swizzling。

相关代码:

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)
}

我什至尝试过 swizzling。同样的事情正在发生。我将非常感谢任何帮助或提示正确方向。

【问题讨论】:

  • 嗨,你找到答案了吗??
  • 你有任何相当于content-available = 1的firebase吗?

标签: ios swift firebase apple-push-notifications firebase-cloud-messaging


【解决方案1】:

您必须在 JSON 负载中添加 "content-available" : true。否则你不会在后台模式下收到推送通知。

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

【讨论】:

  • content_available 应该是content-available
【解决方案2】:

我在下面配置了一些东西,它对我的​​应用程序工作正常,

在info.plist中设置下面两个键

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

在appdelegate下面设置测试通知开发模式的代码

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

从 firebase 控制台下载 GoogleService-Info.plist 并将其放到您的应用程序中

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 2020-06-16
    • 2019-02-06
    • 2020-11-07
    • 2020-10-27
    • 2022-06-20
    • 2017-02-06
    • 1970-01-01
    • 2019-10-08
    相关资源
    最近更新 更多