我的通知仅在应用程序打开时有效
My notification is only working when the app is opened
所以,我有一个应用程序,每小时检查一个文本文件是否有更改。如果发现更改,则需要发出推送通知。但是当我测试该应用程序一天时,它只在第一次运行。每隔一段时间,它只会在我打开应用程序时立即发生。我目前的代码如下:
主要activity:
创建时
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 以在您的警报处理程序中设置一个新警报。
所以,我有一个应用程序,每小时检查一个文本文件是否有更改。如果发现更改,则需要发出推送通知。但是当我测试该应用程序一天时,它只在第一次运行。每隔一段时间,它只会在我打开应用程序时立即发生。我目前的代码如下:
主要activity:
创建时
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 以在您的警报处理程序中设置一个新警报。