4

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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

My android skills are very weak. I did try to do the NotificationExtenderService I just can't get it to work. If by any chance, can you create a sample repo? thanks
Additionally, you don't receive the the notification while the app is on focus unless you enable OneSignal.enableNotificationsWhenActive(true); which opens an alert.
@ryeballar Did you get it to work? Can you post a sample repo?
@jkasten Can you post a sample repo?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.