关闭应用程序时不会触发AlarmManager [英] AlarmManager is not triggered when App is closed
本文介绍了关闭应用程序时不会触发AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些应用内通知在特定时间显示给用户,但在应用关闭时什么也没有显示。
I have some in-App notification to show to users at a specific time but nothing is shown when the App is closed.
设置警报:
Intent alarmIntent = new Intent(mMotherActivity, ReminderAlarmManager.class);
if (ReminderNotificationType.CHANGE_LENS.equals(notificationType)) {
alarmIntent.putExtra("NOTIFICATION_TYPE", "REMINDER");
} else {
alarmIntent.putExtra("NOTIFICATION_TYPE", "ORDER");
}
long scTime = alarmDate.getTime();
PendingIntent pendingIntent = PendingIntent.getBroadcast(mMotherActivity, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) mMotherActivity.getSystemService(mMotherActivity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, scTime, pendingIntent);
广播接收器:
public class ReminderAlarmManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String notificationType = intent.getStringExtra("NOTIFICATION_TYPE");
if(notificationType.equalsIgnoreCase("ORDER"))
{
Intent i = new Intent(context, OrderReminderNotificationService.class);
context.startService(i);
}
else if(notificationType.equalsIgnoreCase("REMINDER"))
{
Intent i = new Intent(context, ReminderNotificationService.class);
context.startService(i);
}
}
}
因此,涉及到scTime ,即使该应用已关闭,我也想触发通知。因此,我通过PendingIntent调用服务,如下所示:
So when it comes to scTime, even if the App is closed, I would like to trigger a notification. So I'm calling a service by the PendingIntent, as follows:
public class OrderReminderNotificationService extends IntentService {
public OrderReminderNotificationService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Context context = getApplicationContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setContentTitle("Notification");
mBuilder.setContentText(context.getString(R.string.renewOrderNotificationMsg));
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setSound(uri);
mBuilder.setAutoCancel(true);
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(2, mBuilder.build());
}
清单中与以下内容有关的部分:
The part in the manifest concerning that:
<receiver android:name="com.company.utils.ReminderAlarmManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
但是什么都没有显示……我在做什么错?
But nothing shows up... What am I doing wrong ?
推荐答案
您的 AndroidManifest.xml
应该是这样的:
<receiver android:name=".ReminderAlarmManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".OrderReminderNotificationService "/>
这篇关于关闭应用程序时不会触发AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文