NotificationChannel issue in Android O

后端 未结 2 1875
无人及你
无人及你 2020-12-14 00:06

I am getting a toast saying \"Developer warning for package com.google.android.apps.messaging\" when sending an MMS using Android Messages ver 2.3.063.

In logs

相关标签:
2条回答
  • 2020-12-14 00:39

    Messages on Toast and Logcat talk about you should be paid attention to 2 items and them order:

    1. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
    2. builder = new NotificationCompat.Builder(this, 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 the example in On Android 8.1 API 27 notification does not display.

    0 讨论(0)
  • 2020-12-14 01:00

    As it is written in Android Documentation:

    https://developer.android.com/preview/features/notification-channels.html

    If you target Android O and post a notification without specifying a valid notifications channel, the notification fails to post and the system logs an error.

    To solve this issue you need to create a NotificationChannel.

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    // The id of the channel.
    String id = "my_channel_01";
    
    // The user-visible name of the channel.
    CharSequence name = getString(R.string.channel_name);
    
    // The user-visible description of the channel.
    String description = getString(R.string.channel_description);
    
    int importance = NotificationManager.IMPORTANCE_LOW;
    
    NotificationChannel mChannel = new NotificationChannel(id, name,importance);
    
    // Configure the notification channel.
    mChannel.setDescription(description);
    
    mChannel.enableLights(true);
    // Sets the notification light color for notifications posted to this
    // channel, if the device supports this feature.
    mChannel.setLightColor(Color.RED);
    
    mChannel.enableVibration(true);
    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    
    mNotificationManager.createNotificationChannel(mChannel);
    

    And then assign it to your Notification like this:

    mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    
    // Sets an ID for the notification, so it can be updated.
    int notifyID = 1;
    
    // The id of the channel.
    String CHANNEL_ID = "my_channel_01";
    
    // Create a notification and set the notification channel.
    Notification notification = new Notification.Builder(MainActivity.this)
        .setContentTitle("New Message")
        .setContentText("You've received new messages.")
        .setSmallIcon(R.drawable.ic_notify_status)
        .setChannelId(CHANNEL_ID)
        .build();
    
    // Issue the notification.
    mNotificationManager.notify(id, notification);
    

    Update:

    In case you want to use NotificationCompat here is a simple example:

    NotificationCompat.Builder mBuilder;
    mBuilder = new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.mipmap.ic_launcher_icon)
        .setContentTitle("Title")
        .setContentText("Text")
        .setOngoing(true)
        .setChannelId(id);
    

    In fact you have to use Notification Builder in order to set channel id via setChannelId();

    0 讨论(0)
提交回复
热议问题