2017-02-17 42 views
3

我正在使用下面的代碼實現推送通知。它的工作正常,但它在設備上的狀態欄中顯示多個通知。如何將多個通知合併到單個狀態欄中。請建議。在設備上獲取多個通知

//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); 
    } 
} 


    } 

回答

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:

enter image description here

捆綁

這可以通過使用Builder.setGroup()功能來完成。

enter image description here

看看Android的通知設計模式文檔,瞭解有關通知準則的詳細信息。 https://material.io/guidelines/patterns/notifications.html#notifications-behavior

+0

謝謝大衛:) – Vicky

+0

親愛的大衛,我們被困在堆疊推送通知。你可以請檢查,讓我們知道,如果你可以幫助 http://stackoverflow.com/questions/43852467/cannot-find-symbol-in-push-notification-error-notificationbuilder-setcontenttex – Vicky

相關問題