On Android 8.1 API 27 notification does not display

后端 未结 4 1174
無奈伤痛
無奈伤痛 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:00

    I have set channel id, but notification still not shown.

    Finally i found my problem is had not invoke "setContentText()" method.

    It was really help me that @Andy Sander mentioned "required setters"!

    here are required setters for Notification on Android 8 Oreo API 26 and later:

    builder.setContentTitle() // required
    .setSmallIcon() // required
    .setContentText() // required
    .setChannelId(id) // required for deprecated in API level >= 26 constructor .Builder(this)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 04:06

    Also don't forget to bind your channel_id to your notification builder. After binding it, my problem gone.

    notificationBuilder.setChannelId(channelId)
    

    or

    NotificationCompat.Builder(Context context, String channelId)
    
    0 讨论(0)
  • 2020-11-27 04:11

    If you get this error should be paid attention to 2 items and them order:

    1. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
    2. builder = new NotificationCompat.Builder(context, id);

    Also NotificationManager notifManager and NotificationChannel mChannel are created only once.

    There are required setters for Notification:

    • builder.setContentTitle() // required
    • .setSmallIcon() // required
    • .setContentText() // required

    See example:

    private NotificationManager notifManager;
    public void createNotification(String aMessage, Context context) {
        final int NOTIFY_ID = 0; // ID of notification
        String id = context.getString(R.string.default_notification_channel_id); // default_channel_id
        String title = context.getString(R.string.default_notification_channel_title); // Default Channel
        Intent intent;
        PendingIntent pendingIntent;
        NotificationCompat.Builder builder;
        if (notifManager == null) {
            notifManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = notifManager.getNotificationChannel(id);
            if (mChannel == null) {
                mChannel = new NotificationChannel(id, title, importance);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                notifManager.createNotificationChannel(mChannel);
            }
            builder = new NotificationCompat.Builder(context, id);
            intent = new Intent(context, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            builder.setContentTitle(aMessage)                            // required
                   .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
                   .setContentText(context.getString(R.string.app_name)) // required
                   .setDefaults(Notification.DEFAULT_ALL)
                   .setAutoCancel(true)
                   .setContentIntent(pendingIntent)
                   .setTicker(aMessage)
                   .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        }
        else {
            builder = new NotificationCompat.Builder(context, id);
            intent = new Intent(context, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            builder.setContentTitle(aMessage)                            // required
                   .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
                   .setContentText(context.getString(R.string.app_name)) // required
                   .setDefaults(Notification.DEFAULT_ALL)
                   .setAutoCancel(true)
                   .setContentIntent(pendingIntent)
                   .setTicker(aMessage)
                   .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                   .setPriority(Notification.PRIORITY_HIGH);
        }
        Notification notification = builder.build();
        notifManager.notify(NOTIFY_ID, notification);
    }
    
    0 讨论(0)
提交回复
热议问题