iOS的Firebase FCM文档说:
对于iOS客户端应用,您最多可以接收通知和数据负载 通过Firebase Cloud Messaging APNs接口扩展到4KB。
我的Firebase Cloud Function同时发送notification
和data
消息。
我的应用程序在后台成功接收到notification
消息-但是data
消息仅在应用程序位于前台时通过。
这是当应用程序处于前台时收到的内容:
Foreground notification: <UNNotification: 0x283577fc0; source: app.my.App date: 2020-06-29 09:06:45 +0000, request: <UNNotificationRequest: 0x2835745d0; identifier: 0BF0ED00-FB91-4AC1-A960-BF880CEB8F52, content: <UNNotificationContent: 0x2801412b0; title: <redacted>, subtitle: (null), body: <redacted>, summaryArgument: (null), summaryArgumentCount: 0, categoryIdentifier: , launchImageName: , threadIdentifier: , attachments: (
), badge: (null), sound: (null), realert: 0, trigger: <UNPushNotificationTrigger: 0x283860e10; contentAvailable: YES, mutableContent: NO>>, intents: (
)>
2
[AnyHashable("gcm.message_id"): 1563221604783472, AnyHashable("bio"): test bio, AnyHashable("age"): 23, AnyHashable("img3"): MKcBDWqOtQ6r9Lyrx.jpg, AnyHashable("img1"): nQA42VIBnDvineJ3yF.jpg, AnyHashable("pronoun"): her, AnyHashable("name"): Anna, AnyHashable("aps"): {
"content-available" = 1;
}, AnyHashable("error"): undefined, AnyHashable("img2"): skfuoVTauTZAD3DN33.jpg, AnyHashable("uid"): ehqATVzFKmi944JdtJZkvx3261l2, AnyHashable("google.c.sender.id"): 697466084746]
2
[AnyHashable("gcm.message_id"): 1593421604788701, AnyHashable("aps"): {
alert = {
body = "Test body";
title = "Test title";
};
"content-available" = 1;
}, AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.sender.id"): 697466084746]
AppDelegate.swift
// 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])
print("Foreground notification: \(notification)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print full message.
print("2")
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// Print full message.
print("1")
print(userInfo)
}
当应用程序在后台运行时,我的AppDelegate不会接收/打印任何内容-当我单击它时,它甚至都不会打印推送通知。
这是我的Firebase Cloud功能:
const options = { priority: "high", timeToLive: 30000, content_available: true }
const fcmToken = doc.fcmToken;
// Send notification to user1
const notif = {
notification: {
title: name + "test",
body: "test"
}
}
admin.messaging().sendToDevice(fcmToken, notif, options);
// Send data msg to user1
const dataMsg = {
data: {
uid: uid,
name: name,
age: age.toString(),
bio: bio,
img1: String(img1),
img2: String(img2),
img3: String(img3),
pronoun: pronoun
}
}
return admin.messaging().sendToDevice(fcmToken, dataMsg, options);
如何在后台接收数据消息?如果无法做到这一点,那么是否有办法发送带有通知的额外键值对?
编辑:我还启用了Xcode功能中的Remote notifications
和Background fetch
。