How to navigate to specific page when tapped on One Signal push notification in Android?

Viewed 403

I have an e-commerce Android app in which I am using One Signal for Push Notifications. I have 2 types of Notifications in my App.

  1. Whenever there are new offers on items Admin will push one notification, if user taps on this notification it should open the item details screen directly.
  2. For order status. If order status changes user will get a notification and if user taps on this notification it should open the order details screen directly.

How to implement these functionalities in android by using One Signal?

1 Answers

In OneSignal Service Class

public class NotificationServiceExtension implements OSRemoteNotificationReceivedHandler {

@Override
public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent notificationReceivedEvent) {
    OSNotification notification = notificationReceivedEvent.getNotification();

    // Example of modifying the notification's accent color
    OSMutableNotification mutableNotification = notification.mutableCopy();
    mutableNotification.setExtender(builder -> {
        //... do stuff
        builder.setTimeoutAfter(30000);
        Intent intent = new Intent();
                JSONObject data = notification.payload.additionalData;
              
               // check the data and create intent
                intent = new Intent(getApplicationContext(), OrderDetailScreen.class); 
               // or any other depends on data value
               intent.putExtra("data", data);
               PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                newbuilder[0] = builder.setContentIntent(pendIntent);
        return builder;
    });
    JSONObject data = notification.getAdditionalData();
    Log.i("OneSignalExample", "Received Notification Data: " + data);

    // If complete isn't call within a time period of 25 seconds, OneSignal internal logic will show the original notification
    // To omit displaying a notification, pass `null` to complete()
    notificationReceivedEvent.complete(mutableNotification);
}

}

Related