I want to make OneSignal open another activity in android studio. Follow the activity with the handler.
OneSignal.startInit(this)
.setNotificationReceivedHandler(new PlocarNotificationReceivedHandler())
.setNotificationOpenedHandler(new PlocarNotificationOpenedHandler(PrincipalActivity.this))
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.None)
.init();
public class PlocarNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
@Override
public void notificationReceived(OSNotification notification) {
JSONObject data = notification.payload.additionalData;
String customKey;
if (data != null) {
customKey = data.optString(getResources().getString(R.string.action_display_campanha), null);
if (customKey != null) {
Intent broadcast = new Intent();
broadcast.setAction(getResources().getString(R.string.action_display_campanha));
broadcast.putExtra("id_campanha", customKey);
sendBroadcast(broadcast);
}
}
}
}
public class PlocarNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
private Context context;
public
PlocarNotificationOpenedHandler(Context context) {
this.context = context;
}
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
if (data != null) {
customKey = data.optString(getResources().getString(R.string.action_display_campanha), null);
if (customKey != null) {
Intent myNewActivity = new Intent(context, LoginActivity.class);
myNewActivity.putExtra("id_campanha", customKey);
startActivity(myNewActivity);
}
}
if (actionType == OSNotificationAction.ActionType.ActionTaken) {
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
}
}
}