【发布时间】:2018-06-01 13:27:40
【问题描述】:
我正在开发一个在 Marshmallow 中运行良好的提醒应用程序,但我刚刚购买了带有 Android Oreo 的 Xperia XZ1 紧凑型,通知被延迟甚至根本不显示。当提醒的时间过去了,手机什么也不做,但如果我解锁屏幕,通知就会突然出现。根据应用程序的日志,我的广播接收器工作并及时收到警报,然后成功生成通知。但是系统延迟显示通知,我不知道原因。我的应用程序已获得所有通知权限,配置为从省电模式中忽略等等。这是显示通知的代码。正如我所说,这段代码总是会及时执行,但通知的声音会随机延迟几分钟,或者直到我手动打开屏幕。
有没有办法解决这个问题?
private void makeNotification(Long rowId, String title, String body) {
android.app.NotificationManager mgr = (android.app.NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent;
long notificationId;
PendingIntent pi;
if (rowId == null) {
notificationId = 0;
notificationIntent = new Intent(context, ReminderListActivity.class);
pi = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
} else {
notificationId = rowId;
notificationIntent = new Intent(context, ReminderEditActivity.class);
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId);
long rowIdPrimitive = rowId;
pi = PendingIntent.getActivity(context, (int)rowIdPrimitive, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pi)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body));
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
// An issue could occur if user ever enters over 2,147,483,647 tasks. (Max int value).
// I highly doubt this will ever happen. But is good to note.
int id = (int)((long)notificationId);
mgr.notify(id, note);
NotificationManager.setNotified(context, id);
}
【问题讨论】:
标签: android notifications