12

Hello flutter newbie here! I'm using flutter local notifications and adding the default code to my app. It works in Android but doesn't work in iOS. any idea why?

I tried with both the iOS simulator for iPhone 8 and iPhone 11. It works on my android emulator which is a Pixel 3 API 29.

I have also enabled the Notifications in iOS (Settings>this app>Notifications) for this app and gave the permissions.

For Android I have added permission requests in AndroidManifest.xml.

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';


class AppNotification extends StatefulWidget {
  static const String id = 'app_notification';

  @override
  _AppNotificationState createState() => _AppNotificationState();
}

class _AppNotificationState extends State<AppNotification> {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  @override
  void initState() {
    super.initState();
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    var android = AndroidInitializationSettings('app_icon');
    var iOS = IOSInitializationSettings();
    var initSettings = InitializationSettings(android, iOS);

    flutterLocalNotificationsPlugin.initialize(initSettings,
        onSelectNotification: onSelectNotification);
  }

  Future onSelectNotification(String payload) async {
    debugPrint("payload : $payload");
    showDialog(
      context: context,
      builder: (_) {
        return AlertDialog(
          title: Text("PayLoad"),
          content: Text("Payload : $payload"),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(
              child: Text('Notification'),
              onPressed: _showNotificationWithDefaultSound,
            ),
            FlatButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('back'),
            )
          ],
        ),
      ),
    );
  }

  // TODO iOS doesn't work

  Future _showNotificationWithDefaultSound() async {
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
        'your channel id', 'your channel name', 'your channel description',
        importance: Importance.Max, priority: Priority.High);
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
      0,
      'New Post',
      'How to Show Notification in Flutter',
      platformChannelSpecifics,
      payload: 'Default_Sound',
    );
  }
vpxoxo
  • 195
  • 1
  • 2
  • 7
  • Hey I'm facing the same problem with iOS, when i click a button nothing happens. Did you manage to find a solution? – flutterNoob Apr 02 '20 at 17:20
  • Does this answer your question? [How can I test Apple Push Notification Service without an iPhone?](https://stackoverflow.com/questions/1080556/how-can-i-test-apple-push-notification-service-without-an-iphone) – Vallas Jul 12 '22 at 11:12

2 Answers2

30

I added This line in the "AppDelegate.swift" inside didFinishLaunchingWithOptions method and it solved my issue:

if #available(iOS 10.0, *) {
    UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

Documentation Link: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications#%EF%B8%8F-ios-setup

// I found this in the documentation of the plugin, hopefully it solves your issue!

fixatd
  • 1,394
  • 1
  • 11
  • 19
K-Soliman
  • 914
  • 7
  • 11
1

in the notification_service class add requestAlertPermission: true,

Eg:

Future<void> initNotification() async {
    // Android initialization
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');

// ios initialization
const IOSInitializationSettings initializationSettingsIOS =
    IOSInitializationSettings(
  requestAlertPermission: true,
  requestBadgePermission: false,
  requestSoundPermission: false,
);

const InitializationSettings initializationSettings =
    InitializationSettings(
        android: initializationSettingsAndroid,
        iOS: initializationSettingsIOS);
// the initialization settings are initialized after they are setted
    await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 30 '22 at 20:34