Notification not coming via Firebase console

2019-06-14 08:18发布

问题:

I have tried every possible solutions for above issue but still not getting notification to my device via Firebase Console. Please suggest.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {

        self.handleNotification(remoteNotification as! [NSObject : AnyObject])
    }
    let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    // Add observer for InstanceID token refresh callback.
    NSNotificationCenter
        .defaultCenter()
        .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
                     name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true }
 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {


    FIRMessaging.messaging().appDidReceiveMessage(userInfo)



    NSNotificationCenter.defaultCenter().postNotificationName("reloadTheTable", object: nil)


}

 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}
func tokenRefreshNotificaiton(notification: NSNotification) {
    guard let refreshedToken = FIRInstanceID.instanceID().token()
        else {
            return
    }


    print("InstanceID token: \(refreshedToken)")

    utill.tokenDefault.setValue(refreshedToken, forKey: "tokenId")

    connectToFcm()
}

Few firebase warnings are also displaying in the debugger:

回答1:

Please make sure you have enabled push notification capabilities from project target capabilities section shown in the picture if you are deploying the application from Xcode 8 or later.



回答2:

I think the problem is because of the ios version. ios 10 requires UNUserNotificationCenter. Try the code below and add the function to your application didFinishLaunching.

    func registerForRemoteNotification() {
    if #available(iOS 10.0, *) {
        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
            if error == nil{
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
    else {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
}


回答3:

Issue Resolved for me. I skipped uploading APNs development certificate.