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',
);
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With