Android - 状态栏中不显示通知

Android - notification does not appear in status bar

当启动 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,因此缺少 NotificationChannel 不应该是原因。

试一试:

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

这是我的代码,可以很好地与目标 SDK 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);

其中 NOTIFICATION_ID

 public static final int NOTIFICATION_ID = 100;

我觉得很蠢。原来通知一直在显示,但是黑色背景上有一个黑色图标。

将 xml 中的图标颜色从

更改为

android:fillColor="#FF000000"android:fillColor="#FFFFFFFF"

它就像一个魅力