android 中未显示通知
Notifications not showing in android
我有多个通知,但如果时间到了,其他通知不会显示,任何想法我该如何解决这个问题每个通知都有独特的数据尝试过这个但没有工作Multiple Notifications not showing in android
//Schedule alarm notification
private void scheduleNotification(Notification notification, long delay) {
Intent notificationIntent = new Intent(getContext(), MyNotificationPublisher.class);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
notificationIntent.putExtra(MyNotificationPublisher.NOTIF_CONTENT, title);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pendingIntent);
Log.d(TAG, "scheduleNotification: Notification set successfully!");
}
//Build notification
private Notification getNotification(String content) {
//on notification click open MainActivity
Intent intent = new Intent(getContext(), SchedulerFragment.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), default_notification_channel_id);
builder.setContentTitle("ToDo Reminder");
builder.setContentText(content);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_stat_name);
builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
return builder.build();
}
哦
通知发布者
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(SchedulerFragment.NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
}
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
String con = intent.getStringExtra(NOTIF_CONTENT);
assert notificationManager != null;
notificationManager.notify(id, notification);
你可以试试下面的代码,这个方法是用来生成通知的。如果不同的通知有不同的channelId
,可以同时产生多个通知。
private void initNotification(Context context,String channelId,String channelName,String title,String content){
Intent activityIntent = new Intent(context,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context,0,activityIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationChannel channel = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(context,channelId)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentIntent(pi)
.build();
manager.notify(Integer.parseInt(channelId),notification);
}
确保 String channelID
的值不同以生成多重通知。
我有多个通知,但如果时间到了,其他通知不会显示,任何想法我该如何解决这个问题每个通知都有独特的数据尝试过这个但没有工作Multiple Notifications not showing in android
//Schedule alarm notification
private void scheduleNotification(Notification notification, long delay) {
Intent notificationIntent = new Intent(getContext(), MyNotificationPublisher.class);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
notificationIntent.putExtra(MyNotificationPublisher.NOTIF_CONTENT, title);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pendingIntent);
Log.d(TAG, "scheduleNotification: Notification set successfully!");
}
//Build notification
private Notification getNotification(String content) {
//on notification click open MainActivity
Intent intent = new Intent(getContext(), SchedulerFragment.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), default_notification_channel_id);
builder.setContentTitle("ToDo Reminder");
builder.setContentText(content);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_stat_name);
builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
return builder.build();
}
哦 通知发布者
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(SchedulerFragment.NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
}
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
String con = intent.getStringExtra(NOTIF_CONTENT);
assert notificationManager != null;
notificationManager.notify(id, notification);
你可以试试下面的代码,这个方法是用来生成通知的。如果不同的通知有不同的channelId
,可以同时产生多个通知。
private void initNotification(Context context,String channelId,String channelName,String title,String content){
Intent activityIntent = new Intent(context,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context,0,activityIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationChannel channel = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(context,channelId)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentIntent(pi)
.build();
manager.notify(Integer.parseInt(channelId),notification);
}
确保 String channelID
的值不同以生成多重通知。