-->

Firebase Remote Notifications Not Receiving?

2019-07-17 23:43发布

问题:

I'm trying to send remote notifications from my Firebase console through the cloud messaging but my phone isn't receiving any of the alerts. I have already uploaded my certificates to Firebase and I'm using the default code given by the Firebase tutorial to receive notifications.

Here is a picture of my certificates showing that I have already created it

Here is my code in which I implemented it as well. class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

override init() {
    FIRApp.configure()
    FIRDatabase.database().persistenceEnabled = true
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let notificationTypes : UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
    let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    application.registerForRemoteNotifications()
    application.registerUserNotificationSettings(notificationSettings)

    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(application: UIApplication,
    openURL url: NSURL,
    sourceApplication: String?,
    annotation: AnyObject) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(
            application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)
}

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    FBSDKAppEvents.activateApp()
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification

    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print("%@", userInfo)
}

EDIT Upon more debugging, my console is now printing this error:

Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.

回答1:

It seems like, based on your last edit, you are receiving the notification. Otherwise, it wouldn't be giving you that warning that the completion handler was never called.

You can go ahead and remove the warning (and make iOS happy) by calling the completion handler. For example,

completionHandler(.NoData)

which is telling iOS that there was no new data associated with the notification that your app needed to download.