Skip to content

Open app when notification is clicked

Sarun1001 edited this page Sep 7, 2017 · 7 revisions

So first, we go in the file RadioPlayerService.java in the function buildNotification(): We delete this line of code:

Intent intentOpenPlayer = new Intent(NOTIFICATION_INTENT_OPEN_PLAYER);

and replace it with:

Intent notificationIntent = new Intent();
        //replace "be.medias.pulseair" by your package name
        //replace "be.medias.pulseair.Home" by the path of your activity you want to open
        notificationIntent.setClassName("be.medias.pulseair", "be.medias.pulseair.Home");
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Again, delete this line of code:

PendingIntent openPending = PendingIntent.getBroadcast(this, 31, intentOpenPlayer, 0);

and replace it with:

PendingIntent openPending = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

Also change your opening activity launch mode to "singleTop" in app Manifest to resume it's previous state. Otherwise a new activity will be launched

eg:

<activity android:name=".RadioMainActivity" android:theme="@style/AppThemeBar" android:launchMode="singleTop" android:configChanges="orientation|screenSize"/>

Here it is, now your notification opens the activity of your choice 👍