解决通知栏8.0适配问题

8.0以上系统创建通知栏

@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String title, String message) {
    String CHANNEL_ID = "channel_01";
    String CHANNEL_NAME = "channel_name";
    //设置渠道
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
            NotificationManager.IMPORTANCE_HIGH);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);

    //设置跳转的页面
    PendingIntent intent = PendingIntent.getActivity(this,
            100, new Intent(this, MainActivity.class),
            PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification.Builder(this, CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(message)
            .setContentIntent(intent)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_kuda_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_kuda_launcher))
            .setAutoCancel(true)
            .build();
    manager.notify(2, notification);
}

8.0以下系统创建通知栏

public void createNotification(String title, String message){
    //创建通知管理类
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    //设置跳转的页面
    PendingIntent intent = PendingIntent.getActivity(this,
            100, new Intent(this, MainActivity.class),
            PendingIntent.FLAG_CANCEL_CURRENT);

    //创建通知建设类
    Notification.Builder builder = new Notification.Builder(this)
            .setContentTitle(title)//设置通知栏标题
            .setContentText(message)//设置通知栏内容
            .setContentIntent(intent)//设置跳转
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_kuda_launcher)//设置图标
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_kuda_launcher))
            .setDefaults(Notification.DEFAULT_ALL)//设置
            .setAutoCancel(true);//设置自动消失
    //创建通知类
    Notification notification = builder.build();
    //显示在通知栏
    manager.notify(2, notification);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值