NotificationCompat with API 26

前端 未结 5 536
滥情空心
滥情空心 2020-11-27 03:38

I dont see any information about how to use NotificationCompat with Android O\'s Notification Channels

I do see a new Constructor that takes a cha

5条回答
  •  一整个雨季
    2020-11-27 03:48

    Declare Notification Manager:

       final NotificationManager mNotific=            
       (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    
        CharSequence name="Ragav";
        String desc="this is notific";
        int imp=NotificationManager.IMPORTANCE_HIGH;
        final String ChannelID="my_channel_01";
    

    Notification Channel

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        {
          NotificationChannel mChannel = new NotificationChannel(ChannelID, name, 
         imp);
                mChannel.setDescription(desc);
                mChannel.setLightColor(Color.CYAN);
                mChannel.canShowBadge();
                mChannel.setShowBadge(true);
                mNotific.createNotificationChannel(mChannel);
            }
    
        final int ncode=101;
    
        String Body="This is testing notific";
    

    Notification Builder

            Notification n= new Notification.Builder(this,ChannelID)
                    .setContentTitle(getPackageName())
                    .setContentText(Body)
                    .setBadgeIconType(R.mipmap.ic_launcher)
                    .setNumber(5)
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setAutoCancel(true)
                    .build();
    

    NotificationManager notify to User:

                mNotific.notify(ncode, n);
    

提交回复
热议问题