11

I have a group of notifications in Android App. I need my application to know if the app was opened when the user clicked on the notification. I have used a pending intent and it works brilliantly. But how can I check if the user has clicked on the notification group? It shows no links, no notifications sounds, nothing. I found no information in the documentation. Please help.enter image description here

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23
Yakov Kemer
  • 373
  • 3
  • 17
  • Did you found solution? – Vitaliy A Feb 04 '19 at 13:53
  • @VitaliyA I'm looking for a solution too – ivanovd422 Feb 05 '19 at 17:08
  • Solved it by changing notification structure. Usualy notification may contain 2 parts: "notification" and "data". So if you want to handle everything by yourself, just remove the "notification" part. In this case you get full controll on notifications when app in background or foreground. Otherwise, firebase will handle all background notifications by it self. – Vitaliy A Feb 06 '19 at 21:13
  • @VitaliyA It doesn't sound like a solution to the question. I already receive that _data_ type notifications & clicking on a group of notifications leads to restarting the Activity. – ivan8m8 Dec 25 '19 at 13:54

1 Answers1

-2

You can add flag in intent like isFromNotification boolean and then pass that intent to pending intent and then notify user.

Now in activity check intent has this parameter then the user has come from notification. (simple like that)

Sample code

Intent notificationIntent = new Intent(this, TargetActivity.class);

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TASK
            | Intent.FLAG_ACTIVITY_NEW_TASK);

String channel_Id = "app_channel_01";

notificationIntent.putExtra(AppConstants.INTENTDATA.ISFROMNOTIFICATION,
        true);

PendingIntent pendingIntent = PendingIntent.getActivity(MyApplication.getInstance(),
            0, notificationIntent,
            0);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyApplication.getInstance(), channel_Id)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setColor(MyApplication.getInstance().getResources().getColor(android.R.color.white))
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis());

NotificationManager notificationManager =
    (NotificationManager) MyApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    // Create or update.
    NotificationChannel channel = new NotificationChannel(channel_Id,
        "General Notifications",
        NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(channel);
}

notificationManager.notify(notificationId, notificationBuilder.build());
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
  • How's that related to determination of whether the user taps on a single notification or a notification group? – ivan8m8 Dec 25 '19 at 13:57