Swift 5 - 无法通过 FCM 接收推送通知
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
}
}
注意:我正在从 Postman 发送请求
如果能在这里找到解决方案,那就太好了。谢谢。
通过 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 样式的负载。
我在获取推送通知时遇到问题,这是我的负载
{
"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
}
}
注意:我正在从 Postman 发送请求
如果能在这里找到解决方案,那就太好了。谢谢。
通过 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 样式的负载。