当应用关闭时,AlarmManager未被触发。

5

我有一些在特定时间向用户展示的应用内通知,但当应用程序关闭时没有任何内容显示。

设置闹钟:

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调用服务,如下所示:

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());
}

关于清单中的相关部分:
<receiver android:name="com.company.utils.ReminderAlarmManager">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

但是什么也没显示出来...我做错了什么吗?


你是从 BroadcastReceiver 调用服务吗? - Marat
我已经编辑了我的帖子并附上了完整的代码。我已经发布了BroadCastreceiver并完成了AlarmManager的设置。在BOOT_COMPLETED时,我是否应该在清单中做一些类似的事情? - Hubert Solecki
你的清单文件也是必需的。我的意思是,你在清单文件中注册了你的广播接收器和服务吗? - Marat
我刚刚注册了广播接收器,你可以看到(已编辑)。我应该同时注册这两个服务吗? - Hubert Solecki
是的,服务也需要存在。 - Marat
3个回答

7

由于需要节省一些电池电量,您的设备不允许您触发警报。 因此,转到 设置->电池/省电模式->应用程序,然后选择您的应用程序,之后选择“无限制”。


1
有没有办法通过应用程序重定向到这些设置,因为我想以编程方式实现它。 - Gaurav Roy

4
您的AndroidManifest.xml应该像这样:
<receiver android:name=".ReminderAlarmManager">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service android:name=".OrderReminderNotificationService "/>

3
还有一个问题。通过告知在BOOT_COMPLETED时执行接收器,我理解它将在设备启动时触发。但是,我想在经过传递给闹钟管理器的特定时间后触发闹钟,即使应用程序已关闭?它应该可以这样工作,对吗? - Hubert Solecki
当手机关闭时,所有的闹钟都会被删除。因此,我们有一个 BOOT_COMPLETED 操作,系统会将其发送到所有具有该操作的接收器中。这就是我们知道系统已经重新启动并且需要重置闹钟的方式。但是,如果您不关心重启后闹钟的删除,则不需要使用带有 BOOT_COMPLETED 的意图过滤器。即使应用程序已关闭,您的闹钟也将被触发。 - Marat
2021年,此代码已失效。如果应用程序关闭,AlarmManager将不会触发! - user1034912

2

闹钟管理器在某些手机上无法触发意图服务。我在Oneplus 3T上测试应用程序整整一天,直到我意识到代码没有问题。刚刚在Moto G上测试了同样的应用程序,它按预期工作。


2021年,这太荒谬了,Gmail通知工作得很好! - user1034912
2
@user1034912,这太荒谬了。是的,它在Gmail上运行良好的原因是因为它们已经被系统白名单授权以正常方式执行,但是当涉及到开发人员制作的应用程序时,这是不允许的,他们让我们苦苦寻找解决方法。 - Taki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接