设为首页 加入收藏

TOP

Android O的通知渠道适配
2019-08-24 00:11:12 】 浏览:28
Tags:Android 通知 渠道 适配
 在 Android O 以后,Google引入了通知通道的概念,如果目标API大于 Android O ,不直指定通知渠道是不能发送通知的。

这里放一个我写好的通知方法,大家可以适当的改改再用,*当然亦可以直接用*

 
    /**
     * 通过通知渠道发送通知 Android O 新增API
     * 其他的还和以前一样
     *
     * @param channelID 渠道ID
     * @param channelName 渠道名字
     * @param subText 小标题
     * @param title 大标题
     * @param text 内容
     */
    @TargetApi(Build.VERSION_CODES.O)
    public void sendNotification(String channelID, String channelName, String subText, String title, String text) {

        //创建通道管理器
        NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);

        NotificationManager manager;
        manager = (NotificationManager) this.getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);

        //构建通知
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        //设置小图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置通知 标题,内容,小标题
        builder.setContentTitle(title);
        builder.setContentText(text);
        builder.setSubText(subText);
        //设置通知颜色
        builder.setColor(Color.parseColor("#E91E63"));
        //设置创建时间
        builder.setWhen(System.currentTimeMillis());
        //创建通知时指定channelID
        builder.setChannelId(channelID);

        Intent resultIntent = new Intent(this, ClipActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        //构建一个通知,最后通知管理器 发送
        Notification notification = builder.build();
        manager.notify(1, notification);
    }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇从小白到大神程序员必读15本经典.. 下一篇安卓投屏助手(B1425)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目