如何从多个通知 android 中识别选定的通知?
how to identify selected notification from multiple Notification android?
这是我的 MyFirebaseMessagingService,我用它在应用程序中生成通知。
class MyFirebaseMessagingService extends FirebaseMessagingService {
private final String TAG = this.getClass().getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification MESSAGE Body: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "onMessageReceived: " + remoteMessage.getNotification().getTag());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle());
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody, String title) {
Intent intent = new Intent(this, NotificationDetailActivity.class);
intent.putExtra(Appconstant.NOTIFICATION, messageBody);
intent.putExtra(Appconstant.TITLE, title);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notificationBuilder.build());
}
}
这里我可以生成多个通知,但无法从通知栏中的多个通知中识别特定通知,现在它显示最新通知的标题和消息body,请大家帮帮我。
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notificationBuilder.build());
将(int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE)
替换为您自己的标识逻辑(例如增量整数)。
这是我的 MyFirebaseMessagingService,我用它在应用程序中生成通知。
class MyFirebaseMessagingService extends FirebaseMessagingService {
private final String TAG = this.getClass().getSimpleName();
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification MESSAGE Body: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "onMessageReceived: " + remoteMessage.getNotification().getTag());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle());
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody, String title) {
Intent intent = new Intent(this, NotificationDetailActivity.class);
intent.putExtra(Appconstant.NOTIFICATION, messageBody);
intent.putExtra(Appconstant.TITLE, title);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notificationBuilder.build());
}
}
这里我可以生成多个通知,但无法从通知栏中的多个通知中识别特定通知,现在它显示最新通知的标题和消息body,请大家帮帮我。
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notificationBuilder.build());
将(int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE)
替换为您自己的标识逻辑(例如增量整数)。