My task is to set a alarm to trigger receiver to show notification a specific time.
And the code works perfectly if app is opened or just in background.
At the time app is being killed, receiver can still be triggered when alarm is executed.
However, no notification was shown.
What can I do?
Alarm function:
public static void makeAlarm(Context context,Date date) {
Log.i("Alarm", "makeAlarm");
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP,
date.getTime(), alarmIntent);
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Alarm", "onReceive");
android.support.v4.app.NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle("Title")
.setContentText("Message");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Random rand = new Random();
mNotificationManager.notify(rand.nextInt(), mBuilder.build());
}
}
Manifest.xml
<receiver
android:name=".receiver.AlarmReceiver"
android:enabled="true"
android:exported="true" />