首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有任何安卓通知setContentTitle和setContextText魔术值吗?

有任何安卓通知setContentTitle和setContextText魔术值吗?
EN

Stack Overflow用户
提问于 2020-04-01 13:56:00
回答 1查看 84关注 0票数 1

我目前正在开发一个支持android的库。

我被要求在前台服务启动时通知用户。通知必须包含"ApplicationName“作为标题,"ApplicationName正在运行”作为文本。通知图标必须与启动器图标相同。

目标API级别为26。

通知不起作用,因为之前的开发人员忘记打开通知chanel。现在这是固定的,我们有正确弹出的通知。标签和期望相符。

但是现在我想知道为什么通知包含预期的值。我在javadoc中找不到任何引用。下面的代码将将通知显示为期望将应用程序的名称作为标题,并显示文本"ApplicationName正在运行“:

代码语言:javascript
复制
@Override
public void onCreate() {
    NotificationChannel channel = new NotificationChannel("APPLICATION_CHANNEL", "MyService", NotificationManager.IMPORTANCE_LOW);
    channel.setDescription(notificationChannelText);
    //block below useful for head up notification
    channel.setSound(null, null);
    channel.setShowBadge(false);
    channel.enableLights(false);
    channel.enableVibration(false);

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
}
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    foregroundNotification();
    return Service.START_STICKY;
}
/**
 * In order to start foreground service we and generate a service running notification.
 */
private void foregroundNotification() {
    Context context = getApplicationContext();
    Intent notificationIntent = new Intent(context, getClass());
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    Notification notification = new Notification.Builder(context, "APPLICATION_CHANNEL")
            .setContentTitle("Title")
            .setContentText("Subject")
            .setContentIntent(pendingIntent)
            .build();
    startForeground(42666, notification);
}

为什么不直接显示一个通知,以" title "作为标题,"Subject"作为内容?

有没有我们必须知道的常量或魔法值?

我们在哪里可以找到任何关于它的文档或定义?

编辑2020/04/01 :添加表示创建通知通道的代码

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-01 14:15:59

我发现了你的问题。这是代码的结果:

在添加小图标之后:

代码语言:javascript
复制
.setSmallIcon(R.mipmap.ic_launcher)

它工作得很好

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60972538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档