推送通知不会调用didReceiveRemoteNotification swift

时间:2016-10-31 11:39:21

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

我已经使用FCM实现了推送通知。 当我的应用程序在前台并且通知到达时,会调用didReceiveRemoteNotification方法,但是当应用程序未处于活动状态时,它不会调用此方法

我正在通过在app处于后台时将bool值设置为true来检查这个,但是不会更改bool,这意味着它不会执行此行。 这是我的方法。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        self.application(application, didReceiveRemoteNotification: userInfo) { (UIBackgroundFetchResult) in
            if UIApplication.sharedApplication().applicationState != .Active{


            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "AriveNotification")
            NSUserDefaults.standardUserDefaults().synchronize()
            }else{

                NSUserDefaults.standardUserDefaults().setBool(false, forKey: "AriveNotification")
                NSUserDefaults.standardUserDefaults().synchronize()
            }
        }
    }


    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        if UIApplication.sharedApplication().applicationState != .Active{


            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "AriveNotification")
            NSUserDefaults.standardUserDefaults().synchronize()
            }else{

                NSUserDefaults.standardUserDefaults().setBool(false, forKey: "AriveNotification")
                NSUserDefaults.standardUserDefaults().synchronize()
            }
        }
        completionHandler(.NewData)
    }

注册GCM推送通知我在用户成功登录时使用这些方法。

func userDidLoggedIn() -> Void {
    tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! VaboTabBarController


    self.window?.rootViewController = self.tabBarController
    self.window?.makeKeyAndVisible()

    self.registerDeviceForPushNotification(UIApplication.sharedApplication())

}
func connectToFcm() {

    FIRMessaging.messaging().connectWithCompletion({error in


        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }


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


func registerDeviceForPushNotification(application:UIApplication) -> Void {

    let settings: UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: [.Alert,.Badge,.Sound], categories: nil)
    self.pushNotificationToken = FIRInstanceID.instanceID().token()!
    let userID = self.userData["id"] as! NSNumber

    print("InstanceID token: \(self.pushNotificationToken)")
    self.registerDeviceOnServerWith(self.pushNotificationToken, userID: userID)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()
}


func tokenRefreshNotification(notification: NSNotification) {
    if let refreshedToken = FIRInstanceID.instanceID().token() {

        let userID = self.userData["id"] as! NSNumber

        self.registerDeviceOnServerWith(refreshedToken, userID: userID)
        print("InstanceID token: \(refreshedToken)")
    }

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

PS。我已经从功能以及info.plist中设置了Remote-Notification检查。另外,我还试过从通知有效载荷中添加"content-available":1

1 个答案:

答案 0 :(得分:0)

您必须在通知有效负载中编写正确的拼写和字符位置。 根据您的陈述,您可能在内容可用位置放置了错误的字符。 请以这种格式发送您的有效载荷..

{
    "aps":{
        "alert":{
            "title":"Notification Title Text",
            "Body" :"Notification Body",
            "content_available":1

        }
    }
}

您已将content-available写为content_availble。现在,这将唤醒您的应用程序以在后台运行您的逻辑