我已经将Firebase
集成到我的项目中,我认为我已经完成了在应用程序中生成推送通知所需的所有步骤。我还通过从Firebase控制台发送一些伪通知来测试推送通知。
这是我完全遵循的教程。
现在我的问题是。。
- 我将如何获得
FCM ID
,以便在我的api调用中发送它 - 当推送通知到来时,我该在哪里处理它
- 我在哪里提到什么时候收到通知以及在点击通知时要采取的行动
编辑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)")
}
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
}