本文介绍了安装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.
- How will I get the
FCM ID
so that I can send it in my api call. - When a push notification comes, where do I handle it..?
- Where do I mention things like when to receive the notifications and the actions to take while tapping on notifications..?
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)")
}
推荐答案
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后生成推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!