As long as I don't turn the phone off (even if the application is closed), the forward notification that I have created with the alarm manager in my android-java mobile application does not have a problem. But when I restart the phone before the notification time comes, it doesn't work even if it's time for my notification. Can you please help? Thanks.
MY ALARM SERVICE CLASS
package com.gokhankopuz.kopuzfilo.services;
public class AlarmService {
private Context context = null;
private long timeInMillis = 0L;
private String notificationTitle, notificationDesc = "";
public AlarmService(Context context, long timeInMillis, String notificationTitle, String notificationDesc) {
this.context = context;
this.timeInMillis = timeInMillis;
this.notificationTitle = notificationTitle;
this.notificationDesc = notificationDesc;
}
public void setAlarm() {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, getPendingIntent());
}
private PendingIntent getPendingIntent() {
@SuppressLint("UnspecifiedImmutableFlag")
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) timeInMillis, getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
private Intent getIntent() {
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("notificationTitle", notificationTitle);
intent.putExtra("notificationDesc", notificationDesc);
return intent;
}
}
MY ALARM RECEIVER CLASS
package com.gokhankopuz.kopuzfilo.receivers;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
buildNotification(context, intent);
}
private void buildNotification(Context context, Intent intent) {
String notificationTitle = intent.getStringExtra("notificationTitle");
String notificationDesc = intent.getStringExtra("notificationDesc");
Notify.build(context)
.setImportance(Notify.NotifyImportance.HIGH)
.setTitle(notificationTitle)
.setContent(notificationDesc)
.setColor(R.color.app_background)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(false)
.show();
}
}
MY MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.gokhankopuz.kopuzfilo">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
tools:ignore="CoarseFineLocation" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.KopuzFilo"
tools:ignore="AllowBackup">
<activity
android:name=".activities.NavigationActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="true" />
<activity
android:name=".activities.MainActivity"
android:exported="true"
android:configChanges="orientation|screenSize|keyboardHidden"
android:windowSoftInputMode="stateVisible|adjustPan" />
<activity
android:name=".activities.SplashActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".receivers.AlarmReceiver" />
</application>
</manifest>
I added the following permissions to my manifest file but it didn't work
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
CodePudding user response:
This is the expected behavior. If you turn off your device, all alarms are deleted. So, to workaround this issue, you must register for boot complete broadcast and re-schedule your alarm.
You can find more details on how to do it in the following question/answer:
does Alarm Manager persist even after reboot?
CodePudding user response:
I think you can try use work manager, which will be executed by system.
WorkManager is the recommended solution for persistent work. Work is persistent when it remains scheduled through app restarts and system reboots. Because most background processing is best accomplished through persistent work, WorkManager is the primary recommended API for background processing.