Skip to content

Instantly share code, notes, and snippets.

@coopbri
Created January 3, 2021 19:31
Show Gist options
  • Save coopbri/dd233a2a5756c9a41d94934049f814a2 to your computer and use it in GitHub Desktop.
Save coopbri/dd233a2a5756c9a41d94934049f814a2 to your computer and use it in GitHub Desktop.
rn_onesignal
import com.onesignal.OSNotification;
import com.onesignal.OSMutableNotification;
import com.onesignal.OSNotificationReceivedEvent;
import com.onesignal.OneSignal.OSRemoteNotificationReceivedHandler;
/**
* Notification service extension. Handles receiving of notifications in destroyed lifecycle state.
*/
public class NotificationServiceExtension implements OSRemoteNotificationReceivedHandler {
/**
* {@inheritDoc}
*/
@Override
public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
// extract notification from event
OSNotification notification = notificationReceivedEvent.getNotification();
// add logic to do whatever you want with notification here
// ...
}
}

App.tsx

const App = () => {
  // OneSignal setup
  useEffect(() => {
    OneSignal.setLogLevel(6, 0);
    OneSignal.init(ONESIGNAL_APP_ID, {
      kOSSettingsKeyAutoPrompt: false,
      kOSSettingsKeyInAppLaunchURL: false,
      kOSSettingsKeyInFocusDisplayOption: 2,
    });
    OneSignal.inFocusDisplaying(2);
  }, []);

  return ...;
}

Screen (First screen in stack navigator)

  // handle opened push notification
  useEffect(() => {
    OneSignal.addEventListener("opened", (res) => {
      const data = res.notification.payload.additionalData;

    // handle data here
    // ...

    return () => {
      OneSignal.removeEventListener("opened");
    };
  }, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment