我在为android 8.0及更高版本创建通知时遇到问题 此代码正确吗?请看一下频道创建。
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
NotificationChannel channel = new NotificationChannel("0", name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
res = getIntent().getStringExtra("Reminder");
Notification.Builder mBuilder = new Notification.Builder(ReminderfabActivity.this);
mBuilder.setSmallIcon(R.drawable.ic_assignment_late_black);
mBuilder.setContentTitle("Alert!");
mBuilder.setContentText(res);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pendingIntent).setAutoCancel(false);
notificationManager.notify(0, mBuilder.build());
}
答案 0 :(得分:0)
您应该使用NotificationCompat.Builder
而不是Notification.Builder
更改此行Notification.Builder mBuilder = new Notification.Builder(ReminderfabActivity.this);
到
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ReminderfabActivity.this,"0");
,并更改此行PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
您应该使用PendingIntent
中的标志之一,而不是intent