AlarmManager is not triggered when App is closed

南笙酒味 提交于 2019-12-21 06:29:17

问题


I have some in-App notification to show to users at a specific time but nothing is shown when the App is closed.

Setting alarm:

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

broadcast receiver:

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

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 ?


回答1:


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 "/>



回答2:


The alarm manager doesn't trigger the Intent services on certain phones. I was testing the app on a Oneplus 3T the entire day before I realized there was nothing wrong with the code. Just tested the same app on a Moto G, and it works as expected.



来源:https://stackoverflow.com/questions/39979703/alarmmanager-is-not-triggered-when-app-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!