2017-02-20 65 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>