【发布时间】:2016-11-13 18:26:11
【问题描述】:
如何在 FirebaseMessagingService 中合并推送通知。我几乎尝试了所有方法,但似乎没有任何效果。对于每个新的 Data 对象,它都会发出一个新的通知。我打印通知数量的日志打印 0。
是否有任何方法可以跟踪通知抽屉中是否有具有相同notificationId 的未读通知,以便将新通知与其合并?
任何帮助将不胜感激。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private static final String actionLiked = "liked";
mNumber=0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
ArrayList<String>notificationString= new ArrayList<>();
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Log.d(TAG, "Number of notifications" +mNumber);
Map<String, String> dataFromCloud = remoteMessage.getData();
String action = dataFromCloud.get("action");
switch (action) {
case actionLiked:
notificationString.add(action);
Intent intent = new Intent(this, MainActivity.class);
sendNotification(action, intent);
break;
default:
break;
}
}
}
private void sendNotification(String messageTitle, Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String[] events = new String[6];
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setNumber(++numMessages);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for (int i=0; i < notificationString.size(); i++) {
inboxStyle.addLine(notificationString.get(i));
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}}
【问题讨论】:
-
我还想合并多个来自 firebase 云消息的推送通知。但没有办法做到这一点。
-
@GulnazGhanchi 看看我的回答,如果它适合你,请告诉我。
标签: android push-notification android-notifications firebase-cloud-messaging firebase-notifications