如何识别来自多个Notification android的选定通知?

时间:2017-04-07 07:54:53

标签: android push-notification android-notification-bar

这是我的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());
}

}

在这里我能够生成多个通知,但无法从通知栏中的多个通知中识别出特定通知,现在它正在显示最新通知的标题和消息正文,伙计们请帮帮我。

1 个答案:

答案 0 :(得分:1)

notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), notificationBuilder.build());

(int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE)替换为您自己的识别逻辑(例如增量整数)。