安装后火力点生成的推送通知(Generating push notifications after

2019-09-26 04:58发布

我已经将Firebase到我的项目,我想我已经遵循所有的应用程序中产生推送通知所需的步骤。 我也从火力地堡控制台发送一些虚拟测试通知的推送通知。

这是教程我完全遵循。

现在,我的问题是..

  1. 我将如何获得FCM ID ,这样我可以在我的API调用发送。
  2. 当一个推送通知来了,我在哪里处理呢..?
  3. 我在哪里提之类的东西时,接收通知和采取的行动,而在通知攻..?

编辑1

因此,这是所有的代码,我有处理推送通知...

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()

接着,

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)")

}

Answer 1:

Q1: - 第一种方法Q2&3: - 第二种方法

     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
    }


文章来源: Generating push notifications after installing firebase