我目前正在开发一个支持android的库。
我被要求在前台服务启动时通知用户。通知必须包含"ApplicationName“作为标题,"ApplicationName正在运行”作为文本。通知图标必须与启动器图标相同。
目标API级别为26。
通知不起作用,因为之前的开发人员忘记打开通知chanel。现在这是固定的,我们有正确弹出的通知。标签和期望相符。
但是现在我想知道为什么通知包含预期的值。我在javadoc中找不到任何引用。下面的代码将将通知显示为期望将应用程序的名称作为标题,并显示文本"ApplicationName正在运行“:
@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 :添加表示创建通知通道的代码
发布于 2020-04-01 14:15:59
我发现了你的问题。这是代码的结果:

在添加小图标之后:
.setSmallIcon(R.mipmap.ic_launcher)

它工作得很好
https://stackoverflow.com/questions/60972538
复制相似问题