2017-02-20 86 views

回答

2

谷歌是您的朋友:https://developer.android.com/guide/topics/ui/notifiers/notifications.html#bundle

中的Android 7.0(API級別24)開始,Android提供開發 與代表通知的隊列中的新方法:捆綁 通知。這與Android Wear的 中的通知堆棧功能類似。例如,如果您的應用程序爲收到的消息創建了通知,則當收到多條消息時,將 通知作爲一個羣組捆綁在一起。您可以使用 Build.setGroup()方法捆綁相似的通知。

+1

我通過個setgroup()方法中去。但是對於每個新通知,狀態欄中都會顯示一個圖標。我的要求是我應該得到堆疊在一個之上的通知,但只能顯示一個通知圖標。我使用「收件箱樣式」,但添加的線條是靜態的。如何以不同的通知ID顯示多個通知,但是來自同一個應用程序,堆疊在另一個之上。非常像watsapp。以前的通知不應該被傳入的新通知所取代,而是我希望它被堆疊。 –

+0

@freddy pradeep你的問題類似於http://stackoverflow.com/questions/17521908/android-gcm-multiple-push-notifications-with-one-icon-in-status-bar – Androbin

0
public class FirebaseInstanceIDService extends FirebaseInstanceIdService { 
private String TAG="FirebaseInstanceIDService"; 
private Context context; 
@Override 
public void onTokenRefresh(){ 
    context=getApplicationContext(); 
    String Token= FirebaseInstanceId.getInstance().getToken(); 
    saveToken(Token); 
} 

private void saveToken(String token) { 
    Log.i(TAG,token); 
    SharedPref.setToken("Token",token,context); 
} 
} 

清單文件創建服務

public class FirebaseMessaginService extends FirebaseMessagingService { 
int i=0; 
private int temp=0; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    showNotification(remoteMessage.getData().get("message")); 

} 

private void showNotification(String message) { 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setAutoCancel(true) 
      .setContentTitle("Track Mee") 
      .setContentText(message) 
      .setSmallIcon(R.drawable.ic_marker) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(0, builder.build()); 

} 
} 

<service android:name=".Service.FirebaseInstanceIDService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
    </intent-filter> 
</service> 
<service 
    android:name=".Service.FirebaseMessaginService" 
    android:enabled="true" 
    android:exported="true"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
    </intent-filter> 
</service>