我正在开发一个使用前台服务的应用程序。为此,我从服务的 onStartCommand 回调中调用 startForeground(id,Notification)
。
我使用通知构建器创建我的通知,但当我将其传递给 startForeground 时,只有 ticker 文本显示为我设定的内容,其他所有内容都变成了默认值,即标题显示为“ is running”,而我本来想设置为“ is online”。
使用 setText 和 setInfo 方法在 notification.Builder 中设置的任何内容都不会显示出来,而是显示类似“触摸以获取更多信息或停止应用程序”这样的默认文本。
以下是相关的代码:
服务:
private final int NOTIFICATION_ID=1;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"EDI: Core service Started" , Toast.LENGTH_LONG).show();
startForeground(NOTIFICATION_ID, CoreServiceNotification.getNotification(this, "EDI is online", "Online","Online and running","EDI just started"));
return super.onStartCommand(intent, flags, startId);
}
核心服务通知:
public class CoreServiceNotification {
public static Notification getNotification(Context context,String title,String text,String info,String tickerText){
Notification.Builder notificationBuilder= new Notification.Builder(context);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(text);
notificationBuilder.setContentInfo(info);
notificationBuilder.setTicker(tickerText);
notificationBuilder.setLights(0x00ffff00, 1000, 0);
return notificationBuilder.build();
}
}
RESULT:
NotificationManager notificationManager;
对象,然后你需要添加像notificationManager.notify("1",notificationBuilder);
这样的代码。 - M DstartForeground()
之前调用notificationBuilder.setSmallIcon(R.drawable.ic_launcher)
,这对我解决了问题。 - jeremija