otak info official Logo   
Personal Blog

Hariyanto Lim

정길상 / 鄭吉祥
Home   Last Posts  
Please login to see more topics and discussion categories.
goto main category list >> Software programming >> Android

Title : Catch event when Notification is removed by either SWIPE or CLEAR ALL
Total Reply : 0
Total View : 5318

Total thread: 131
Total reply: 50

Post #181
Catch event when Notification is removed by either SWIPE or CLEAR ALL
In my recent mobile app project, our marketing division needs to know which promotion push message is most viewed, we send the data to Google Analytics, so far we have only implemented event for RECEIVED push message & CLICKED push message.

Apparently these are not enough, they also need to know how many user REMOVED (discard) the notification from our marketing promo push message.

The step to catch the single notification delete (swipe gesture) and/or CLEAR ALL notifications is:

  1. Register BroadcastReceiver in AndroidManifest.xml, such as:
    ', ' ', '<receiver android:name=".listener.NotificationDeletedReceiver" android:enabled="true" />').'
  2. Create the BroadcastReceiver class, such as:
    ', ' ', '
    public class NotificationDeletedReceiver extends BroadcastReceiver {
    private final String TAG = NotificationDeletedReceiver.class.getSimpleName();

    public void onReceive(Context context, Intent intent) {
    LogHelper.d(TAG, "onReceive(Context, Intent)");

    if(intent != null) {
    String msgId = intent.getStringExtra(MainActivity.NOTIFICATION_MESSAGE_ID);
    LogHelper.d(TAG, "msgId: " + msgId);

    // TODO: remove this Toast later !!
    Toast.makeText(context, "msgId: " + msgId + " is removed !", Toast.LENGTH_SHORT).show();

    //TODO: send to Google Analytics !!
    //HGoogleAnalyticWrapperSingleton.getInstance().send(HGoogleAnalyticWrapperSingleton.PUSH_MESSAGE_REMOVED, msgId, null);
    } else {
    LogHelper.d(TAG, "Intent is null (no data)");
  3. Create and link our "delete" Intent to the Notification, such as:
    ', ' ', '
    // attach "delete" Intent
    Intent deleteIntent = new Intent(ctx, NotificationDeletedReceiver.class);
    deleteIntent.putExtra(NOTIFICATION_MESSAGE_ID, notificationId + "");
    mNotification.deleteIntent = PendingIntent.getBroadcast(ctx, 0, deleteIntent, 0);

    notificationManager.notify(notificationId, mNotification);
Falling in love with the world

Write : 2017-01-17 17:20:57

If you want to create a new reply then please login first.

Since 19 January 2007
Page hit : 1,015,951

Code update 24th June 2013
Brain is a very capable to solve big problems
but requires constant reminders about how to.
peace bird