react-native-onesignal receive push notification w

2020-07-17 15:31发布

问题:

I have checked react-native-onesignal github README and it seems the only way to get the notification is to open which is through the onNotificationOpened() callback.

Their documentation stated that:

When any notification is opened or received the callback onNotification is called passing an object with the notification data.

But onNotification clearly does not work.

Is there any way to get the notification without opening the push notification or enabling the in-app alert notification?

回答1:

The onNotification function will only be called when opening a notification or when one is received while the app is in focus.

If you need to handle a notification in the background before it is opened you will need to do so with native code.

iOS - set content_available to true on the OneSignal create notification REST API POST call, this will fire the - application:didReceiveRemoteNotification:fetchCompletionHandler: selector.

Android - Setup a NotificationExtenderService by following the OneSignal Background Data and Notification Overriding instructions.



回答2:

Regarding implementation of NotificationExtenderService in android (not answering the original question, but the question asked by @ryeballar), as explained in https://documentation.onesignal.com/docs/android-customizations#section-background-data-and-notification-overriding:

  • I am getting notifications when the app is closed/swiped-out/not-started without implementing NotificationExtenderService
  • However, in order to implement NotificationExtenderService you need to do the following (as described in the onesignal documentation referenced above, note that there is a typo there, fixed below):

    1. Create a file called NotificationExtender.java in node_modules\react-native-onesignal\android\src\main\java\com\geektime\rnonesignalandroid, with the following contents:

.

import com.onesignal.OSNotificationPayload;
package com.geektime.rnonesignalandroid;
import com.onesignal.OSNotificationPayload;
import com.onesignal.OSNotificationPayload;
import com.onesignal.NotificationExtenderService;
import com.onesignal.OSNotificationReceivedResult;

public class NotificationExtender extends NotificationExtenderService {
   @Override
   protected boolean onNotificationProcessing(OSNotificationReceivedResult receivedResult) {
        // Read properties from result.

      // Return true to stop the notification from displaying.
      return false;
   }
}
  1. Add the following to android\app\src\main\AndroidManifest.xml:

.

<service android:name=".NotificationExtender" 
   android:permission="android.permission.BIND_JOB_SERVICE" 
   android:exported="false">
  <intent-filter>
    <action android:name="com.onesignal.NotificationExtenderService" />
  </intent-filter>
</service>
  1. rebuild your app