简体   繁体   中英

iOS push notification via Firebase not working

Hello I'm making an iPhone app where I send push notifications via Google's Firebase. I'm using Swift and Xcode for the programming. When I open the app I get asked to allow push notifications, however I don't receive any when I send them from the Firebase Console. I was wondering if you could help me out. I'm using Ad Hoc export to transfer an .isa to my friend's iPhone and test it that way. I followed exactly the Firebase tutorial - adding the .plist, the cocoa pod and I uploaded a certificate in the project settings. I have a paid apple developer account.

import UIKit
import CoreData
import Firebase
import FirebaseMessaging


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    FIRApp.configure()

    let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert,UIUserNotificationType.Badge,UIUserNotificationType.Sound]

    let notificationSettings = UIUserNotificationSettings(forTypes:notificationTypes, categories:nil)

    application.registerForRemoteNotifications()
    application.registerUserNotificationSettings(notificationSettings)
    return true
}


func applicationWillResignActive(application: UIApplication) {
}

func applicationDidEnterBackground(application: UIApplication) {
}

func applicationWillEnterForeground(application: UIApplication) {
}

func applicationDidBecomeActive(application: UIApplication) {
}

func applicationWillTerminate(application: UIApplication) {
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    print("MessageID : \(userInfo["gcm_message_id"]!)")
    print("%@", userInfo)

   }
}

There are a few more things you need to do. When you registerForRemoteNotifications , you receive an APNS Token which you need to give to Firebase. This is done in application didRegisterForRemoteNotificationsWithDeviceToken .

import UIKit
import CoreData
import Firebase
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    override init() {

        super.init()

        FIRApp.configure()
    }

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        application.registerForRemoteNotifications()
        application.registerUserNotificationSettings(
            UIUserNotificationSettings(
                forTypes: [.Alert, .Badge, .Sound],
                categories: nil
            )
        )

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: #selector(tokenRefreshNotification),
            name: kFIRInstanceIDTokenRefreshNotification,
            object: nil
        )

        return true
    }

    func applicationDidEnterBackground(application: UIApplication) {

        FIRMessaging.messaging().disconnect()
    }

    func applicationDidBecomeActive(application: UIApplication) {

        FIRMessaging.messaging().connectWithCompletion { (error) in

            switch error {
            case .Some:
                print("Unable to connect with FCM. \(error)")
            case .None:
                print("Connected to FCM.")
            }
        }
    }

    func applicationWillResignActive(application: UIApplication) {}

    func applicationWillEnterForeground(application: UIApplication) {}

    func applicationWillTerminate(application: UIApplication) {}

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

        FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .Sandbox)
    }

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                     fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

        print("MessageID : \(userInfo["gcm.message_id"]!)")
        print("%@", userInfo)
    }

    func tokenRefreshNotification(notification: NSNotification) {

        if let refreshedToken = FIRInstanceID.instanceID().token() {
            print("Instance ID token: \(refreshedToken)")
        }

        applicationDidBecomeActive(UIApplication.sharedApplication())
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM