Android O - Notification Channels and NotificationCompat

前端 未结 6 1365
忘了有多久
忘了有多久 2021-02-05 03:57

I cannot change this feeling: again, the Android developers came up with something new and leave everybody in the dark about how they would think the feature is used.

I

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-05 04:09

    It's not as expensive as you think! All you need to do is creating a notification channel and bind it to notification.

    You can solve this in two ways but for both of them you need to create a notification channel with a specific channel id.

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String id = "my_channel_01";
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel mChannel = new NotificationChannel(id, name,importance);
    mChannel.enableLights(true);
    mNotificationManager.createNotificationChannel(mChannel);
    

    First way is to set channel for notification in constructor:

    Notification notification = new Notification.Builder(MainActivity.this , id).setContentTitle("Title");
    mNotificationManager.notify("your_notification_id", notification);
    

    Second way is to set the channel by Notificiation.Builder.setChannelId()

    Notification notification = new Notification.Builder(MainActivity.this).setContentTitle("Title").
    setChannelId(id);
    mNotificationManager.notify("your_notification_id", notification);
    

    Hope this helps

提交回复
热议问题