0

I use flutter_local_notifications for scheduled Android notifications:

    tz.initializeTimeZones();

final String currentTimeZone = await FlutterTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(currentTimeZone));

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'your_channel_id', 'your_channel_name',
      importance: Importance.max, priority: Priority.high, ticker: 'ticker');
var platformChannelSpecifics = NotificationDetails(
         android: androidPlatformChannelSpecifics);
 

var date = tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5));

await flutterLocalNotificationsPlugin.zonedSchedule(
    0,
    'scheduled title',
    'scheduled body',
    date,
    platformChannelSpecifics,
    androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle);

}

This works when the app is in the background, but it doesn't work if the device is locked. I need a sound signal when the device is locked.

Here is permissions in AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

Do I need to use Full-screen intent notifications to get notifications when device is locked?

1 Answer 1

0

Android devices may not allow notifications when the screen is locked for battery optimization. For this reason, I recommend ignoring battery optimization.

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

If you also use a notification service, you also need this permission on Android 9+ versions.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Sign up to request clarification or add additional context in comments.

4 Comments

As far as I know, this is a dangerous permission, Google doesn't like its use.
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> Google doesn't allow the use of this permission unless "Battery optimization" is the key concern of your app. They rarely accept it, even if you try to convince them. Avoid this permission. Instead, prompt users to enable optimization manually themselves.
await Permission.ignoreBatteryOptimizations.request()
But I see apps on android that play sounds and notifications on a locked device without asking for this permission. Maybe there is another way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.