Android:启动前台将无法正确显示通知 [英] Android:Start foreground won't display notification properly

查看:65
本文介绍了Android:启动前台将无法正确显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我使用通知生成器来创建通知,但是当我将其传递给startForeground时,我设置时仅显示置顶栏文本,其他所有内容都变为默认值,即标题显示为正在运行,而我将其设置为"在线"

我在通知中使用setText和setInfo方法设置的任何内容.Builder不会显示,而是显示诸如触摸以获得更多信息或停止应用程序"之类的默认文本.

这是相关代码:

服务:

  private最终int NOTIFICATION_ID = 1;@Overridepublic int onStartCommand(Intent intent,int flags,int startId){Toast.makeText(this,"EDI:核心服务已启动",Toast.LENGTH_LONG).show();startForeground(NOTIFICATION_ID,CoreServiceNotification.getNotification(this,"EDI在线",在线",在线且正在运行","EDI刚开始"));返回super.onStartCommand(intent,flags,startId);} 

CoreServiceNotification:

 公共类CoreServiceNotification {公共静态通知getNotification(上下文上下文,字符串标题,字符串文本,字符串信息,字符串tickerText){Notification.Builder notificationBuilder =新的Notification.Builder(上下文);notificationBuilder.setContentTitle(title);notificationBuilder.setContentText(text);notificationBuilder.setContentInfo(info);notificationBuilder.setTicker(tickerText);notificationBuilder.setLights(0x00ffff00,1000,0);返回notificationBuilder.build();}} 

结果:

解决方案

我认为创建通知时必须先设置smallIcon.

  Notification.Builder notificationBuilder =新Notification.Builder(上下文);notificationBuilder.setSmallIcon(R.drawable.yourIcon);//然后设置其他人员... 

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

I am working on an application that uses a foreground service. for this purpose I am calling startForeground(id,Notification) from inside onStartCommand callback of the service.

I use a notification builder to create my notification but when I pass it to startForeground only the ticker text is displayed as i set it, everything else turns to default, i.e. the Title says " is running while i had it set to " is online"

Also anything that i had set using the setText and setInfo method in the notification.Builder does not show up instead default text like "touch for more information or to stop the application" shows in its place.

here's the relevant code:

Service :

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

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

    }

RESULT:

解决方案

I think it's necessary to set the smallIcon first when you create the notification.

Notification.Builder notificationBuilder= new Notification.Builder(context);
notificationBuilder.setSmallIcon(R.drawable.yourIcon);
// then set other staff...

Here is another simple example from Cousera Android class, Click here

这篇关于Android:启动前台将无法正确显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆