'Android - Setting up OneSignal's setNotificationOpenedHandler on MainActivity does not launch app
I'm working on an Android app (Java; minSdkVersion 21) and using One Signal to receive notifications. The One Signal notification will send a payload with enough data to build an Episode object which the setNotificationOpened handler will use to navigate to a particular episode in the app.
I'm doing this in MainActivity (the app has this one activity, everything else is fragments):
@Override
protected void onCreate(Bundle savedInstanceState) {
...
OneSignal.setNotificationOpenedHandler(
new OneSignal.OSNotificationOpenedHandler() {
@Override
public void notificationOpened(OSNotificationOpenedResult result) {
OSNotification notification = result.getNotification();
try {
String notificationType = (String) notification.getAdditionalData().get("type");
if (notificationType.equals("episode")) {
Episode episode = NotificationHandler.setUpEpisode(notification.getAdditionalData()); // returns an episode object
Bundle bundle = new Bundle();
bundle.putSerializable("episode", episode);
bundle.putString("title", episode.getName());
navController.navigate(R.id.action_global_episodeFragment, bundle);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
...
This setup works fine when the app is in the background. The user clicks on the notification, the app comes to the foreground and navigates to the appropriate episode. However, when the app is closed and the user clicks on the notification, the app is never launched.
I initially tried to put this handler in the AppController instead of MainActivity but couldn't find a way of navigating since R was not available at this point.
How can I get the above code to also work in cases where the app is closed? }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
