[英]Swift 5 - Not able to receive Push Notification through FCM
I am having trouble with getting push notification, here's my payload我在获取推送通知时遇到问题,这是我的有效负载
{
"to": "erOZW7CN....(fcm token)",
"data": {
"body": "Incoming-Call",
"title": "AppName",
"type": "incoming-call",
"number": "0212222222"
},
"aps": {
"alert": {
"title": "AppName",
"body": "Incoming-Call"
}
}
}
The data
part is for Android. data
部分适用于Android。 When I use notification
instead of aps
it works but the Android side gets buggy.当我使用notification
而不是aps
它可以工作,但 Android 端会出现问题。 So I need to use aps
to detect request.所以我需要使用aps
来检测请求。 I've done lots of research but couldn't find any solution.我做了很多研究,但找不到任何解决方案。
Here's my AppDelegate这是我的 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
}
}
Note: I am sending request from Postman注意:我正在发送来自邮递员的请求
Would be great to find a solution here.在这里找到解决方案会很棒。 Thanks.谢谢。
Generate your payload as following when sending Push Notification via FCM or APNS.通过 FCM 或 APNS 发送推送通知时,生成如下有效负载。
Basic Payload: iOS and Android (Sending via FCM)基本负载:iOS 和 Android(通过 FCM 发送)
{
"message": {
"notification": {
"body": "YOUR MESSAGE BODY HERE",
"title": "YOUR MESSAGE TITLE HERE"
}
}
}
Basic Payload: iOS (Sending via APNS)基本负载:iOS(通过 APNS 发送)
{
"aps": {
"alert": {
"body": "YOUR MESSAGE BODY HERE",
"title": "YOUR MESSAGE TITLE HERE"
}
}
}
NOTE:笔记:
In the didReceiveRemoteNotification
function you will receive your payload as per iOS style.在didReceiveRemoteNotification
函数中,您将收到符合 iOS 风格的有效负载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.