设置永久服务的通知文本

Set notification text from permanent service

我有一项服务始终在后台 运行,并且有通知告知其当前状态。当您单击通知时,将打开一个 activity,允许您管理该服务。在我设置

之前完全有效
android:targetSdkVersion="23"

在清单中。

在此之前,我会用这个创建我的通知:

Notification myNotification = new Notification();
//several options

然后我会用这个更新我的通知文本:

myNotification.setLatestEventInfo

恐怕 setLatestEventInfo 被弃用了一段时间,似乎已经随着 sdk 23 消失了。很好。

然后我使用 Notification.Builder 创建通知。我正在尝试用这个更新文本:

notificationBuilder.setContentText(textToDisplay);

或这个

notificationBuilder.setStyle(new Notification.BigTextStyle().bigText(textToDisplay));

但没有任何效果。它只显示默认文本,如 "click here to display more information".

然后通知不是"started"和

notificationManager.notify(notificationId, myNotification);

但因为它来自

的服务
instance.startForeground(notificationId, myNotification);

问题 1:

文字不显示,只显示 "click for further information"

问题 2:

单击将从 OS 的设置菜单中打开应用程序信息 activity。它应该显示我选择的 activity。

通知文档表明 three fields are required: small icon, title, and detail text. Small icon seems particularly important. If it is missing, it appears the notification is judged "malformed" and the special handling described here 已应用。该处理包括在通知中显示通用标题,例如 "app-name is running",并在单击通知时启动应用的应用信息 activity。

您看到的行为是因为您在构建通知时没有设置小图标。您还应该为类别指定 Notification.CATEGORY_SERVICE,而不是 Context.ACTIVITY_SERVICE。