java - 我的通知仅在应用程序打开时有效
问题描述
所以,我有一个应用程序,它每小时检查一个文本文件的更改。如果发现更改,则需要发出推送通知。但是当我测试应用程序一天时,它只能在第一次运行。每隔一次,它只会在我打开应用程序时立即发生。我的当前代码如下:
主要活动:
在创建时
notificationManager = NotificationManagerCompat.from(this);
// sets time for when to notify/ check for rescheduling.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 01);
calendar.set(Calendar.SECOND, 00);
setAlarm(calendar);
设置闹钟
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void setAlarm(Calendar calendar) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
报警接收器:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
notificationHelper.getManager().notify(1, nb.build());
}
}
通知助手:
public class NotificationHelper extends ContextWrapper {
public static final String channelID = "Chanel1";
public static final String channelName = "Probably change";
private NotificationManager mManager;
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationChannel channel1 = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
channel1.enableLights(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel1.setImportance(NotificationManager.IMPORTANCE_HIGH);
getManager().createNotificationChannel(channel1);
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannelNotification(){
return new NotificationCompat.Builder(getApplicationContext(), channelID)
.setContentTitle("Schedule change")
.setContentText("There is probably a change in schedule for your class.")
.setSmallIcon(R.drawable.ic_launcher_foreground);
}
}
解决方案
对于更现代的方法,您可能真的想使用 JobScheduler 而不是 AlarmManager。但是任何一个都可以工作。
我看到的最重要的事情是你调用了AlarmManager.setExact。这将安排警报发生一次,而且只发生一次。如果您希望它每小时发生一次,则需要调用 setRepeating,或者需要再次调用 setExact 以在警报处理程序中设置新警报。
推荐阅读
- javascript - Node.js 在多个 .js 文件中使用 socket.io
- regex - 正则表达式“and OR or”不重复表达
- java - 如何在不知道确切路径的情况下在 eclipse 项目目录中创建文件?
- jquery - 使用jquery打印json数组时如何添加换行符?
- android - 登录后如何始终保持有效的 FireBase 令牌
- swift - 有没有办法使用来自安全飞地的私钥来生成 TOTP?
- google-my-business-api - 获取帐户列表时出现 RATE_LIMIT_EXCEEDED 异常(Google My Business API)
- javascript - “431 请求标头字段太大”
- android - 使用 QR 码注册 EMM 期间不必要的设置向导
- python - 如何获得正确的权重?