Sava Dimitrijević
Sava Dimitrijević

Reputation: 396

Click on notification not launching the app

When push notification is received while the app is not running and the user presses on notification, it just disappears and the log says:

2019-10-22 12:42:45.747 23260-23260/de.app.test.staging E/FirebaseMessaging: Notification pending intent canceled

Here is the SplashActivity that is supposed to be launched as a Launcher Activity:

    <activity
        android:name="de.app.test.login.SplashActivity"
        android:screenOrientation="portrait"
        android:exported="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

What can the issue be here?

Upvotes: 5

Views: 4096

Answers (3)

Sava Dimitrijević
Sava Dimitrijević

Reputation: 396

For anyone looking for an answer, the back-end was sending the "click_action" to notification, therefore there was no intent filter for that Activity.

For me the click_action is "OPEN_ACTIVITY_1" so I just added one more intent-filter to my SplashActivity like this:

<intent-filter>
        <action android:name="OPEN_ACTIVITY_1" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Upvotes: 8

majid ghafouri
majid ghafouri

Reputation: 869

Forground app:

You need a PendingIntent to open app.

Try this:

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

Intent notificationIntent = new Intent(context, SplashActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
        notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

If Activity is in background or closed then notification message is shown in the notification center for app launcher activity.

You can use a BroadcastReceiver class. When you close the app, broadcast can listen this action. So, if you create a BroadcastReceiver class, you don't take this problem.

public class ClosedBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context, HomePage.class));
        } else {
            context.startService(new Intent(context, HomePage.class));
        }
    }
}

More Information:

https://firebase.google.com/docs/cloud-messaging/android/receive

Upvotes: 0

Adarsh Binjola
Adarsh Binjola

Reputation: 246

you need to set pendingIntent object in notificationBuilder instance like

 mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
    Intent intent = new Intent(context, TargetActivity.class));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    builder.setContentIntent(pendingIntent);

Please note that CHANNEL_ID is for build version greater or equal to Oreo

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CHANNEL_ID = createNotificationChannel(CHANNEL_ID, "channelName");
    }


  @RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(String channelId, String channelName) {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library

    String description = "description";

    NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
    channel.setLightColor(Color.BLUE);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    channel.setDescription(description);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    mNotificationManager.createNotificationChannel(channel);
    return channelId;

}

Upvotes: 0

Related Questions