[英]Swift 5 - Not able to receive Push Notification through FCM
我在獲取推送通知時遇到問題,這是我的有效負載
{
"to": "erOZW7CN....(fcm token)",
"data": {
"body": "Incoming-Call",
"title": "AppName",
"type": "incoming-call",
"number": "0212222222"
},
"aps": {
"alert": {
"title": "AppName",
"body": "Incoming-Call"
}
}
}
data
部分適用於Android。 當我使用notification
而不是aps
它可以工作,但 Android 端會出現問題。 所以我需要使用aps
來檢測請求。 我做了很多研究,但找不到任何解決方案。
這是我的 AppDelegate
import UIKit
import CoreData
import Firebase
import Network
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
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
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Database")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
UserDefaults.standard.set(fcmToken, forKey: "fcmToken")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("Push Received: \(userInfo)")
//TODO: CALLKIT DETECT
}
}
注意:我正在發送來自郵遞員的請求
在這里找到解決方案會很棒。 謝謝。
通過 FCM 或 APNS 發送推送通知時,生成如下有效負載。
基本負載:iOS 和 Android(通過 FCM 發送)
{
"message": {
"notification": {
"body": "YOUR MESSAGE BODY HERE",
"title": "YOUR MESSAGE TITLE HERE"
}
}
}
基本負載:iOS(通過 APNS 發送)
{
"aps": {
"alert": {
"body": "YOUR MESSAGE BODY HERE",
"title": "YOUR MESSAGE TITLE HERE"
}
}
}
筆記:
在didReceiveRemoteNotification
函數中,您將收到符合 iOS 風格的有效負載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.