繁体   English   中英

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

[英]How to stack push notifications in Android?

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

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

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

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 {

@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("Title")
            .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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM