Reputation: 11
I have created a scheduled repeat alarm once in a particular time everyday using AlarmManager to trigger notification in a class that extends BroadcastReceiver. But the onReceive method is never called from the activity where the AlarmManager was set.
I'm using Android Oreo to test the apps, so I created a method createNotificationChannel() to set the NotificationChannel and call it from my MainActivity onCreate() method.
public void createNotificationChannel() {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.O) {
// Create the NotificationChannel with all the parameters.
NotificationChannel notificationChannel = new NotificationChannel
(PRIMARY_CHANNEL_ID,
"My Notification",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription
("My Notification Description");
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
with some variables:
private NotificationManager mNotificationManager;
private static final int NOTIFICATION_ID = 0;
private static final String PRIMARY_CHANNEL_ID =
"primary_notification_channel";
Then I setup a button that onClick() method calls the startAlarm() method as follow:
public void startAlarm(int aHour, int aMinutes) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent notifyPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,aHour);
calendar.set(Calendar.MINUTE, aMinutes);
alarmManager.setInexactRepeating
(AlarmManager.ELAPSED_REALTIME_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, notifyPendingIntent);
}
And finally in AlarmReceiver.java class, in onReceive() as follow:
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
callNotification(context);
}
private void callNotification(Context context) {
Intent contentIntent = new Intent(context, DRSetting.class);
PendingIntent contentPendingIntent = PendingIntent.getActivity
(context, NOTIFICATION_ID, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, PRIMARY_CHANNEL_ID);
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon_mynotif)
.setContentTitle("My Title")
.setContentIntent(contentPendingIntent)
.setContentText("Reminder for you!")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentInfo("Info");
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}
in AndroidManifest.xml, I put this:
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
and permission as follow:
<uses-permission android:name="android.permission.SET_ALARM" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
So, can somebody please help me, since I can't find the problem why the onReceive() method is never called, and therefore, there is no notification that ever triggered in callNotification().
Upvotes: 0
Views: 1092
Reputation: 11
I found the problem that caused the alarm won't trigger, thanks to JournalDev. It's because since Android Oreo, implicit broadcast receivers won't work when registered in the AndroidManifest.xml. Instead, we should use explicit broadcast receivers. Hence, I rework the code adding two additional method as follow:
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("android.app.action.NEXT_ALARM_CLOCK_CHANGED");
registerReceiver(alarmReceiver, filter);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(alarmReceiver);
}
Also, we should pay attention to the calendar month value setting, if we want to set specific date to trigger the alarm. Month in Java start from 0 to 11, so we need to -1 when converting the month value from DatePicker. I hope this answer can help others facing the same problem.
Upvotes: 0