我一直在尝试在android上处理通知,并且对于API 25来说是成功的,但是在API更高的版本上,我一直在阅读它需要一个Channel才能工作。我正在使用Firebase进行测试。
我已经尝试了教程中的以下代码:
int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String Description = "This is my channel";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = (android.support.v7.app.NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("title")
.setContentText("content");
Intent resultIntent = new Intent(this, mainPageShop.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(mainPageShop.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
如果缺少任何内容,或者是什么原因导致该通知无法在API 26以上的设备上显示,您是否可以引导我。
我一直在使用API 26在设备和仿真器上进行测试。 如果需要任何澄清,请告诉我。