【问题标题】:Oreo Notification Bar Round Icon奥利奥通知栏圆形图标
【发布时间】:2018-06-19 08:23:42
【问题描述】:

我想知道如何更改 android Oreo (API 26) 中的通知栏小图标。它在通知栏中显示圆形白色图标。我怎样才能改变它?清单文件默认通知图标设置如下。

<meta-data
     android:name="com.google.firebase.messaging.default_notification_icon"
     android:resource="@drawable/ic_status" />

见下图

【问题讨论】:

  • 这是通知栏截图的链接。 ibb.co/d4i2P6
  • 这个问题只发生在 8.0 上吗?您是否在任何其他 5+ 设备上检查过它?您的通知图标中是否有任何 alpha 值?
  • 是的,它只发生在 android oreo 设备中,低于 api 26 设备,图标显示正确。
  • 你能试试删除元数据吗
  • 如果元数据被删除,应用在后台或被杀时是否会显示通知图标?

标签: android android-notifications android-notification-bar android-statusbar


【解决方案1】:

有一个bug in Firebase SDK 11.8.0 on Android 8.0 (API 26),导致在状态栏中显示应用程序启动器图标的白色版本,而不是通知图标。

有人用overriding the Application class's getResources()的方法修复了。

对我有用的另一种方法是使用HTTP POST requestdata message 的形式发送通知:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
   "to": "/topics/test",
   "data": {
       "title": "Update",
       "body": "New feature available"
    }
 }

然后subclass FirebaseMessagingService 以编程方式显示通知:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent i = new Intent(context, HomeActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 
                                         PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder builder = 
            new NotificationCompat.Builder(this, GENERAL_CHANNEL_ID)
                 .setSmallIcon(R.drawable.ic_stat_chameleon)
                 .setContentTitle(remoteMessage.getData().get("title"))
                 .setContentText(remoteMessage.getData().get("body"))
                 .setContentIntent(pi);

        NotificationManager manager = 
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
}

最后,我得到了这个,即使是在 Android 8.0 上:

【讨论】:

    【解决方案2】:
    1. AndroidManifest.xml 中删除&lt;meta-data&gt; 标签。
    2. Notification Builder 有 .setSmallIcon(int icon, int level),它接受 icon 作为强制参数和 level 参数作为可选参数。

    注意:.setSmallIcon接受drawables & 不接受mipmaps

    根据Android 8.0 Oreo Notification Channels & 行为变化应该是这样的:

    public class MainActivity extends AppCompatActivity {
    
        private CharSequence name;
        private int notifyId;
        private int importance;
        private String id;
        private String description;
    
        private Notification mNotification;
        private NotificationManager mNotificationManager;
        private NotificationChannel mChannel;
        private PendingIntent mPendingIntent;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
            binding.btnNotification.setOnClickListener(v -> notification());
        }
    
        private void notification() {
    
            mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notifyId = 1;
            description = "Hello World, welcome to Android Oreo!";
    
            Intent intent = new Intent(this, MainActivity.class);
            mPendingIntent = PendingIntent.getActivity(this, notifyId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            if (SDK_INT >= O) {
                id = "id";
                name = "a";
                importance = NotificationManager.IMPORTANCE_HIGH;
    
                mChannel = new NotificationChannel(id, name, importance);
                mChannel.setDescription(description);
                mChannel.enableLights(true);
                mChannel.setLightColor(Color.WHITE);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[] {100, 300, 200, 300});
                mNotificationManager.createNotificationChannel(mChannel);
    
                mNotification = new Notification.Builder(MainActivity.this, id)
                    .setContentTitle(id)
                    .setContentText(description)
                    .setContentIntent(mPendingIntent)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .build();
            } else {
                mNotification = new Notification.Builder(MainActivity.this)
                    .setContentTitle(id)
                    .setContentText(description)
                    .setContentIntent(mPendingIntent)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setLights(Color.WHITE, Color.RED, Color.GREEN)
                    .setVibrate(new long[] {100, 300, 200, 300})
                    .build();
            }
                mNotificationManager.notify(notifyId, mNotification);
        }
    }
    

    【讨论】:

    • 据我所知,当应用程序在 bg 中或被杀死时,fcm onmessagereceived 没有调用。因此,我在清单中设置了默认图标。当应用程序处于后台或被杀死时,它会以这种方法工作吗?
    • 是的,它可以工作,因为对象mNotification 是通过NotificationManager 通知并设置为通知通道的。
    • 这里有点混乱,我想这样改变的活动是什么?是我的 mainativity 类还是 firebasemessagingservice 类?
    • 您可以将notification() 方法放在您想要触发它的任何位置。我将它放在一个活动中作为示例,当我单击相关按钮时会触发通知。您可以通过服务或活动触发通知。
    • 我已将它放在 firebasemessagingservice 类中,并在 onmessagereceived 方法中调用它。当应用程序在前台时它正在工作,但当应用程序在后台或被杀死时它不工作。