【问题标题】:Android notification white circle when app is not running [duplicate]应用程序未运行时的Android通知白圈[重复]
【发布时间】:2016-12-17 22:27:29
【问题描述】:

我正在为我的应用推送通知使用 Firebase 通知。 一切正常,但应用程序未运行时通知图标显示白色圆圈。 我的目标是 SDK 版本 23,我也在使用 Roman Nurik 的通知图标生成器来生成白色透明图标。

当应用程序处于前台并运行时,通知图标会正确显示。 img

但是当应用程序处于后台或被终止时,图标会被通用白色圆圈替换。 img

这是我的通知生成器方法:

private void sendNotification(String messageTitle, String messageBody) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notification_icon)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_DEFAULT)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notification.build());

}

【问题讨论】:

    标签: android android-layout firebase android-notifications firebase-notifications


    【解决方案1】:

    我已经找到了类似问题的答案:Notification Icon with the new Firebase Cloud Messaging system

    很遗憾,这是 SDK 中 Firebase 通知的限制 9.0.0。当应用程序在后台时,启动器图标从清单(带有必要的 Android 着色)中用于发送消息 从控制台。

    如果应用程序在前台(或发送数据消息),您应该 能够使用您自己的逻辑进行自定义,并且您应该能够 如果从 HTTP/XMPP API 发送消息,请自定义图标。 现在从控制台发送的消息你会得到这个 行为。

    似乎避免此 Firebase 通知错误的最佳方法是将 build.gradle 中的 targetSdkVersion 更改为 19。然后通知图标将被着色。 Firebase 会在一段时间后解决此问题。

    希望对你有帮助

    【讨论】:

    • 感谢您的回复。所以这意味着只有从 Firebase 控制台发送测试通知时才会出现问题?如果我从我的真实后端服务器发送真实通知,那么就不会有这种问题了吗?
    • 是的,这正是它的意思。还尝试检查上面帖子中的其他解决方案,例如 AndroidManifest.xml 中的 标签中的 ...,但我很确定这是 Firebase 控制台错误
    【解决方案2】:

    试试这个代码:

     Intent intent = new Intent(this, TabActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(getNotificationIcon())
                .setContentTitle("IDS DMS Support")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    

    【讨论】:

      【解决方案3】:

      我认为您需要在应用程序中添加一个剪影图标,如果设备运行的是 Android Lollipop,则使用它。

      使用以下代码获取通知的小图标。

      protected int getNotificationIcon() {
              boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
              return useWhiteIcon ? R.drawable.ic_silhouette : R.drawable.ic_launcher;
          }
      

      你可以看到完整的解决方案Here

      我做了同样的事情并且工作正常。

      【讨论】:

        猜你喜欢
        • 2013-10-05
        • 2013-10-08
        • 1970-01-01
        • 2016-10-26
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多