【发布时间】:2019-10-08 07:30:55
【问题描述】:
通知在某些版本 8 的真实 android 设备和 9 即使启用了通知设置,但工作正常 模拟器。我正在安排警报管理器以触发通知 那个时候使用一个广播接收器,其中通知是 生成。
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);
}
}
在模拟器中它工作正常,我没有所有真实设备 测试和调试我卡在这一点上。任何事情都应该处理 android pie上的警报管理器?请推荐
【问题讨论】:
标签: android notifications version