Flutter local notification doesn t work for iOS

0 votes

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',
    );
  }

To know more about Flutter, join our Flutter APP Development Course today.

Sep 21, 2022 in Others by gaurav
• 23,260 points
3,832 views

1 answer to this question.

0 votes

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
}
answered Sep 21, 2022 by rajatha
• 7,640 points

Related Questions In Others

0 votes
2 answers

How to set margin for a Container in Flutter?

Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, ...READ MORE

answered Apr 17, 2023 in Others by anonymous
8,496 views
0 votes
1 answer

How to use for loop in Flutter?

Hi@akhtar, As the name suggests Dart for in ...READ MORE

answered Sep 3, 2020 in Others by MD
• 95,440 points
18,793 views
0 votes
0 answers

Custom URL scheme for Pinterest on iOS Safari to open board URL?

I can't for the life of me ...READ MORE

Sep 23, 2022 in Others by gaurav
• 23,260 points
290 views
0 votes
1 answer

What is the future of flutter?

Hi@MD, Flutter is a rather new cross-platform framework ...READ MORE

answered Jul 17, 2020 in Others by akhtar
• 38,230 points
929 views
0 votes
1 answer

What is Flutter?

Hi@akhtar, Flutter is an app SDK for building ...READ MORE

answered Jul 17, 2020 in Others by MD
• 95,440 points
1,087 views
0 votes
1 answer

ios: localize emoji name (kCFStringTransformToUnicodeName)

There is no api for this job. Unicode ...READ MORE

answered Sep 21, 2022 in Others by rajatha
• 7,640 points
748 views
0 votes
1 answer

iOS Keyboard Extension: Memoji like paste

All the mentioned apps use UITextView, upon ...READ MORE

answered Sep 21, 2022 in Others by rajatha
• 7,640 points
527 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP