I'm making an reminder app, so I need to send notification every x minutes, as I researched I found that I need to use AlarmManager ( idk if I can use any other method ). So I'm trying to use AlarmManager to fire Notification. It works only when I'm using the phone if phone is locked alarm does not fire, when I unlock the phone then fires.
This is my AndroidManifest.xml:
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".util.NotificationAlarmReceiver"/>
<receiver android:name=".util.BootCompletedReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:enabled="true" android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
</intent-filter>
</receiver>
So I have to receivers one for notification and one to Activate alarmManager on boot.
On NotificationAlarmReceiver that extends BroadcastReceiver I'm also trying to use WakeLock:
@Override public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "com.test.reminder:WakeLockForNotification");
wl.acquire(60 * 1000L /*1 minute*/);
// Notification code is here.
wl.release();
}
So I'm trying to wakeup show notification then release, but only does not wakeup. And this is the code I use to set Alarm:
Intent intentAlarm = new Intent(this, NotificationAlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
PendingIntent pi = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.getBroadcast(this, Constants.ALARM_ID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE) : PendingIntent.getBroadcast(this, Constants.ALARM_ID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+tinyDB.getLong(Constants.notification_interval_key,AlarmManager.INTERVAL_HALF_HOUR), tinyDB.getLong(Constants.notification_interval_key,AlarmManager.INTERVAL_HALF_HOUR), pi);
So is there any solution to show notification every time is needed. I also read on android documentation to not use exact alarm manager, I also saw someone used job scheduler, but i'm not familiar with that, but also with that they had problems, so any idea or solution will be appriciated.