Android 发送通知(Android 8.0 及以后)

本人在郭林老师第一行代码第二版阅读时照着书并没有得到想要的输出
查阅官方文档后在此列出发送通知的方法

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//首先搞一个channel,定义这个channel的名字 描述,也可以定义 id不过可以直接用字符串的形式把id和description传入
                    CharSequence name = "official notice";
                    String description = "official description";
                    //channel接收三个参数。
                    //第一个是 channel id
                    //第二个是channel name
                    //第三个是//importance
                    NotificationChannel channel = new NotificationChannel("channel_03", name, NotificationManager.IMPORTANCE_HIGH);
                    //这里把channel的description传入,也可以不要description
                    channel.setDescription(description);
                    // Register the channel with the system; you can't change the importance
                    // or other notification behaviors after this
                    //这一步也可以写成 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    NotificationManager notificationManager = getSystemService(NotificationManager.class);
                    //在这里我们已经定义了channel的所有参数,然后把channel在notificationManager里面进行创建
                                  notificationManager.createNotificationChannel(channel);
                                  //然后创建builder对象,利用builder。build()方法即可创建一个通知,在这个builder里面我们为通知设定属性
                    NotificationCompat.Builder builder = new NotificationCompat.Builder
                            (MainActivity.this, "channel_03")
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("My notification")
                            .setContentText("Much longer text that cannot fit one line...")
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText("Much longer text that cannot fit one line..."))
                            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
                            //然后创建一个notificationManagerCompat实例,
                    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(MainActivity.this);

                    // notificationId is a unique int for each notification that you must define
                    //然后就可以用notify方法进行通知了,第一个参数是notificationId,第二个参数是builder。build()方法创建的通知
                    notificationManagerCompat.notify(3, builder.build());
                }

当然,这是官方声明的创建方法,我们还有其他的创建方法,下面的是我在百度上看到的方法

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //创建通知渠道实例
                //并为它设置属性
                //通知渠道的ID,随便写
                String id = "channel_01";
                CharSequence name = getString(R.string.app_name);
                //用户可看到的通知描述
                String description = getString(R.string.app_name);
                //构建NotificationChannel实例
                NotificationChannel notificationChannel =
                        new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);
                //配置通知渠道的属性
                notificationChannel.setDescription(description);
                //设置通知出现时的闪光灯
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                //设置通知出现时的震动
                notificationChannel.enableVibration(true);
                notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 100});
                //在notificationManager中创建通知渠道
                manager.createNotificationChannel(notificationChannel);

                
                Notification notification = new NotificationCompat.Builder(MainActivity.this, id)
                        .setContentTitle("标题")
                        .setContentText("内容666666666666666666666666666")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .build();
                manager.notify(1, notification);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值