终止应用程序后不会触发AlarmManager通知

时间:2020-10-07 07:09:24

标签: android service alarmmanager localnotification

需要将通知显示为特定日期和时间的剩余部分。要设置余数,我正在使用AlarmManger设置任务,并使用广播接收器来显示通知。但是在终止应用程序后,通知剩余部分不会触发。

要使用此代码设置日期和时间

 Intent notificationIntent = new Intent(this, AddAppointmentAlarmReceiver.class);
        notificationIntent.putExtra(NOTIFICATION,addAppointDataBinding.edTitle.getText().toString());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        calendar.set(Calendar.HOUR,7);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else {

            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }

服务等级

    public class AlarmIntentService extends Service {
    private static final int NOTIFICATION_ID = 3;
    Context context;
    public AlarmIntentService(Context applicationContext) {
        super();
        this.context = applicationContext;
    }

    public AlarmIntentService() {
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);


        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel(Constants.NOTIFICATION_ID,intent.getStringExtra(NOTIFICATION), NotificationManager.IMPORTANCE_HIGH);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            managerCompat.createNotificationChannel(channel);
        }
        Intent notifyIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //to be able to launch your activity from the notification
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle(NOTIFICATION_TITLE);
        builder.setContentText(intent.getStringExtra(NOTIFICATION));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);

        builder.setSound(alarmSound);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(Constants.NOTIFICATION_ID);
        }
        Notification notificationCompat = builder.build();
        managerCompat.notify(NOTIFICATION_ID, notificationCompat);

        return START_STICKY;
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

广播接收器类

public class AddAppointmentAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        AlarmWakeLock.acquire(context);

        Intent intent1 = new Intent(context, AlarmIntentService.class);
        intent1.putExtra(NOTIFICATION,intent.getStringExtra(NOTIFICATION));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent1);
        }else {
            context.startService(intent1);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我记得警报有类似的问题,以及触发时它是如何工作的。请尝试下面的代码,看看是否有任何结果。

// Depending on the version of Android use different function for setting an Alarm.
// setAlarmClock() => Android < Marshmallow
// setExactAndAllowWhileIdle() => Android >= Marshmallow
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
            alarmManager.setAlarmClock(
                AlarmManager.AlarmClockInfo(alarmTimeAtUTC, pendingIntent),
                pendingIntent
            )
        } else {
            alarmManager.setExactAndAllowWhileIdle(
                AlarmManager.RTC_WAKEUP,
                alarmTimeAtUTC,
                pendingIntent
            )
        }

第1步:尝试不使用AlarmIntentService:

在您的setAlarm函数中,执行以下操作:

...
Intent notificationIntent = new Intent(this, AddAppointmentAlarmReceiver.class);
notificationIntent.setAction = "AppointmentBroadcastReceiverAction"
...

然后,在您的广播接收器中:

public class AddAppointmentAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.action == "AppointmentBroadcastReceiverAction") {
            sendNotification()
        }
    }
}

第2步:建立最大优先级通知:[Kotlin中的示例代码;适应Java]

private fun sendInternetDaysLeftNotification(context: Context) {
        val channelId: String = context.resources.getString(R.string.internetDaysLeftNotificationsChannelId)
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(%YOUR TITLE%)
            .setContentText(%YOUR TEXT%)
            .setAutoCancel(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setPriority(NotificationCompat.PRIORITY_MAX)

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId, DEFAULT_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(channel)
        }
// Random.nextInt() can be replaced with any Integer.
            notificationManager.notify(Random.nextInt(0, 100), notificationBuilder.build())
        }