I'm trying to use Local Notification on Flutter with an Ios device, it works, but the notification popup is shown two times, because one is from my flutterLocalNotificationsPlugin.show(...)
and one is showed automatically when a notification is catched.
In my case, I would like to show the notification only using Local Notifications, so I have to prevent showing the automatic notification. I've tried to send a notification without the "notification" object but only with a "data" object: in this case the automatic notification doesn't appears, but the event is not catched from the method FirebaseMessaging.onMessage.listen((RemoteMessage message) async {...})
, so also Local Notifications are not working.
On Flutter:
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(alert: false);
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
const IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings();
final InitializationSettings initializationSettings =
InitializationSettings(
iOS: initializationSettingsIOS,
);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: (payload) async {
print("selected");
},
);
Future<String> _downloadAndSaveFile(
String url, String fileName) async {
final Directory directory =
await getApplicationDocumentsDirectory();
final String filePath = '${directory.path}/$fileName';
final http.Response response = await http.get(Uri.parse(url));
final File file = File(filePath);
await file.writeAsBytes(response.bodyBytes);
return filePath;
}
final String bigPicturePath = await _downloadAndSaveFile(
message.data['image'], 'bigPicture.jpg');
final IOSNotificationDetails iOSPlatformChannelSpecifics =
IOSNotificationDetails(attachments: <IOSNotificationAttachment>[
IOSNotificationAttachment(bigPicturePath)
]);
NotificationDetails platformChannelSpecifics =
NotificationDetails(iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, message.data['title'],
message.data['body'], platformChannelSpecifics,
payload: 'item x');
}
print("Notification arrived");
});
}
On server (PHP):
$fields = array(
'notification' => [
'content_avaible' => true,
'body' => $message,
'title' => $title,
'priority' => "high",
"image" => 'image' => "https://foo.bar/image.png"
],
"apns" => [
"payload" => [
"aps" => [
"mutable-content" => 1
]
],
"fcm_options" => [
'image' => "https://foo.bar/image.png"
]
],
'registration_ids' => $alltokens,
'data' => [
'image' => "https://foo.bar/image.png"
],
);
$headers = array(
'Authorization: key=' . API_ACCESS_KEY_IOS,
'Content-Type: application/json'
);
$curlCall = curl_init();
curl_setopt($curlCall, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($curlCall, CURLOPT_POST, true);
curl_setopt($curlCall, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlCall, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlCall, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlCall, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($curlCall);
curl_close($curlCall);