如何在Android中堆叠推送通知?

时间:2017-02-20 14:31:51

标签: android notifications

我正在处理推送通知。当我收到多个通知时,它们都会显示在状态栏中。我需要一个解决方案,其中状态栏中只显示一个图标,并且通知堆叠在一起。

2 个答案:

答案 0 :(得分:2)

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

  

从Android 7.0(API级别24)开始,Android为开发人员提供服务   用一种新的方式来表示通知队列:捆绑   通知。这类似于通知堆栈功能   Android Wear。例如,如果您的应用为其创建通知   收到消息,当收到多条消息时,将其捆绑   通知一起作为一个组。你可以使用   用于捆绑类似通知的Builder.setGroup()方法。

答案 1 :(得分:-1)

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>