Android-通知未出现在状态栏中

时间:2018-09-05 13:01:16

标签: android notifications statusbar intentservice

当启动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不应成为原因。

3 个答案:

答案 0 :(得分:0)

尝试:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

更改了xml中的图标颜色,

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

它就像一个魅力