【问题标题】:Android - notification does not appear in status barAndroid - 通知不会出现在状态栏中
【发布时间】:2019-02-10 15:19:58
【问题描述】:

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

【问题讨论】:

  • 通知通道添加在api级别>26,对于oreo及更高版本,意味着您的代码正在工作以在锁定屏幕中显示通知,您是否在oreo设备上测试?
  • 我已经添加了答案,请尝试并告诉我:)

标签: android notifications statusbar intentservice


【解决方案1】:

试试看:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 谢谢,从这里开始我可以一步一步找到错误
    • 你的意思是使用答案吗?
    【解决方案3】:

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

    更改了 xml 中的图标颜色

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

    它就像一个魅力

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多