我正在构建一个应用程序,该应用程序应该提醒用户他们设置的即将发生的事件(基本上是提醒)。我遇到的问题是,在一段时间内(约15分钟或更长时间)未使用该应用程序时,将通知推送到用户的电话(仅在API 26+上);通知根本不显示。
我仔细阅读了此内容,并意识到App Standby和Doze模式可能会阻止我的应用推送此类通知;用户将在运行API 25及以下版本的手机上收到我的通知。为了解决这个问题,我尝试使用AlarmManager.setExactAndAllowWhileIdle(),但问题仍然存在。
class TaskNotifications {
private AlarmManager alarmManager;
private Context c;
TaskNotifications(Context context) {
this.c = context;
this.alarmManager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
}
void setReminder(Context context, Task task) {
if (VERSION.SDK_INT < Build.VERSION_CODES.O) {
long reminderMilliseconds = task.getReminderMilliseconds();
if (reminderMilliseconds > Calendar.getInstance().getTimeInMillis() && !task.isDone()) {
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra("ID", task.getID());
intent.putExtra("TITLE", task.getTitle());
intent.putExtra("DETAILS", task.getDetails());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, task.getID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (VERSION.SDK_INT >= 23) {
this.alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderMilliseconds, pendingIntent);
} else
this.alarmManager.setExact(AlarmManager.RTC_WAKEUP, reminderMilliseconds, pendingIntent);
}
}
}
void cancelReminder(Task task) {
if (VERSION.SDK_INT < Build.VERSION_CODES.O) {
this.alarmManager.cancel(PendingIntent.getBroadcast(this.c, task.getID(),
new Intent(this.c, NotificationReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT));
}
}
}
public class NotificationReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent startIntent = new Intent(context, NotificationJobIntentService.class);
startIntent.putExtra("ID", intent.getIntExtra("ID", -1));
startIntent.putExtra("TITLE", intent.getStringExtra("TITLE"));
startIntent.putExtra("DETAILS", intent.getStringExtra("DETAILS"));
JobIntentService.enqueueWork(context, NotificationJobIntentService.class, intent.getIntExtra("ID", -1), startIntent);
}
}
public class NotificationJobIntentService extends JobIntentService {
private String CHANNEL_ID = getResources().getString(R.string.channel_name);
protected void onHandleWork(@NonNull Intent intent) {
createNotificationChannel(NotificationJobIntentService.this);
int NOTIFICATION_ID = intent.getIntExtra("ID", -1);
String GROUP = "NOTIFICATION_GROUP";
String title = intent.getStringExtra("TITLE");
if (title.isEmpty())
title = getResources().getString(R.string.no_title);
String details = intent.getStringExtra("DETAILS");
if (details.isEmpty())
details = getResources().getString(R.string.no_details);
Intent openAppIntent = new Intent(NotificationJobIntentService.this, MainActivity.class);
TaskStackBuilder create = TaskStackBuilder.create(this);
create.addNextIntentWithParentStack(openAppIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationJobIntentService.this, this.CHANNEL_ID)
.setContentTitle(title)
.setContentText(details)
.setSmallIcon(R.drawable.baseline_alarm_black_18)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(create.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT))
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setGroup(GROUP)
.setAutoCancel(true)
.setColor(Color.argb(100, 0, 87, 75))
.setVibrate(new long[]{1000, 1000})
.setLights(Color.GREEN, PathInterpolatorCompat.MAX_NUM_POINTS, PathInterpolatorCompat.MAX_NUM_POINTS)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, builder.build());
}
private void createNotificationChannel(Context context) {
if (VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence string = context.getString(R.string.channel_name);
String description = context.getString(R.string.channel_description);
NotificationChannel notificationChannel = new NotificationChannel(this.CHANNEL_ID, string, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(description);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{1000, 1000});
(context.getSystemService(NotificationManager.class)).createNotificationChannel(notificationChannel);
}
}
}
我是否可以通过可靠的方式向运行API 26+的用户手机发送确切/确切的通知?还是我的代码中有一个我没有注意到的错误?
答案 0 :(得分:0)
我无法使通知系统在API 26+设备上运行,但是,我使用Android日历提供程序提醒将事件添加到用户日历中,然后通过默认日历设置提醒...不是我所需要的原本想要的,但这是一个创可贴的解决方案。
如果任何人仍然可以按预期解决问题,请告诉我。
以下代码:
kfold = StratifiedKFold(n_splits=folds, shuffle=True, random_state=random_state)