1

I'm trying to stack push notifications with Firebase Cloud Messaging and I'm facing a problem, I don't know how to detect that the push notification has been cleared (with a sweep or with the Clear all button).

I've tried almost every tutorial I've seen on Stackoverflow with no success, so I'm missing something.

This is what I have right now:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

final static String GROUP_KEY_EMAILS = "group_key_emails";

protected PendingIntent getContentIntent(String data)
{
    Intent notificationIntent = new Intent(getApplicationContext(), SplashScreen.class);
    notificationIntent.putExtra("custom", data);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    return PendingIntent.getActivity(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

protected PendingIntent getDeleteIntent()
{
    Intent intent = new Intent(getApplicationContext(), NotificationBroadcastReceiver.class);
    intent.setAction("notification_cancelled");
    return PendingIntent.getBroadcast(getApplicationContext(), 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if(remoteMessage.getNotification() != null)
    {
        try {
            Map<String, String> data = remoteMessage.getData();
            String customData = data.get("custom");
            JSONObject customJSON = new JSONObject(customData);
            final JSONObject pushData = customJSON.getJSONObject("custom data");

            String message = remoteMessage.getNotification().getBody();
            Notification notif1 = new android.support.v4.app.NotificationCompat.Builder(getApplicationContext())
                    .setContentTitle("My app")
                    .setContentText(message)
                    .setSmallIcon(R.drawable.icon)
                    .setAutoCancel(true)
                    .setGroup(GROUP_KEY_EMAILS)
                    .setContentIntent(getContentIntent(customData))
                    .setDeleteIntent(getDeleteIntent())
                    .build();

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            notificationManager.notify(1, notif1);
            AuxiliaryFunctions.addPushNotificationsMessage(getApplicationContext(), message);
            int notifId = AuxiliaryFunctions.readPushNotificationsMessagesNum(getApplicationContext());
            if(notifId > 1)
            {
                NotificationCompat.InboxStyle style = new android.support.v4.app.NotificationCompat.InboxStyle()
                        .setSummaryText(notifId + " new messages");
                JSONArray messages = AuxiliaryFunctions.readPushNotificationsMessages(getApplicationContext());
                for(int i = 0; i < messages.length(); i++)
                {
                    String localmessage = messages.getString(i);
                    style.addLine(localmessage);
                }
                Notification summaryNotification = new android.support.v4.app.NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle("My app "+notifId+" new messages")
                        .setSmallIcon(R.drawable.iconplus)
                        .setStyle(style)
                        .setGroup(GROUP_KEY_EMAILS)
                        .setGroupSummary(true)
                        .setAutoCancel(true)
                        .setContentIntent(getContentIntent(""))
                        .setDeleteIntent(getDeleteIntent())
                        .build();

                notificationManager.notify(1, summaryNotification);
            }

        } catch (JSONException e) {

        }
    }
  }
}

This is my Notification Broadcast Receiver:

public class NotificationBroadcastReceiver  extends BroadcastReceiver   
{

  @Override
  public void onReceive(Context context, Intent intent)
  {
    Log.e("", "NotificationBroadcastReceiver:onReceive");
  }
}

And finally I declare this in my manifest:

       <receiver android:name="auxiliary.NotificationBroadcastReceiver">
        <intent-filter>
            <action android:name="notification_cancelled"/>
        </intent-filter>
    </receiver>

Well, the notifications are shown correctly, they are being stacked if there are more than one notification and if the user clicks on the notification (summary or not) everything is going well and the notification is processed with no problem. But if the user makes a sweep in the notification (summary or not) or clicks on the Clear all notifications button, the notification dissapears but the NotificationBroadcastReceiver is never called (the log NotificationBroadcastReceiver:onReceive is not shown in the console).

What am I missing??

Wonton
  • 1,033
  • 16
  • 33
  • See also https://stackoverflow.com/questions/65703474/how-to-send-a-request-when-push-notification-is-cancelled-in-android. Thanks for `setGroup`, `setGroupSummary`. – CoolMind Jan 15 '21 at 07:36

1 Answers1

0

I'm very ashamed! I've found out the reason of my problem and it was complete stupidity.

In my AndroidManifest I had this:

<receiver android:name="auxiliary.NotificationBroadcastReceiver">
        <intent-filter>
            <action android:name="notification_cancelled"/>
        </intent-filter>
    </receiver>

I forgot the dot before auxiliary. Once I put it:

<receiver android:name=".auxiliary.NotificationBroadcastReceiver">
            <intent-filter>
                <action android:name="notification_cancelled"/>
            </intent-filter>
        </receiver>

the receiver callback was called.

What surprise me is that AndroidManifest gave no error resolving the wrong path.

Wonton
  • 1,033
  • 16
  • 33