Pumuckelo
Pumuckelo

Reputation: 389

flutter_local_notifications not working on ios, but working on android

If I use the show() method of the FlutterLocalNotificationsPlugin then I get a notification on Android but not on ios.

showNotification(
    String notificationId, String title, String body, String payload) async {
  final AndroidNotificationDetails androidPlatformChannelSpecifics =
      AndroidNotificationDetails(
          "important-notifications",
          tr("DEVICE.IMPORTANT_NOTIFICATIONS_NAME"),
          tr("DEVICE.IMPORTANT_NOTIFICATIONS_DESCRIPTION"),
          importance: Importance.max,
          priority: Priority.high,
          ledColor: Colors.pink,
          ledOffMs: 50,
          ledOnMs: 50,
          color: Colors.purple,
          styleInformation: BigTextStyleInformation(""),
          ticker: 'ticker');

  final NotificationDetails platformChannelSpecifics = NotificationDetails(
    android: androidPlatformChannelSpecifics,
  );
  await flutterLocalNotificationsPlugin
      .show(0, title, body, platformChannelSpecifics, payload: payload);
}

The solution to this problem is below.

Upvotes: 1

Views: 2902

Answers (1)

Pumuckelo
Pumuckelo

Reputation: 389

There's a problem with platform specifics in the flutter_local_notifications plugin.

For example, if you use

await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          .createNotificationChannel(channel);

inside of your local notification initialization function, then the function will stop at this point if you're not on an android device. This results in not initializing the plugin with ios + android settings.

Solution To fix this just wrap everything you do with .resolvePlatformSpecificImplementation into a platform check

if (Platform.isAndroid) {
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          .createNotificationChannel(channel);
    }
if (Platform.isIOS) {
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              IOSFlutterLocalNotificationsPlugin>()
          .requestPermissions(
            alert: true,
            badge: true,
            sound: true,
          );
    } 

Upvotes: 3

Related Questions