Android O reporting notification not posted to channel - but it is

前端 未结 8 1376
礼貌的吻别
礼貌的吻别 2020-12-05 06:52

Couple Android O notification questions:

1) I have created a Notification Channel (see below), am calling the builder with .setChannelId() (passing in the name of th

8条回答
  •  眼角桃花
    2020-12-05 06:54

    You gotta create a channel before.

    private void createNotificationChannel() {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = getString(R.string.channel_name);
                String description = getString(R.string.channel_description);
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);
                // Register the channel with the system; you can't change the importance
                // or other notification behaviors after this
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
    }
    
    public void notifyThis(String title, String message) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.green_circle)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    
            // notificationId is a unique int for each notification that you must define
            notificationManager.notify(0, mBuilder.build());
    }
    

    Finally you call this method:

    createNotificationChannel();
    notifyThis("My notification", "Hello World!");
    

提交回复
热议问题