On Android 8.1 API 27 notification does not display

后端 未结 4 1214
無奈伤痛
無奈伤痛 2020-11-27 03:51

I get Toast on Android 8.1 API 27:

Developer warning for package \"my_package_name\"
Failed to post notification on ...

L

4条回答
  •  鱼传尺愫
    2020-11-27 04:05

    Andy's answer is working however I wanted to avoid to deprecated Builder and follow the FireBase Quickstart Project. I just added code before notify from manager.

    String channelId = "default_channel_id";
    String channelDescription = "Default Channel";
    // Since android Oreo notification channel is needed.
    //Check if notification channel exists and if not create one
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
        if (notificationChannel == null) {
            int importance = NotificationManager.IMPORTANCE_HIGH; //Set the importance level
            notificationChannel = new NotificationChannel(channelId, channelDescription, importance);
            notificationChannel.setLightColor(Color.GREEN); //Set if it is necesssary
            notificationChannel.enableVibration(true); //Set if it is necesssary
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }
    
    //notificationManager.notify as usual
    

    Edit: They removed the channel exist check from example I am not sure why.

提交回复
热议问题