奥利奥通知栏圆形图标

时间:2018-01-10 08:03:37

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

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

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

见下图

Screenshot

2 个答案:

答案 0 :(得分:4)

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

screenshot 1

有些人已经通过overriding the Application class's getResources()方法修复了它。

另一种对我有用的方法是使用HTTP POST request将通知作为data 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上:

screenshot 2

答案 1 :(得分:-3)

  1. <meta-data>移除AndroidManifest.xml代码。
  2. 通知构建器.setSmallIcon(int icon, int level)接受图标作为强制参数&amp; level参数作为可选参数。
  3. 注: .setSmallIcon 接受drawables &amp; 不接受mipmap

    根据Android 8.0 Oreo Notification Channels&amp;行为改变:

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