终止应用程序后未触发 AlarmManager 通知
Posted
技术标签:
【中文标题】终止应用程序后未触发 AlarmManager 通知【英文标题】:AlarmManager notification not triggering after killing application 【发布时间】:2020-10-07 07:09:24 【问题描述】:需要在特定日期和时间将通知显示为剩余时间。要设置余数,我使用 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】:我记得在警报方面遇到过类似的问题,以及在触发时它是如何工作的。请尝试下面的一段代码,看看是否有任何结果。
// 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())
【讨论】:
感谢您的快速回复。但它在应用程序被杀死后也不起作用。 @priyankaMore 您是否将广播接收器添加到 AndroidManifest? 是的,就是这样。AddAppointmentAlarmReceiver
onReceive 函数中显示通知。我将编辑我的帖子,以便您了解我的意思。
我按照步骤尝试了您的答案。面临同样的问题。以上是关于终止应用程序后未触发 AlarmManager 通知的主要内容,如果未能解决你的问题,请参考以下文章