未收到FCM推送通知

时间:2017-09-18 11:17:07

标签: ios firebase swift3 push-notification firebase-cloud-messaging

您已经包含以下代码以接收来自FCM服务器的推送通知InstanceId可用且FCM控制台显示已完成推送通知但未在应用程序委托中调用任何功能。请帮助我缺少的东西

func registerForPushNotifications(_ application: UIApplication) {

    if #available(iOS 10.0, *) {
        Messaging.messaging().delegate = self
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(grant, error)  in
            if error == nil {
                if grant {
                    application.registerForRemoteNotifications()
                } else {
                    //User didn't grant permission
                }
            } else {
                print("error: ",error?.localizedDescription)
            }
        })
    } else {
        let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
    }
    application.registerForRemoteNotifications()
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(self.tokenRefreshNotification),
                                           name: .InstanceIDTokenRefresh,
                                           object: nil)

}

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != UIUserNotificationType() {
        application.registerForRemoteNotifications()
        self.isNotificationEnabled { (isEnabled) in
            if isEnabled{
                print("Push notification enabled")
            }else{
                print("Push notification not enabled")
            }
        }
    }
}

func isNotificationEnabled(completion:@escaping (_ enabled:Bool)->()){
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { (settings: UNNotificationSettings) in
            let status =  (settings.authorizationStatus == .authorized)
            completion(status)
        })
    } else {
        if let status = UIApplication.shared.currentUserNotificationSettings?.types{
            let status = status.rawValue != UIUserNotificationType(rawValue: 0).rawValue
            completion(status)
        }else{
            completion(false)
        }
    }
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    var token = ""
    for i in 0..<deviceToken.count {
        token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }
    Messaging.messaging()
        .setAPNSToken(deviceToken, type: MessagingAPNSTokenType.unknown)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Registration failed!",error.localizedDescription)

}

func tokenRefreshNotification(_ notification: Notification) {
    if let refreshedToken = InstanceID.instanceID().token() {
        print("InstanceID token: \(refreshedToken)")
    }
    connectToFcm()
}

func connectToFcm() {
    Messaging.messaging().shouldEstablishDirectChannel = true
}


Extensions for app delegate 

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler(.alert)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    let userInfo = response.notification.request.content.userInfo as NSDictionary
     print("userinfo",userInfo)
}
}

extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print("Received data message: \(remoteMessage.appData)")
}
 }

通过以上代码我生成了instanceID,我还上传了带有app前缀和密钥的.p8文件。当我发送推送通知时,其状态已完成,但未在应用代理中调用任何功能

0 个答案:

没有答案