【问题标题】:How can I modify a Notification that is created with StartForeground?如何修改使用 StartForeground 创建的通知?
【发布时间】:2017-05-15 00:16:06
【问题描述】:

我有一个需要作为前台服务运行的服务。 为此,必须将通知链接到它。

服务启动时,我将 OnGoing 设置为 true,当用户与 Notification 交互停止服务时,我想将当前的 Notification 更改为 OnGoing(false)。 (因此,如果需要,用户可以关闭通知) 我希望通知仍然可见,以便用户可以从通知中再次启动服务。

通常,我必须使用 NoficiationManager 来使用特定 ID 进行通知,然后再对其进行修改。 但是,在调用 StartForeground(flags, notification) 时,没有设置 ID 的选项。

如何将 OnGoing(true) 通知(从 StartForeground 调用开始)修改为 OnGoing(false)?

【问题讨论】:

    标签: android service notifications foreground


    【解决方案1】:

    我认为您的想法是正确的:您必须使用一个 ID,然后使用该 ID 更新通知。

    startForeground 方法的第一个字段不是标志而是 ID。这里是签名:

    void startForeground (int id, Notification notification)

    文档here

    因此您可以使用相同的 ID 更新通知。

    mNotificationManager.notify(sNotificationId, notification);
    

    【讨论】:

    • 谢谢,我实际上是在使用 Xamarin for Android,在他们的示例中,第一个参数是一个标志“StartForeground ((int)NotificationFlags.ForegroundService”,这显然是错误的。实际上,我还必须调用 StopForeground 才能让通知消失。
    猜你喜欢
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多