Android-通知不会出现在状态栏中
启动Android-通知不会出现在状态栏中,android,notifications,statusbar,intentservice,Android,Notifications,Statusbar,Intentservice,启动IntentService在后台上载文件时,我希望通过在我的服务中调用showNotification()向用户显示正在上载的通知: private void showNotification() { Notification notification = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_cloud_upload_black_24dp) .set
IntentService
在后台上载文件时,我希望通过在我的服务中调用showNotification()
向用户显示正在上载的通知:
private void showNotification() {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.setWhen(System.currentTimeMillis())
.setContentTitle("Uploading")
.setContentText("Your upload is in progress.")
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
现在我的问题是:当屏幕解锁时,通知会出现在锁定屏幕上,但不会出现在状态栏中。我错过了什么
部署目标是API级别24,因此缺少通知通道不应该是原因。试试:
PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
android.app.Notification pp = new NotificationCompat.Builder(getmContext())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText(getMessage())
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, pp);
这是我的代码,与targetSDK
25
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
其中通知\u ID
public static final int NOTIFICATION_ID = 100;
我觉得自己好笨。原来通知一直显示,但在黑色背景上有一个黑色图标
将xml中的图标颜色从更改为
android:fillColor=“#FF000000”
到
android:fillColor=“#FFFFFFFF”
它的工作原理就像在api level>26中添加了一个charm通知通道,对于oreo和更高版本,这意味着您的代码正在锁屏中显示通知,您正在oreo设备上测试吗?我添加了答案,请尝试并让我知道:)谢谢,从这里开始,我可以一步一步地找出错误你是说用答案吗?