应用程序关闭时不触发AlarmManager [英] AlarmManager is not triggered when App is closed

查看:32
本文介绍了应用程序关闭时不触发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,即使App关闭了,我也想触发一个通知.所以我通过 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 应该是这样的:

Your AndroidManifest.xml should be like this:

<receiver android:name=".ReminderAlarmManager">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

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

这篇关于应用程序关闭时不触发AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆