Android 在OREO和更早版本中,在特定时间不会再次触发报警服务

Android 在OREO和更早版本中,在特定时间不会再次触发报警服务,android,push-notification,alarmmanager,Android,Push Notification,Alarmmanager,我正在开发一个android应用程序,在该应用程序中,我使用报警服务触发本地推送通知。第一次(早上6:00)它工作正常,即使我正在从堆栈中删除应用程序,但我在(早上6:00)再次更改系统时钟,它不会再次触发。下面给出了我的代码,其中我也根据API级别发出通知 // Activity where I am calling AlarmService public class MainActivity extends FragmentActivity { final sta

我正在开发一个android应用程序,在该应用程序中,我使用报警服务触发本地推送通知。第一次(早上6:00)它工作正常,即使我正在从堆栈中删除应用程序,但我在(早上6:00)再次更改系统时钟,它不会再次触发。下面给出了我的代码,其中我也根据API级别发出通知

// Activity where I am calling AlarmService

    public class MainActivity extends FragmentActivity {

        final static int RQS_1 = 4;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 6);
            calendar.set(Calendar.MINUTE,00);
            calendar.add(Calendar.SECOND, 00);

           // API LEVEL OREO or greater
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){

            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); //ALARM IS SET
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }else{
          // API LEVEL Lower than OREO
           AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            Intent notificationIntent = new     Intent("android.media.action.DISPLAY_NOTIFICATION");
            notificationIntent.addCategory("android.intent.category.DEFAULT");

            PendingIntent broadcast = PendingIntent.getBroadcast(this,100,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),broadcast);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),broadcast);
        }
    }
    }
报警接收器

public class AlarmReceiver extends BroadcastReceiver {

    int importance = NotificationManager.IMPORTANCE_HIGH;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(notificationIntent);
        localPushNotifications26GreaterAPI(context);
    }

    public void localPushNotifications26GreaterAPI(Context context){


        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// The id of the channel.
            String id = "my_channel_01";
            CharSequence name = "iam.peace";
            String description = "Description";

            NotificationChannel mChannel = new NotificationChannel(id, name,importance);
            mChannel.setDescription(description);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            mNotificationManager.createNotificationChannel(mChannel);
            mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            int notifyID = 1;
            String CHANNEL_ID = "my_channel_01";
            Notification notification = new Notification.Builder(context,CHANNEL_ID)
                    .setContentTitle("OREO LEVEL NOTIFICATION")
                    .setContentText("You've received new messages.")
                    .setSmallIcon(R.drawable.notification_icon)
                    .setChannelId(CHANNEL_ID)
                    .build();
            mNotificationManager.notify(notifyID, notification);
        } else{
            localPushNotifications26LowerAPI(context);
        }


    }


    public void localPushNotifications26LowerAPI(Context context){

        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder b = new NotificationCompat.Builder(context);

        b.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("I am Peace")
                .setContentTitle("OREO Below LEVEL NOTIFICATION")
                .setContentText("Oreo below level push notifications.")
                .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
                .setContentIntent(contentIntent)
                .setContentInfo("Info");


        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, b.build());
    }



}
我注册接收人的清单文件:

<receiver android:name="helper.AlarmReceiver">
            <intent-filter>
                <action android:name="android.media.action.DISPLAY_NOTIFICATION"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>

这是因为您使用的方法会发出一次性警报。您可以使用来发出重复报警(但由于API 19不再精确):

或者在onReceiveMethod中的AlarmReceiver中创建新的一次性报警

@Override
public void onReceive(Context context, Intent intent) {
    Intent notificationIntent = new Intent(context, NotificationActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(notificationIntent);
    localPushNotifications26GreaterAPI(context);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),broadcast);

}

从Android Oreo来看,如果你选择JobScheduler会更好,请参考谷歌的官方文件,

谢谢您的留言。我需要问一件事,什么是一次性警报?基本上,我需要报警,即使我的应用程序在后台或我从堆栈中删除了应用程序。我需要这种警报。这种警报只会触发一次,如果你想在AlarmReceiver中接收广播,你需要安排另一个alarmAlarms始终工作,即使你的应用程序被关闭。但是有4种不同的类型(经过的实时、经过的实时唤醒、RTC、RTC唤醒),并且您已经根据需要使用了正确的类型(RTC唤醒)。如果您想了解更多,请查看此项。
@Override
public void onReceive(Context context, Intent intent) {
    Intent notificationIntent = new Intent(context, NotificationActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(notificationIntent);
    localPushNotifications26GreaterAPI(context);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),broadcast);

}