Search code examples
androidbroadcastreceiveralarmmanagerandroid-pendingintent

App crashes while trying to run AlarmManager and PendingIntent


I am trying to run my app, contains use in AlarmManager, but the program crashes for some reason. here is the specific code:

private void scheduleNotification (Notification notification, int delay)
    {
        Intent notificationIntent = new Intent(this, MyNotificationPublisher.class);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATIONID, 1);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATIONID, notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        long futureMillis = SystemClock.elapsedRealtime() + delay;
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        assert alarmManager != null;
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureMillis, pendingIntent);
    }

I wrote the correct permissions using, in the manifest, but the program crashes with the following error:

FATAL EXCEPTION: main
                                                                                                Process: com.example.localnotification, PID: 3172
                                                                                                java.lang.IllegalArgumentException: com.example.localnotification: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
                                                                                                Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
                                                                                                    at android.app.PendingIntent.checkFlags(PendingIntent.java:401)
                                                                                                    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:671)
                                                                                                    at android.app.PendingIntent.getBroadcast(PendingIntent.java:658)
                                                                                                    at com.example.localnotification.MainActivity.scheduleNotification(MainActivity.java:39)
                                                                                                    at com.example.localnotification.MainActivity.lambda$onCreate$0$com-example-localnotification-MainActivity(MainActivity.java:28)
                                                                                                    at com.example.localnotification.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
                                                                                                    at android.view.View.performClick(View.java:7506)
                                                                                                    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1218)
                                                                                                    at android.view.View.performClickInternal(View.java:7483)
                                                                                                    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
                                                                                                    at android.view.View$PerformClick.run(View.java:29357)
                                                                                                    at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                    at android.os.Looper.loop(Looper.java:288)
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:7884)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

2024-03-02 22:20:35.898 3172-3172 Process com.example.localnotification


Solution

  • It's not a manifest thing, it's a flag of your pending intent if you're targeting API 31+. You can check it out here in the docs, but in short it should be like that

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE);