计划通知不适用于某些 android 版本 8 和 9 的设备
Scheduling Notifications not working on some android devices of version 8 and 9
Notification not working on some real android devices of version 8 and
9 eventhough notification settings enabled, but working fine on
emulator. Im scheduling alarm manager to trigger the notification in
that time using a broadcast receiver in which notification is
generated.
private void generateNotification(Context context, String message) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel(notificationManager);
RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.notification_main);
view.setImageViewResource(R.id.ImageView1, R.drawable.tenkey_icon);
view.setTextViewText(R.id.title1, Utils.NOTIFICATION_TITLE);
view.setTextViewText(R.id.message1, message);
view.setTextViewText(R.id.time1, notificationTiming);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("content", message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification.Builder builder=new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, 0))
.setContent(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_ID);
}
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify((int)System.currentTimeMillis() % 1000000, notification);
}
private void createNotificationChannel(NotificationManager manager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Shankaram",
NotificationManager.IMPORTANCE_DEFAULT
);
manager.createNotificationChannel(serviceChannel);
}
}
As in emulator its working fine and i dont have all real devices to
test and debug im stuck at this point. Anything should be handled for
alarm manager on android pie? Please suggest
在 Android 8 及更高版本中,您必须使用 Notification channels
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear. By categorizing notifications into channels, users can disable specific notification channels for your app (instead of disabling all your notifications), and users can control the visual and auditory options for each channel—all from the Android system settings (figure 11). Users can also long-press a notification to change behaviors for the associated channel.
I have solved this problem by deeply checking all the notification
settings in phone and making all the settings enabled and finally
worked. No code change was made.
Notification not working on some real android devices of version 8 and 9 eventhough notification settings enabled, but working fine on emulator. Im scheduling alarm manager to trigger the notification in that time using a broadcast receiver in which notification is generated.
private void generateNotification(Context context, String message) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
createNotificationChannel(notificationManager);
RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.notification_main);
view.setImageViewResource(R.id.ImageView1, R.drawable.tenkey_icon);
view.setTextViewText(R.id.title1, Utils.NOTIFICATION_TITLE);
view.setTextViewText(R.id.message1, message);
view.setTextViewText(R.id.time1, notificationTiming);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("content", message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Notification.Builder builder=new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, 0))
.setContent(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_ID);
}
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify((int)System.currentTimeMillis() % 1000000, notification);
}
private void createNotificationChannel(NotificationManager manager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Shankaram",
NotificationManager.IMPORTANCE_DEFAULT
);
manager.createNotificationChannel(serviceChannel);
}
}
As in emulator its working fine and i dont have all real devices to test and debug im stuck at this point. Anything should be handled for alarm manager on android pie? Please suggest
在 Android 8 及更高版本中,您必须使用 Notification channels
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear. By categorizing notifications into channels, users can disable specific notification channels for your app (instead of disabling all your notifications), and users can control the visual and auditory options for each channel—all from the Android system settings (figure 11). Users can also long-press a notification to change behaviors for the associated channel.
I have solved this problem by deeply checking all the notification settings in phone and making all the settings enabled and finally worked. No code change was made.