Harry
 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: - Register BroadcastReceiver in AndroidManifest.xml, such as:
''.str_replace(' ', '
', '<receiver android:name=".listener.NotificationDeletedReceiver" android:enabled="true" />').' '
- Create the BroadcastReceiver class, such as:
''.str_replace(' ', '
', ' public class NotificationDeletedReceiver extends BroadcastReceiver { private final String TAG = NotificationDeletedReceiver.class.getSimpleName();
@Override 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)"); } } } ').' '
- Create and link our "delete" Intent to the Notification, such as:
''.str_replace(' ', '
', ' // 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 |