Ios 使用FCM的推送通知不起作用
我已经实施了FCM。显示时没有错误。已成功生成令牌。但没有通知进入设备 我看了谷歌的文档 这是我的AppDelegate类Ios 使用FCM的推送通知不起作用,ios,swift,swiftui,firebase-cloud-messaging,apple-push-notifications,Ios,Swift,Swiftui,Firebase Cloud Messaging,Apple Push Notifications,我已经实施了FCM。显示时没有错误。已成功生成令牌。但没有通知进入设备 我看了谷歌的文档 这是我的AppDelegate类 import Foundation import UIKit import Firebase //https://github.com/firebase/quickstart-ios/blob/5694c29ac5a8dcc672ec13f55f0ab74a6b9af11e/messaging/MessagingExampleSwift/AppDelegate.swift
import Foundation
import UIKit
import Firebase
//https://github.com/firebase/quickstart-ios/blob/5694c29ac5a8dcc672ec13f55f0ab74a6b9af11e/messaging/MessagingExampleSwift/AppDelegate.swift#L40-L55
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
// 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 })
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print(userInfo)
}
// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(String(describing: fcmToken))")
let dataDict:[String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}
}
DidReceiverRegistrationToken委托方法调用两次
这是我的能力
我尝试从firebase云消息选项发送通知,但没有到达任何推入式设备。
我也试过邮递员。这是我的有效载荷和输出
我已将APNS证书上传到firebabse。
我试着进入主项目,但它不起作用。
然后我尝试将新的实现放在一个虚拟项目中,它也不起作用
这是我的源代码。你可以检查一下。
我认为APNS的有效载荷不同。从文档中,我发现以下是APNS中FCM有效载荷的格式
{
"message": {
"token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "This is an FCM notification that displays an image.!",
"title" : "FCM Notification",
},
"apns": {
"payload": {
"aps": {
"mutable-content": 1
}
},
"fcm_options": {
"image": "url-to-image"
}
}
}
}
您可以添加所有UNUserNotificationCenterDelegate方法以及completionHandler([.alert、.badge、.sound])。检查有效负载,但在文档中,APN的fcm有效负载有所不同。在iOS上使用fcm时遇到了困难,在Android上总是工作得更好。曾与Firebase团队就iOS不稳定的FCM进行了长时间的讨论。请在将来使用APNs,它总是稳定的。这不是从邮递员的工作。然而,我的负载在我的另一个应用程序中工作。