Open android app from PUSH notification

跟風遠走 提交于 2019-12-03 03:07:21

Create a pending Intent to start the activity and set it in notification using setLatestEventInfo.

Example:

  Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

More info can be found here

You need to use a custom notification builder and use one of your activities as the PendingIntent.

https://docs.urbanairship.com/android-lib/reference/com/urbanairship/push/CustomPushNotificationBuilder.html

Following one of their sample projects (https://github.com/urbanairship/android-samples/tree/master/app/src/main/java/com/urbanairship/sample), you can extend the AirshipReceiver class and then override the onReceive method. This did the trick for me:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    String action = intent.getAction();
    if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
        Intent launch = new Intent(Intent.ACTION_MAIN);
        launch.setClass(UAirship.shared().getApplicationContext(), MyHome.class);
        launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        launch.putExtra("doWhatever",true);
        UAirship.shared().getApplicationContext().startActivity(launch);
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!