如何在React Native的PushNotificationIOS中获取自定义参数?

时间:2018-10-24 10:40:10

标签: reactjs react-native push-notification apple-push-notifications react-native-ios

我收到PushNotificationiOSreact-native的以下回复

{
  "aps": {
    "alert": "Next live session will start on 15-10-2018 13:00:00 for course NEET\/AIIMS",
    "sound": "default",
    "badge": 1,
    "relData": {
      "video_url": "",
      "start_time": "15-10-2018 13:00:00"
    }
  }
}

如何使用start_timerelData获得PushNotificatonIOS

目前,我只能使用以下代码获取警报消息:

PushNotificationIOS.addEventListener('notification', function(notification) {
    console.log('You have received a new notification! ', notification);
    Alert.alert(notification.getMessage())
});

1 个答案:

答案 0 :(得分:0)

将您的APNS notification内容修改为以下格式。

{
    "aps": {
      "alert": {
          "title": "Some Title",
          "body": "Next live session will start on 15-10-2018 13:00:00 for course NEET\/AIIMS"
      },
      "sound": "default",
      "badge": 1,
      "mutable-content": 1,
      "content-available": 1
    },
    "relData": {
        "video_url": "",
        "start_time": "15-10-2018 13:00:00"
    }
}

来自Apple文档。 https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

  

要支持后台更新通知,请确保   有效负载的aps词典包含 content-available 键,其中包含一个   值 1 。如果有用户可见的更新与   后台更新,您可以在   APS字典(视情况而定)。

在AppDelegate.m的顶部:

#import <React/RCTPushNotificationManager.h>

然后在您的AppDelegate实现中添加以下内容:

// Required for the notification event. You must call the completion handler after handling the remote notification.
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
                                                        fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
 {
   [RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
 }

在上述方法中,您将在relData中获得userInfo以及其余的有效载荷。

有关更多信息,请访问https://facebook.github.io/react-native/docs/pushnotificationios