首页 > 移动平台 > 详细

Android开发学习——使用通知

时间:2020-07-22 12:14:17      阅读:73      评论:0      收藏:0      [点我收藏+]
在按照书中的例子使用通知在设备上没有通知,查找资料后发现Android8后通知需要NotificationChannel,兼容 Android 8.0的通知如下:

               NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                Intent intent = new Intent(this,NotificationMain2Activity.class);
                PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);

                // 构建 Notification
                Notification.Builder builder = new Notification.Builder(this);
                builder.setContentTitle("ContentTitle")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentText("Content Text Here")
                        .setContentIntent(pi)
                        .setAutoCancel(true);

                // 兼容 Android 8.0
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                    String channelId = "ET_Notification_001";
                    String channelName = "ETtNotificationName";
                    // 第三个参数表示通知的重要程度
                    NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
                    // 册后除非卸载再安装否则不改变
                    notificationManager.createNotificationChannel(notificationChannel);
                    builder.setChannelId(channelId);
                }
                // 发出通知
                notificationManager.notify(1, builder.build());

Android开发学习——使用通知

原文:https://blog.51cto.com/470462/2512416

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!