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.