NotificationCompat with API 26

前端 未结 5 569
滥情空心
滥情空心 2020-11-27 03:38

I dont see any information about how to use NotificationCompat with Android O\'s Notification Channels

I do see a new Constructor that takes a cha

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 04:06

    Create the NotificationChannel only if API >= 26

    public void initChannels(Context context) {
        if (Build.VERSION.SDK_INT < 26) {
            return;
        }
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel("default",
                                                              "Channel name",
                                                              NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("Channel description");
        notificationManager.createNotificationChannel(channel);
    }
    

    And then just use:

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default");
    

    So your notifications are working with both API 26 (with channel) and below (without).

提交回复
热议问题