flutter_local_notifications icon indicating copy to clipboard operation
flutter_local_notifications copied to clipboard

When use flutter_local_notifications with firebase message on ios 18 get push notification twice

Open Nak-2000 opened this issue 1 year ago • 6 comments

Describe the bug since the ios18 release there's some issue with push notification twice when I use flutter local notification function inside FirebaseMessaging.onMessage.listen() function. and it's only happen to ios18 device (even after firebase has already fix the notification twice but the local notification still push twice).

Expected behavior I would expect the notification to only push once.

Sample code to reproduce the problem

var initializationSettingsAndroid = const AndroidInitializationSettings('@mipmap/ic_launcher'); var initializationSettingsIOS = const DarwinInitializationSettings( defaultPresentAlert: true, );

var initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS); await flutterLocalNotificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse: (response) async {

print("onDidReceiveNotificationResponse:");
selectNotificationSubject.add(response.payload!);
print("onDidReceiveNotificationResponse: ${response.payload}");

});

FirebaseMessaging.onMessage.listen((RemoteMessage remoteMessage) { var message = remoteMessage.data; print("onMessage==>: listen $message"); appNotification = AppNotification( id: message['id'], type: message['type'], title: message['title'], content: message['content'], referenceId: message['reference_id'], screenDetail: message['detail_screen'], ); _showNotification(appNotification); });

Future _showNotification(AppNotification? appNotification) async { try { const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails( 'default_channel_id', 'your_channel_name', channelDescription: 'your_channel_description', importance: Importance.max, priority: Priority.high, showWhen: false, );

  const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
    1,
    "${appNotification!.title}",
    "${appNotification.content}",
    platformChannelSpecifics,
    payload: jsonEncode(appNotification),
  );
} on Exception catch (e) {
  log("show notification error $e");
}

}

Nak-2000 avatar Nov 11 '24 01:11 Nak-2000

Are you sure firebase isn't also trying to show a notification? In that case you'd get the first notification from fcm and then another one from this package. Try commenting out the call to show() and see if you get 0, 1, or 2 notifications

Levi-Lesches avatar Nov 18 '24 22:11 Levi-Lesches

Yes same problem for me.

FirebaseMessaging.onMessage.listen(_onMessage); - triggers twice. So when the flutterLocalNotificationsPlugin.show is commented out then nothing will happen.

bbader-kada avatar Nov 28 '24 14:11 bbader-kada

Interesting. If you can demonstrate that it's firebase triggering twice and not an issue with this package, I'd recommend we close this issue and open one on firebase_messaging instead.

One way to demonstrate this would be to use shared preferences to set a flag, but check if it's already set. If firebase is really triggering twice, you should only be able to set the flag, and show a notification, once.

Please let us know if you try this

Levi-Lesches avatar Nov 28 '24 18:11 Levi-Lesches

In ios only it will trigger twice, we can check platform.isAndroid flag to call this method flutterLocalNotificationsPlugin.show

SomaPrasanna4037 avatar Dec 06 '24 06:12 SomaPrasanna4037

for this i get solution from sever end send this type of data { "to": "<device_token>", "data": { "title": "Your Title", "body": "Your Body", "id": "1" } }

jkarjunsingh avatar Jan 23 '25 10:01 jkarjunsingh

Based on what @bbader-kada has said, the issue is that the FCM callback is triggering twice. If so, then this isn't this plugin can help resolve. The other thing to be conscious of on if you're using this plugin when it isn't necessary. I don't recall when but the FCM docs state here they can handle showing notifications in the foreground. If you've configured this and use this plugin to show a notification as well then this could be another cause for seeing two notifications

MaikuB avatar Jan 24 '25 12:01 MaikuB