我想使用FCM将通知和数据消息发送到iOS设备。
都可以正常工作,但是由于某种原因,委托方法messaging didReceiveRemoteMessage
从未被调用。因此,当应用在前景中时,我无法收到数据消息...我在数据旁边尝试了有没有通知块的情况。因此,我发送的消息看起来像这样:
{'message': {
'token': 'mytokenstandshere',
'notification': {
'body': 'message',
'title': 'title'},
'data': {
'datafield': 'datavalue',
'datafield2': 'datavalue2'}
}}
我尝试了所有可能性(没有通知,没有数据,两者都(。通知没有问题,但是数据块没有出现在干净的数据消息中。
我只想在iOS 11 上运行此操作。但是我什至尝试了Google的文档中iOS 9和10的零件。
。这是我的AppDelegate:
@
UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Hardware.start()
Preferences.initDefaults()
FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("new Token: (fcmToken)")
Messaging.messaging().subscribe(toTopic: TOPIC_ALL_MESSAGES)
NotificationSettingsListener.start()
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Got a remote")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(UIBackgroundFetchResult.newData)
}
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print full message.
print(userInfo)
completionHandler()
}
}
我没有看到:
application(_: didReceiveRemoteNotification: fetchCompletionHandler:)
我相信这是处理所必需的。参考
请确保您必须在 AppDelegate.m
[FIRMessaging messaging].delegate = self;