android - 尝试在我的应用程序中创建两个服务并从每个服务发送通知,但第二个替换了第一个

标签 android android-service

我正致力于在我的应用程序中创建第二个服务,由于它的工作与第一个完全不同,我希望他们发送单独的、不相关的通知。但是,第二个通知似乎取代了我状态栏中的第一个。

这是我的第一个服务的通知代码,看起来运行良好:

private void showNotification()
{
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder (this)
            .setAutoCancel (true)
            .setSmallIcon (R.drawable.icon)
            .setWhen (System.currentTimeMillis())
            .setContentTitle ("TCP Server Service Started");

    TaskStackBuilder stackBuilder = TaskStackBuilder.create (this);
    stackBuilder.addNextIntent (new Intent (this, Launcher.class));                             //  This cannot be removed

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent (0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent (resultPendingIntent);

    notificationMngr.notify (0, mBuilder.build());
}

我基本上是将那个字符提取为字符并将其粘贴到我的新服务类中。这些魔法零(在 getPendingIntent() 和 notify() 中)参数中的任何一个我应该尝试将它们分开吗?

最佳答案

来自文档。

If the ID remains unchanged, the existing notification is updated.

那里也有一个很好的例子。检查here .查找更新通知。

关于android - 尝试在我的应用程序中创建两个服务并从每个服务发送通知,但第二个替换了第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31060320/

相关文章:

android - 解绑服务但不销毁它

android - 如何使用广播接收器将事件从 Activity 发送到android中的服务

android - 创建一个在 Activity/fragment 中存活的倒数计时器

java - android ble setValue 是 0x00010000

java - 我可以从构造函数主体调用静态方法来获取实例而不是调用 Super 吗?

android - 错误: "getGamesClient() not defined" - trying to run Google's Sample Game

android - 无法在项目 :features:myDynamicFeatureModule: 的以下变体之间进行选择

android - 使用相机拍照的后台服务

Android Socket.io 后台服务

android - OpenGL ES 是否比标准 2D 图形耗电更多?