1

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>
Milestone
  • 13
  • 5
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Mar 30 '22 at 23:34

2 Answers2

1

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?

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • I have already tried the suggestions in this thread. But it doesn't work. – Milestone Mar 31 '22 at 00:10
  • Can't I write a code that won't be deleted until the time comes, in the manifest file? – Milestone Mar 31 '22 at 00:11
  • No. You can't. In the manifest, you just add the intents that you want to receive. However, the system won't send your intent because the phone was rebooted. So, you must subscribe for the BOOT_COMPLETED (it means the device was turned on) and then, you re-schedule your alarm. That's how it does. If it is not working, it is because your are missing something – guipivoto Mar 31 '22 at 00:46
1

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.

james
  • 36
  • 3