I am working on a GPS app. My app will fire notification by NotificationManager if the user go inside or outside a predefined zone. My app can fire notification for both cases.
From onResume(), I always get the latest intent.setExtra() value instead of of intent.setExtra value of the clicked notification.
The problem is how to detect the user click on the notification for inside zone or outside zone? (I want to show different view on different case)
Is it possible to add a listener for notification clicked?
Here is my code spinet:
private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag)
{
Notification notification = new Notification(R.drawable.buslogo, message, System.currentTimeMillis());
PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (vibrate)
notification.vibrate=new long[] {100L, 100L, 200L, 500L};
if (playSound)
notification.defaults |= Notification.DEFAULT_SOUND;
notification.number = ++notificationCounter;
notificationMgr.notify(notificationTag, notificationCounter, notification);
}
@Override
protected void onNewIntent( Intent intent ) {
Log.i( TAG, "*********** onNewIntent(), intent = " + intent );
if (intent.getExtras() != null)
{
Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test"));
}
super.onNewIntent( intent );
setIntent( intent );
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "*********** Main - onResume()");
Intent intent = this.getIntent();
if (intent.getExtras() != null)
{
Log.i(TAG, "test = " + intent.getExtras().getString("test"));
}
}
public void createNotification(String msnContent)
{
Intent intent = new Intent();
intent.setClass(this, Main.class);
Bundle bundle = new Bundle();
bundle.putString("test", msnContent);
intent.putExtras(bundle);
displayNotificationMessage(msnContent, true, true, intent, "test");
}
Is it possible to add a listener for notification clicked?
Use a different PendingIntent
for each case. If you are only changing an extra, be sure to use FLAG_UPDATE_CURRENT
when creating the PendingIntent
.