android 8.0及以上创建Notification

2021-11-10  本文已影响0人  背锅TV丶伴奏大师

为了兼容4.1和8.0及以上notification,在此用最简单的实现功能:

private void sendNotifyMsg(Context context){
//两种创建方式
        //NotificationManager manager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationManagerCompat managerCompat=NotificationManagerCompat.from(context);
        String channelId="111";
        String channelName="cobe";
        int notifyId=1,requestCode=0;
        NotificationCompat.Builder builder=new NotificationCompat.Builder(context,channelId);
        //builder.setChannelId(channelId);
        builder.setSmallIcon(R.mipmap.ic_launcher);
    
        builder.setContentTitle("title");
        builder.setContentText("content");
        builder.setAutoCancel(true);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            //8.0以上必须配置NotificationChannel否则不显示,最后一个参数有5种,区别是通知方式不同。
            NotificationChannel channel=new NotificationChannel(channelId,channelName,NotificationManager.IMPORTANCE_DEFAULT);
            managerCompat.createNotificationChannel(channel);
        }
        //设置进度条的:false,表示刻度,设置为true,表示流动
        //builder.setProgress(100, progress, false);
        Intent resultIntent=new Intent(context, TeamMessageActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        resultIntent.putExtra("key","value");
        //最后一个参数是对点击通知栏时不同的反应和取值
        PendingIntent pendingIntent=PendingIntent.getActivity(context,requestCode,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        //manager.notify(1,builder.build());
        managerCompat.notify(notifyId,builder.build());
    }

封装成类:

public class NotificationHelper {
    private Context context;
    private NotificationManagerCompat managerCompat;
    private Notification notification;
    private static final int NOTIFY_ID = NotificationHelper.class.hashCode();
    private  String channelId="111",channelName="cobe";
    public NotificationHelper(Context context) {
        this.context = context;
        managerCompat=NotificationManagerCompat.from(context);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            //8.0以上必须配置NotificationChannel,最后一个参数有5种,区别是通知方式不同。
            NotificationChannel channel=new NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_DEFAULT);
            managerCompat.createNotificationChannel(channel);
        }
    }
/**
 @param message根据业务情况来写
**/
    public void activeCallingNotification(String message) {
        if (managerCompat != null) {
            buildCallingNotification(message);
            managerCompat.notify(NOTIFY_ID, notification);
        }
    }
    public void cancelNotification(){
        managerCompat.cancel(NOTIFY_ID);
    }
    private void buildCallingNotification(String message) {
        if (notification == null) {
            Intent localIntent = new Intent();
            localIntent.setClass(context, TeamMessageActivity.class);
            localIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            String to=String.valueOf(dto.getTo());
            localIntent.putExtra("key","value");
            int iconId = R.mipmap.ic_launcher;
            String title="title";
            String content="content";
            PendingIntent pendingIntent = PendingIntent.getActivity(context, NOTIFY_ID, localIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            notification = makeNotification(pendingIntent,title,content,iconId);
        }
    }
    private Notification makeNotification(PendingIntent pendingIntent, String title,String content,int iconId) {
        NotificationCompat.Builder builder=new NotificationCompat.Builder(context,channelId);
        builder.setSmallIcon(iconId).setContentTitle(title).setContentText(content).setAutoCancel(true).setContentIntent(pendingIntent);
        return builder.build();
    }
}

详细的用法和解释可以查看这位大佬写的文章:https://www.jianshu.com/p/cb8426620e74

上一篇下一篇

猜你喜欢

热点阅读