Android:启动前台将无法正常显示通知

All*_*ane 14 java notifications android

我正在使用一个使用前台服务的应用程序.为此我startForeground(id,Notification)从内部onStartCommand调用该服务的回调.

我使用通知构建器来创建我的通知但是当我将它传递给startForeground时,只有在我设置它时显示自动收报机文本,其他一切都变为默认,即标题说"正在运行,而我将它设置为"在线"

我在notification.Builder中使用setText和setInfo方法设置的任何内容都不显示默认文本,例如"触摸更多信息或停止应用程序"显示在其位置.

这是相关的代码:

服务:

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);
    }
Run Code Online (Sandbox Code Playgroud)

CoreServiceNotification:

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();
        }

    }
Run Code Online (Sandbox Code Playgroud)

结果: 在此输入图像描述

taf*_*ula 28

我认为在创建通知时首先需要设置smallIcon.

Notification.Builder notificationBuilder= new Notification.Builder(context);
notificationBuilder.setSmallIcon(R.drawable.yourIcon);
// then set other staff...
Run Code Online (Sandbox Code Playgroud)

这是Cousera Android类的另一个简单示例, 点击此处

  • 这个答案应该标记为已接受. (2认同)