'Flutter Push Notification not working in foreground on IOS above IOS 10
here is my code example I found the problem but don't know how to replace this and working for all above IOS 10 or all
var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings(onDidReceiveLocalNotification: onDidRecieveLocalNotification);///HERE IS THE PROBLEM I THINK
var initializationSettings = new InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);
_firebaseMessaging.subscribeToTopic(MyConstants.ClientName);
the onDidReceiveLocalNotification is work in foreground when IOS version is 10 or lower then IOS 10
this is _firebaseMessaging.configure code is below
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message ${message}');
displayNotification(message);
if (message['data']['bit'] == '1') {
ShowAlertNotifiaction(context, message['data']['title'], message['data']['body']);
} else {}
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
if (message['data']['bit'] == '1') {
onPageChanged(1);
navigationTapped(1);
} else {
onPageChanged(0);
navigationTapped(0);
}
},
);
_firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true,
badge: true, alert: true)); _firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) { print("Settings registered: $settings"); }); _firebaseMessaging.getToken().then((String token) { assert(token != null); print(token); }); This for permission
_firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
print(token);
});
Note This above Code Is inside initState Method
After that Add attributes and map that in future here is code
Future displayNotification(Map<String, dynamic> message) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails('channelid', 'flutterfcm', 'your channel description', importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
message['data']['title'],
message['data']['body'],
platformChannelSpecifics,
payload: 'hello',
);}
and then Finally showing the notification
Future onDidRecieveLocalNotification(int id, String title, String body, String payload) async {
showDialog(
context: context,
builder: (BuildContext context) => new CupertinoAlertDialog(
title: new Text(title),
content: new Text(body),
actions: [
CupertinoDialogAction(
isDefaultAction: true,
child: new Text('Ok'),
onPressed: () async {
Navigator.of(context).pop();
// On select iOS notification
},
),
],
),
);}
this is my packages detail for notification
flutter_local_notifications: ^0.7.1+1
firebase_messaging: ^5.0.1+1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
