我的问题可能很糟糕,但是我在任何地方都找不到任何答案,我迷路了...
因此,我想在iOS 10 中显示带有不错图像的丰富通知。
为此,我正在使用FCM和UnotificationersViceExtension,如果我正确理解的话,请获取数据有效负载,在修改Unotification Content之前找到图像URL并加载它。
。我遇到的问题是我无法掌握此"数据"。
我发送给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://usr.audioasylum.com/images/2/20352/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://usr.audioasylum.com/images/2/20352/Cat_and_rose.jpg";
message = "Offer!";
}
据我了解,MediaUrl和消息应以" APS"而不是外部结束,这就是为什么我在扩展中找不到它们。
和在扩展中我得到的:(我在昏迷上将其拆分以获得更多可读性)
<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:。。您的应用程序 can 在没有此回调的情况下仍在后台接收数据消息,但是对于前景,您需要在应用程序委托中的以下逻辑:
#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
P.S。:我不确定您是否已经尝试过,但是您的帖子中没有任何类似的代码,因此我认为应该尝试使用此代码。
据我了解,,媒体和消息应以" APS"而不是外部结束,这就是为什么我在扩展中找不到它们。
问题是,aps
中只有特定参数可以预期:
aps
字典中的任何其他键都被Apple忽略。
同时,只有一些相应的参数可以在FCM中使用,这些参数将出现在aps
有效载荷中。因此,您应该简单地期望您包含的任何自定义键值对都将在aps
参数之外。从上面的苹果文档(强调我的):
除了
aps
字典外, JSON字典可以在您的应用程序特定内容中包括自定义键和值。
关于您在评论中链接的博客时,我没有深入研究它,因为它不是真正使用FCM,最好假定行为不同。