android 我的通知仅在应用程序打开时起作用

rryofs0p  于 2023-11-15  发布在  Android
关注(0)|答案(2)|浏览(146)

所以,我有一个应用程序,每小时检查一个文本文件的更改.如果它发现更改,它需要给给予推送通知.但当我测试了一天的应用程序,它只工作的第一次.每隔一段时间,它只发生在我打开应用程序时inmediatly.我的curent代码如下:
主要活动:
on create

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


NotificationHelper:

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

h5qlskok

h5qlskok1#

对于一个更现代的方法,你可能真的想使用Joblog而不是AlarmManager,但是任何一个都可以工作。
我看到的最重要的事情是你调用AlarmManager.setExact。这将安排闹钟发生一次,而且只发生一次。如果你想让它每小时发生一次,你要么需要调用setRepeating,要么你需要再次调用setExact来在你的闹钟处理程序中设置一个新的闹钟。

dzjeubhm

dzjeubhm2#

在我的情况下,我解决了它添加一些权限:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

字符串

相关问题