我正在使用以下代码实现推送通知。它工作正常,但它在设备上的状态栏中显示多个通知。如何将多个通知合并到单个状态栏中。请建议。
//code//
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.text.TextUtils;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
import java.util.Random;
public class FirebasePluginMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebasePlugin";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://firebase.google.com/support/faq/#fcm-android-background
String title;
String text;
String id;
if (remoteMessage.getNotification() != null) {
title = remoteMessage.getNotification().getTitle();
text = remoteMessage.getNotification().getBody();
id = remoteMessage.getMessageId();
} else {
title = remoteMessage.getData().get("title");
text = remoteMessage.getData().get("text");
id = remoteMessage.getData().get("id");
}
if(TextUtils.isEmpty(id)){
Random rand = new Random();
int n = rand.nextInt(50) + 1;
id = Integer.toString(n);
}
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message id: " + id);
Log.d(TAG, "Notification Message Title: " + title);
Log.d(TAG, "Notification Message Body/Text: " + text);
// TODO: Add option to developer to configure if show notification when app on foreground
if (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title) || (!remoteMessage.getData().isEmpty())) {
boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback()) && (!TextUtils.isEmpty(text) || !TextUtils.isEmpty(title));
sendNotification(id, title, text, remoteMessage.getData(), showNotification);
}
}
private void sendNotification(String id, String title, String messageBody, Map<String, String> data, boolean showNotification) {
Bundle bundle = new Bundle();
int numMessages = 0;
for (String key : data.keySet()) {
bundle.putString(key, data.get(key));
}
if (showNotification) {
NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, OnNotificationOpenReceiver.class);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText("New message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
int resID = getResources().getIdentifier("notification_icon", "drawable", getPackageName());
if (resID != 0) {
notificationBuilder.setSmallIcon(resID);
} else {
notificationBuilder.setSmallIcon(getApplicationInfo().icon);
}
notificationManager.notify(1, notificationBuilder.build());
} else {
bundle.putBoolean("tap", false);
FirebasePlugin.sendNotification(bundle);
}
}
}
答案 0 :(得分:2)
有两种方法可以合并多个通知。您可以使用摘要或捆绑来折叠通知。
https://developer.android.com/guide/topics/ui/notifiers/notifications.html 是一个很好的资源。
<强>综述强>: 这是通过创建具有给定ID的通知来完成的,以便您可以在以后收到另一个通知时更新它
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
...
EX:
<强>捆绑强>:
这可以通过使用Builder.setGroup()功能来完成。
有关通知指南的详细信息,请查看android通知设计模式文档。 https://material.io/guidelines/patterns/notifications.html#notifications-behavior