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

it2023-11-08  76

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