关闭应用程序时不会触发AlarmManager

编程入门 行业动态 更新时间:2024-10-25 14:34:07
本文介绍了关闭应用程序时不会触发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="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

本文发布于:2023-11-27 01:36:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!