首页 > 解决方案 > 我的通知仅在应用程序打开时有效

问题描述

所以,我有一个应用程序,它每小时检查一个文本文件的更改。如果发现更改,则需要发出推送通知。但是当我测试应用程序一天时,它只能在第一次运行。每隔一次,它只会在我打开应用程序时立即发生。我的当前代码如下:

主要活动:

在创建时

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);
    }
}

标签: javaandroidandroid-notifications

解决方案


对于更现代的方法,您可能真的想使用 JobScheduler 而不是 AlarmManager。但是任何一个都可以工作。

我看到的最重要的事情是你调用了AlarmManager.setExact。这将安排警报发生一次,而且只发生一次。如果您希望它每小时发生一次,则需要调用 setRepeating,或者需要再次调用 setExact 以在警报处理程序中设置新警报。


推荐阅读