Generating push notifications after installing fir

2019-08-03 21:32发布

问题:

I have integrated Firebase into my project and I think I have followed all the steps required for generating push notifications within the app. I have also tested the push notifications by sending some dummy notifications from the Firebase console.

This is the tutorial I entirely followed.

Now my questions are..

  1. How will I get the FCM ID so that I can send it in my api call.
  2. When a push notification comes, where do I handle it..?
  3. Where do I mention things like when to receive the notifications and the actions to take while tapping on notifications..?

EDIT 1

So this is all the code I have for handling push notifications...

In didFinishLaunchingWithOptions...

    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        // For iOS 10 data message (sent via FCM
        Messaging.messaging().delegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FirebaseApp.configure()

And then,

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    let token = Messaging.messaging().fcmToken
    print("FCM token: \(token ?? "")")
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]){
    print("userInfo:->  \(userInfo)")

}

回答1:

Q1 :- First Method Q2&3 :- Second Method

     func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
                let token = Messaging.messaging().fcmToken
                print("FCM token: \(token ?? "")")
         }

         func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]){
         print("userInfo:->  \(userInfo)")
        }

     func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

            print("userInfo:->  \(userInfo)")
            let redirect_flag = userInfo["redirect_flag"]as! String

            if application.applicationState == .inactive {
              // handle when you background 
         }
    }else{
          // Here You need to handle all terms which you handle in 
             didReceiveRemoteNotification  method
    }