解决方案:
第一步:创建一个NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(channel_id , channel_name, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
这里,channel_id 和 channel_name 分别是int
和string
变量。
第2步:将其附加到NotificationManager
:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
第 3 步:创建您的通知:
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("Test Title")
.setContentText("Test Message")
.setSmallIcon(R.mipmap.ic_launcher);
Step4:在同一个NotificationManager
对象中附加通知
notificationManager.notify(1, notification.build());
最后,进行检查以通知它是否高于 Android O:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
....
}
参考和更多关于这个可以找到这里
希望能帮助到你。