Android O - Notification Channels and NotificationCompat

前端 未结 6 1473
忘了有多久
忘了有多久 2021-02-05 03:57

I cannot change this feeling: again, the Android developers came up with something new and leave everybody in the dark about how they would think the feature is used.

I

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-05 04:25

    Its pretty simple to work with NotificationChannnel.

    NotificationChannel actually group multiple notifications into channels. It basically gives more control of the notification behavior to the user. You can read more about Notification Channel and its implementation at Working with Notification Channel | With Example

    Creating Notification Channel

     // This is the Notification Channel ID. More about this in the next section
    public static final String NOTIFICATION_CHANNEL_ID="channel_id";
    
    //User visible Channel Name
    public static final String CHANNEL_NAME="Notification Channel";
    
    // Importance applicable to all the notifications in this Channel
    int importance=NotificationManager.IMPORTANCE_DEFAULT;
    
    //Notification channel should only be created for devices running Android 26
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
          NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, CHANNEL_NAME, importance);
    
          //Boolean value to set if lights are enabled for Notifications from this Channel
          notificationChannel.enableLights(true);
    
          //Boolean value to set if vibration is enabled for Notifications from this Channel
          notificationChannel.enableVibration(true);
    
          //Sets the color of Notification Light
          notificationChannel.setLightColor(Color.GREEN);
    
          //Set the vibration pattern for notifications. Pattern is in milliseconds with the format {delay,play,sleep,play,sleep...}
          notificationChannel.setVibrationPattern(new long[]{500,500,500,500,500});
    
          //Sets whether notifications from these Channel should be visible on Lockscreen or not
          notificationChannel.setLockscreenVisibility( 
     Notification.VISIBILITY_PUBLIC);
    }
    // Creating the Channel
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(notificationChannel);
    

    Now while creating the Notification just pass the Channel ID to the Notification Builder constructor as shown below

    //We pass the unique channel id as the second parameter in the constructor
    NotificationCompat.Builder notificationCompatBuilder=new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
    
    //Title for your notification
    notificationCompatBuilder.setContentTitle("This is title");
    
    //Subtext for your notification
    notificationCompatBuilder.setContentText("This is subtext");
    
    //Small Icon for your notificatiom
    notificationCompatBuilder.setSmallIcon(R.id.icon);
    
    //Large Icon for your notification 
    notificationCompatBuilder.setLargeIcon(  BitmapFactory.decodeResource(getResources(),R.id.icon));
    
    notificationManager.notify( NOTIFICATION_ID,notificationCompatBuilder.build());
    

    WIth this above notification becomes a part of the Notification Channel created in the first step and its behavior will now be with respect to the Channel Settings

提交回复
热议问题