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

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

问题描述

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

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

此外,我在通知中使用 setText 和 setInfo 方法设置的任何内容都不会显示,而是在其位置显示触摸以获取更多信息或停止应用程序"等默认文本.

这是相关的代码:

服务:

private final int NOTIFICATION_ID=1;@覆盖public int onStartCommand(意图意图,int标志,int startId){Toast.makeText(this,"EDI: Core service Started" , Toast.LENGTH_LONG).show();startForeground(NOTIFICATION_ID, CoreServiceNotification.getNotification(this, "EDI 在线", "在线","在线并运行","EDI 刚刚启动"));return super.onStartCommand(intent, flags, startId);}

核心服务通知:

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

结果:

解决方案

我觉得创建通知的时候有必要先设置smallIcon.

Notification.Builder notificationBuilder= new Notification.Builder(context);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天全站免登陆