使用 FCM 在 iOS 上的 Rich Push 通知中使用数据

Using Data in Rich Push notification on iOS with FCM

我的问题可能不好,但我找不到任何答案,我迷路了...


所以我想在 iOS 10+.

中显示带有精美图像的丰富通知

为此,我正在使用 FCM 和 UNNotificationServiceExtension,如果我理解正确的话,它应该获取数据负载,找到图像 URL 并在修改 UNNotificationContent 之前加载它。

我遇到的问题是我无法掌握这个 "data"。

我发送给 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"
}

我在 phone 中得到的是:

{
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!";
}

据我了解,媒体URL 和消息应该在 "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);
}

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

The issue I have is that I can't get a hold of this "data".

您提到您希望 mediaUrl 参数应该在 aps.

当您在 data 负载中包含 mediaUrl 时,行为正如预期的那样(mediaUrlaps 之外)。要获取详细信息,您必须执行 documentation:

中所述的内容

Receive data messages on iOS 10

To receive data messages when your app is in the foreground, on iOS 10 devices, you'll need to handle applicationReceivedRemoteMessage:. Your app can still receive data messages when it is in the background without this callback, but for foreground cases you'll need logic like the following in your app delegate:

#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.: 我不完全确定你是否已经尝试过这个,但是你的 post 中没有任何类似的代码所以我认为这应该受审。

As I understand it, the mediaURL and message should end up in "aps" and not outside, which is why I can't find them in the extension.

事实是,aps:

中只有 specific parameters

Any other keys in the aps dictionary are ignored by Apple.

同时,只有一些 corresponding parameters 可以在 FCM 中使用,它们将出现在 aps 负载中。这样,您应该简单地期望您包含的任何自定义键值对都在 aps 参数之外。来自上面的 Apple 文档(强调我的):

In addition to the aps dictionary, the JSON dictionary can include custom keys and values with your app-specific content.

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

我认为您应该检查 self.bestAttemptContent?.userInfo 以获得 mediaUrl 数据。