FCM iOS 推送通知收不到任何通知
FCM iOS Push Notification cannot receive any notification
我目前正在处理通知,但我没有成功收到来自 Firebase FCM 的任何通知。
podfile 配置为:
target 'Project' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for Project
pod 'Firebase/Core'
pod 'Firebase/Messaging'
end
我已经激活了 Background fetches
中的 Push Notifications
和 Remote Notifications
,并且已经阅读了 Whosebug
中的另一个主题,目前与 中的主题相似
我想与您分享我的 AppDelegate 代码,但首先我不得不说 Google 的推送通知文档似乎有点混乱,因为这里有太多重写的方法,每个教程都有不同完成接收通知的方法。
我已经导入了这些
import Firebase
import FirebaseInstanceID
import UserNotifications
import FirebaseMessaging
然后是代表团
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
...
}
然后是willFinishLaunchWithOptions方法
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 data message (sent via FCM
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
这里是 Messaging
委托函数。
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("MEssage -> \(remoteMessage.appData)")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
该函数在 Firebase 文档中用于设置 apns 令牌。
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
我已经从 FCM 发送了数百个通知'并且它在服务器端成功发送但是当我记录收到的消息时没有任何收入数据。
如果你问我为什么在 willFinish
函数中实现配置,文档中有这样的注释。
For devices running iOS 10 and above, you must assign the
UNUserNotificationCenter's delegate property and FIRMessaging's
delegate property. For example, in an iOS app, assign it in the
applicationWillFinishLaunchingWithOptions: or
applicationDidFinishLaunchingWithOptions: method of the app delegate.
如果你能提供任何帮助,我将不胜感激,因为目前很难看出它有什么问题。
请检查您是否在项目功能中激活了推送通知
创建开发 APNs 证书并将其添加到 Firebase 控制台项目设置
在您的 App Delegate 中
import FirebaseMessaging
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,
UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//REMOTE NOTIFICATION
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "")")
//Added Code to display notification when app is in Foreground
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
} else {
// Fallback on earlier versions
}
return true
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
// Print full message.
print(userInfo)
}
// This method will be called when app received push notifications in foreground
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{ completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
}
// MARK:- Messaging Delegates
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instange ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
}
}
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("received remote notification")
}
}
他们说
注意:要以这种方式使用 FCM 直接通道,您必须使用旧版 HTTP API 发送消息。 HTTP v1 API 将 APN 用于发送到 iOS 设备的所有消息。
你需要把
Messaging.messaging().shouldEstablishDirectChannek = true
我正在使用 FirebaseMessaging v4.1.4 并启用了方法调配
application.registerForRemoteNotifications() --> 首先
Messaging.messaging().delegate = self --> 第二
您必须在请求远程通知后设置消息委托。 (我注意到你在请求许可之前调用它)
在我的例子中,因为我在 didRegisterForRemoteNotificationsWithDeviceToken 方法中没有收到 APNS 令牌之前调用它 iOS 13 而生成了 firebase 令牌。
可能因为未生成 APNS 并且 firebase 未与 APNS 令牌映射,您可能没有收到任何通知。
我在 ios 13 台设备上遇到了同样的问题。但它在 ios 13 台设备上运行良好。
问题是没有在 ios 13 设备上调用 didRegisterForRemoteNotificationsWithDeviceToken 方法。
经过一番研究,我发现这是由于网络连接问题。然后我从我的办公室 wifi 切换到蜂窝网络,之后它工作了。 didRegisterForRemoteNotificationsWithDeviceToken 返回了设备令牌。
我回答了类似的问题here。
关键的一步是在功能中启用“推送通知”。
祝大家编码愉快!
我目前正在处理通知,但我没有成功收到来自 Firebase FCM 的任何通知。
podfile 配置为:
target 'Project' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for Project
pod 'Firebase/Core'
pod 'Firebase/Messaging'
end
我已经激活了 Background fetches
中的 Push Notifications
和 Remote Notifications
,并且已经阅读了 Whosebug
中的另一个主题,目前与
我想与您分享我的 AppDelegate 代码,但首先我不得不说 Google 的推送通知文档似乎有点混乱,因为这里有太多重写的方法,每个教程都有不同完成接收通知的方法。
我已经导入了这些
import Firebase
import FirebaseInstanceID
import UserNotifications
import FirebaseMessaging
然后是代表团
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
...
}
然后是willFinishLaunchWithOptions方法
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 data message (sent via FCM
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
这里是 Messaging
委托函数。
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("MEssage -> \(remoteMessage.appData)")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
该函数在 Firebase 文档中用于设置 apns 令牌。
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
我已经从 FCM 发送了数百个通知'并且它在服务器端成功发送但是当我记录收到的消息时没有任何收入数据。
如果你问我为什么在 willFinish
函数中实现配置,文档中有这样的注释。
For devices running iOS 10 and above, you must assign the
UNUserNotificationCenter's delegate property and FIRMessaging's
delegate property. For example, in an iOS app, assign it in the
applicationWillFinishLaunchingWithOptions: or
applicationDidFinishLaunchingWithOptions: method of the app delegate.
如果你能提供任何帮助,我将不胜感激,因为目前很难看出它有什么问题。
请检查您是否在项目功能中激活了推送通知
创建开发 APNs 证书并将其添加到 Firebase 控制台项目设置
在您的 App Delegate 中
import FirebaseMessaging import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. //REMOTE NOTIFICATION if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() Messaging.messaging().delegate = self let token = Messaging.messaging().fcmToken print("FCM token: \(token ?? "")") //Added Code to display notification when app is in Foreground if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self } else { // Fallback on earlier versions } return true } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken as Data } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { // Print full message. print(userInfo) } // This method will be called when app received push notifications in foreground @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge]) } // MARK:- Messaging Delegates func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { InstanceID.instanceID().instanceID { (result, error) in if let error = error { print("Error fetching remote instange ID: \(error)") } else if let result = result { print("Remote instance ID token: \(result.token)") } } } func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { print("received remote notification") } }
他们说 注意:要以这种方式使用 FCM 直接通道,您必须使用旧版 HTTP API 发送消息。 HTTP v1 API 将 APN 用于发送到 iOS 设备的所有消息。 你需要把 Messaging.messaging().shouldEstablishDirectChannek = true
我正在使用 FirebaseMessaging v4.1.4 并启用了方法调配
application.registerForRemoteNotifications() --> 首先
Messaging.messaging().delegate = self --> 第二
您必须在请求远程通知后设置消息委托。 (我注意到你在请求许可之前调用它)
在我的例子中,因为我在 didRegisterForRemoteNotificationsWithDeviceToken 方法中没有收到 APNS 令牌之前调用它 iOS 13 而生成了 firebase 令牌。
可能因为未生成 APNS 并且 firebase 未与 APNS 令牌映射,您可能没有收到任何通知。
我在 ios 13 台设备上遇到了同样的问题。但它在 ios 13 台设备上运行良好。 问题是没有在 ios 13 设备上调用 didRegisterForRemoteNotificationsWithDeviceToken 方法。 经过一番研究,我发现这是由于网络连接问题。然后我从我的办公室 wifi 切换到蜂窝网络,之后它工作了。 didRegisterForRemoteNotificationsWithDeviceToken 返回了设备令牌。
我回答了类似的问题here。
关键的一步是在功能中启用“推送通知”。
祝大家编码愉快!