【问题标题】:How to make persistent notification?如何进行持久通知?
【发布时间】:2018-10-11 16:15:48
【问题描述】:

我正在制作一个通过蓝牙与 Aurdino 通信的应用程序。它使用通信部分的服务。因此,只要服务正在运行,我希望应用程序显示通知。用户不应该能够滑动通知以使其消失。

到目前为止,这是我的通知功能:

void showNotification(String title, String contentText)
{
 ...

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "default")
                .setSmallIcon(R.mipmap.ic_launcher) // notification icon
                .setContentTitle(title) // title for notification
                .setContentText(content)
                .setAutoCancel(false)
                .setOngoing(true);

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

请注意,我已经调用了setOngoing(true),但我仍然可以从通知抽屉中滑动通知,并且当我这样做时它会消失。

如何防止用户滑动和取消通知?我的应用程序在终止时会自动取消它,在服务的OnDestroy() 中。

【问题讨论】:

    标签: android notifications persistent


    【解决方案1】:

    你需要做一个前台服务,当你的服务处于活动状态时它也会通知

    所以你的服务变得很难被系统杀死

    https://www.dev2qa.com/android-foreground-service-example/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      相关资源
      最近更新 更多