在iOS中丰富的推送通知使用数据与FCM

问题描述:

我的问题可能是坏的,但我找不到任何答案的任何地方,我迷路了......在iOS中丰富的推送通知使用数据与FCM


所以我想显示了丰富的通知在iOS 10+中拥有不错的图像。

为此,我使用FCM和UNNotificationServiceExtension,如果我理解正确,应该获取数据有效负载,找到图像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://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); 
} 
+0

嗨。如果您可以发布您的代码片段来接收邮件,我认为这将有助于社区。 :) –

+1

完成,但它确实只是代码示例中的一个打印:) – SeikoTheWiz

+0

嗨@SeikoTheWiz对不起,我在这里没有太多的帮助。我也在我的答案[这里](http://*.com/a/42263795/4625829)中看到了你的评论(与这篇文章有关吗?)。我会尝试环顾四周,看看我在这里找到了什么(但不能承诺任何)。有一件事引起了我的注意,尽管目前为止,'badge'参数应该在'notification'中。无论如何,你希望解释“数据”有效载荷的行为是怎样的,对吧?为什么数据出现在“aps”之外? –

我认为你应该检查self.bestAttemptContent?.userInfomediaUrl数据。

+0

这就是它!我很确定我测试了它,但似乎我没有-_- 非常感谢! – SeikoTheWiz

我看到这里有两个主要问题:

我的问题是,我无法掌握这个“数据”。

你提到你期待的是,mediaUrl参数应该是里面aps。 (mediaUrlaps之外)。要获取详细信息,你就必须实现什么样的documentation指出:

iOS上10接收数据消息

要接收数据的消息时,您的应用程序是在前台,在iOS 10设备,你需要处理applicationReceivedRemoteMessage :.您的应用程序可以仍然收到数据消息时,它是没有这个回调的背景,但对前景的情况下,你需要像你的应用程序代理以下逻辑:

#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 

附::我不完全确定你是否已经尝试了这一点,但在你的文章中没有任何类似的代码,所以我认为这应该尝试。

据我了解,在mediaURL和消息应该在“APS”结束了,而不是外面,这就是为什么我不能在扩展中找到它们。

的一点是,只有specific parameters是内部aps可以预料:

aps字典中的任何其他按键被苹果忽略。

与此同时,在FCM中只能使用一些corresponding parameters,它将出现在aps负载中。因此,您应该简单地期望您包含的任何自定义键值对都将超出aps参数。从Apple文档以上(重点煤矿):

除了aps字典,的JSON字典可以包括自定义键和值与您的应用程序特定的内容

关于blog关于您在评论中链接的内容,我没有深入研究它,因为它没有真正使用FCM,最好假设行为是不同的。

+0

我花了一段时间才回到这里,并用新鲜的眼睛*设法解决了这个问题。我希望我确实得到了你需要的一点。该文档通常具有您[iOS中的接收邮件](https://firebase.google.com/docs/cloud-messaging/ios/receive)所需的内容。 –

+0

谢谢你的回答! 问题是当我想在应用程序未运行时推送通知时。修改发生的事情的唯一方法是运行UNNotificationServiceExtension,然后修改通知并让系统显示它。在这种情况下不会调用applicationReceivedRemoteMessage。或者,也许我做错了什么,扩展应该唤醒应用程序,然后通过正常的过程? – SeikoTheWiz