简体   繁体   English

如何创建多个前台通知?

[英]How to create more than one foreground notification?

Below is a snippet of code in my service class.下面是我的服务类中的一段代码。

If a user "joins a team" (operation = 0) then it create a notification with its designated specifications however if a user shares their location (operation = 1) then its supposed to create a separate foreground notification.如果用户“加入团队”(操作 = 0),那么它会创建一个具有指定规范的通知,但是如果用户共享他们的位置(操作 = 1),那么它应该创建一个单独的前台通知。 Instead one just replaces the other.相反,一个只是替换另一个。

I don't know why, they have different ID's just same channel.我不知道为什么,他们有不同的 ID 只是同一个频道。 I've also tried separating their channel ID, still the same issue我也试过把他们的频道 ID 分开,还是同样的问题

int id = NOTIFICATION_LOCATION;
        int icon = R.drawable.ic_gps_on;
        String message = "Tap to disable location updates";

        if (operation == 0) {
            id = NOTIFICATION_RESPONDER;
            icon = R.drawable.ic_responder_icon;
            message = "Tap to leave the responding team";
        }

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_1)
                .setSmallIcon(icon)
                .setContentTitle("Location established")
                .setContentText(message)
                .setContentIntent(PendingIntent.getBroadcast(getApplicationContext(), 0, getBroadcastIntent(operation), PendingIntent.FLAG_UPDATE_CURRENT))
                .setColor(ContextCompat.getColor(getApplicationContext(), R.color.primaryColor))
                .setDefaults(Notification.DEFAULT_SOUND)
                .setVisibility(VISIBILITY_PUBLIC)
                .build();

        startForeground(id, notification);

The notification you manipulate with startForeground() is meant to be the one "official" notification that corresponds to the foreground service;您使用startForeground()操作的通知是对应于前台服务的一个“官方”通知; the one that Android insists you have up at all times the service is running. Android 坚持要求您在服务运行时始终启动的那个。

It doesn't surprise me that, if you supply a different notification channel ID on a subsequent call to startForeground() , it erases and replaces the original notification.我并不感到惊讶,如果您在随后的startForeground()调用中提供不同的通知通道 ID,它会擦除替换原始通知。 Otherwise, you might end up with multiple foreground notifications for a single service, and things could get confusing.否则,您最终可能会收到针对单个服务的多个前台通知,并且事情可能会变得混乱。

Instead, just use NotificationManager.notify() to manage any notifications that occur in excess of the original foreground service notification.相反,只需使用NotificationManager.notify()来管理超出原始前台服务通知的任何通知。 Use distinct IDs for these extra notifications.对这些额外通知使用不同的 ID。

A good practice is to use a fixed ID for your foreground service notification.一个好的做法是为您的前台服务通知使用一个固定的 ID。 You can still change the Notification at will;您仍然可以随意更改Notification it's just easier to remember which Notification is your "official" one, when you have a fixed ID.当您拥有固定 ID 时,更容易记住哪个Notification是您的“官方” Notification

You can also manipulate your "official foreground service notification" using notify() ;您还可以使用notify()操纵您的“官方前台服务通知”; you don't have to use startForeground() .您不必使用startForeground() A call to startForeground() is only needed once , at the beginning, to associate the service with a specific notification ID.只需在开始时调用startForeground()一次,即可将服务与特定通知 ID 相关联。

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

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