How to get notification id?

2019-03-22 08:15发布

问题:

i developed an basic notification example and tried to catch which notification clicked. But i couldnt.

I put an arraylist my notificaiton and pass it to reciever activity by putting extras, then try to catch but no way !

Are there anyway to catch which one cliked ?

回答1:

You can pass a Bundle along with PendingIntent to the next Activity.

Bundle bundle = new Bundle();
bundle.putString("Any String");
NotificationManager notifManager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
int uniqueInteger = //create a unique Integer        
int icon = R.drawable.ic_launcher;
NotificationCompat2.Builder mNotification = new NotificationCompat2.Builder(this).setSmallIcon(icon)
                .setContentTitle(contentTitle).setContentIntent(getPendingIntent(bundle, uniqueInteger));

mNotification.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mNotification.setAutoCancel(true);
notifManager.notify(uniqueInteger, mNotification.build());

and the method getPendingIntent()

private PendingIntent getPendingIntent(Bunble bundle, int rc) { 
Intent notificationIntent = new Intent(this, YourNextActivity.class);
notificationIntent.putExtras(bundle);
return PendingIntent.getActivity(this, rc, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

I am using NotificationCompat2 by Jake Wharton here but the answer does not depend on that.