安装Firebase后生成推送通知 [英] Generating push notifications after installing firebase

查看:81
本文介绍了安装Firebase后生成推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将Firebase集成到了我的项目中,我认为我已经遵循了在应用程序中生成推送通知所需的所有步骤.我还通过从Firebase控制台发送一些虚拟通知来测试了推送通知.

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.

现在我的问题是..

  1. 我将如何获取FCM ID,以便可以在api调用中发送它.
  2. 当推送通知到来时,我该在哪里处理呢??
  3. 我在哪里提到何时接收通知以及在点击通知时要执行的操作.?
  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..?

编辑1

这就是我用于处理推送通知的所有代码...

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

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

}

推荐答案

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

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
    }

这篇关于安装Firebase后生成推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆