react-native-onesignal receive push notification without opening notification

淺唱寂寞╮ 提交于 2019-12-04 10:57:41

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!