当app在后台时,使用React Native进行无提示iOS推送通知

时间:2021-09-16 20:39:20

I have a React Native app where I am trying to get a silent iOS push notification sent to a handler in JavaScript.

我有一个React Native应用程序,我试图在JavaScript中发送一个静默的iOS推送通知发送给处理程序。

The behaviour I am seeing is that the didReceiveRemoteNotification function in AppDelegate gets called but my handler in JavaScript doesn't get called unless the app is in the foreground or has only been closed recently.

我看到的行为是AppDelegate中的didReceiveRemoteNotification函数被调用,但我的JavaScript处理程序不会被调用,除非应用程序在前台或最近才关闭。

The thing I am confused about is clearly the app is being woken up and having it's didReceiveRemoteNotification function called, but then the call to [RCTPushNotificationManager didReceiveRemoteNotification:notification] doesn't seem to do anything.

我很困惑的事情显然是应用程序被唤醒并且调用了didReceiveRemoteNotification函数,但是对[RCTPushNotificationManager didReceiveRemoteNotification:notification]的调用似乎没有做任何事情。

Also, if I open the app after a notification has been received, then I see the React Native handler is called at that point.

此外,如果我在收到通知后打开应用程序,那么我会看到此时调用React Native处理程序。

My didReceiveRemoteNotification function looks like this:

我的didReceiveRemoteNotification函数如下所示:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
  NSLog(@"didReceiveRemoteNotification");

  [RCTPushNotificationManager didReceiveRemoteNotification:notification];
}

In the Root component of my React Native app I have this:

在我的React Native应用程序的Root组件中,我有:

componentDidMount() {
    AppState.addEventListener('change', this.handleAppStateChange);

    PushNotificationIOS.addEventListener('notification', (notification) => {
        console.log("notification recieved");
    })
}
handleAppStateChange(currentAppState) {
    console.log(currentAppState);
}

I am sending a push notification using AWS SNS with the following message:

我正在使用AWS SNS发送推送通知,并显示以下消息:

{
"APNS_SANDBOX":"{\"aps\":{\"content-available\":\"1\"}}"
}

Here is the log from XCode:

这是来自XCode的日志:

2016-04-20 10:38:01.255 [info][tid:com.facebook.React.JavaScript] inactive
2016-04-20 10:38:01.986 [info][tid:com.facebook.React.JavaScript] background
2016-04-20 10:38:17.279 test[4056:1383261] didReceiveRemoteNotification
2016-04-20 10:38:17.284 [info][tid:com.facebook.React.JavaScript] notification recieved
2016-04-20 10:44:56.330 test[4056:1383261] didReceiveRemoteNotification
2016-04-20 10:44:56.332 [info][tid:com.facebook.React.JavaScript] notification recieved
2016-04-20 10:46:07.091 test[4056:1383261] didReceiveRemoteNotification
2016-04-20 10:49:30.039 [info][tid:com.facebook.React.JavaScript] notification recieved
2016-04-20 10:49:30.639 [info][tid:com.facebook.React.JavaScript] active

In this log, 3 push notifications where sent. The ones received at 10:38 and 10:44 both called JavaScript correctly. However, the one received at 10:46 didn't call the handler in JavaScript until I opened the app at 10:49.

在此日志中,发送了3个推送通知。在10:38和10:44收到的都正确地调用了JavaScript。但是,在10:46收到的那个没有在JavaScript中调用处理程序,直到我在10:49打开应用程序。

Is there anything I can do to ensure the call to my React Native code occurs even with app not running?

有什么办法可以确保即使应用程序没有运行也会调用我的React Native代码吗?

1 个解决方案

#1


5  

In order for notifications to hit your app in the background you need to also define a fetchCompletionHandler, with a completion handler function like below. The aps:{content-available:1} payload should wake up you application and trigger this code in your AppDelegate, and in turn hit your JavaScript in RN.

为了让通知在后台点击您的应用,您还需要定义一个fetchCompletionHandler,其完成处理函数如下所示。 aps:{content-available:1}有效负载应该唤醒您的应用程序并在您的AppDelegate中触发此代码,然后在RN中点击您的JavaScript。

// fetch notifications in the background and foreground
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

[RCTPushNotificationManager didReceiveRemoteNotification:notification];
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"Notification Body %@", notification);

}

#1


5  

In order for notifications to hit your app in the background you need to also define a fetchCompletionHandler, with a completion handler function like below. The aps:{content-available:1} payload should wake up you application and trigger this code in your AppDelegate, and in turn hit your JavaScript in RN.

为了让通知在后台点击您的应用,您还需要定义一个fetchCompletionHandler,其完成处理函数如下所示。 aps:{content-available:1}有效负载应该唤醒您的应用程序并在您的AppDelegate中触发此代码,然后在RN中点击您的JavaScript。

// fetch notifications in the background and foreground
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

[RCTPushNotificationManager didReceiveRemoteNotification:notification];
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"Notification Body %@", notification);

}