Flutter ios device doesn't trigger onMessage from FCM notification. Sendtodevice fails when implementing APNs

322

To enable notification for iOS you need to set up FCM for iOS first: As you said you have generated APNs. just confirm have you linked apn with firebase project or not. after that open your project ios module in Xcode and make following changes in AppDelegate

if #available(iOS 10.0, *) {
  UNUserNotificationCenter.current().delegate = self as? 
  UNUserNotificationCenterDelegate
}

Make changes in flutter side (I'm using this code in splash screen):

Initialization

FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

Code in initState()

var initializationSettingsAndroid =
    new AndroidInitializationSettings('@mipmap/ic_launcher'); //replace with your app icon file
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
    initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
    onSelectNotification: onSelectNotification);

FirebaseNotifications().setUpFirebase(_firebaseMessaging);
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    showNotification(
        message['notification']['title'], message['notification']['body']);
    print("onMessage: $message");
  },
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
  },
);

Methods for notification:

void showNotification(String title, String body) async {
   await _demoNotification(title, body);
}

Future<void> _demoNotification(String title, String body) async {
  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
     'channel_ID', 'channel name', 'channel description',
     importance: Importance.Max,
     playSound: true,
     sound: 'sound',
     showProgress: true,
     icon: 'notification_icon',
     color: AppTheme.secondaryaccent,
     priority: Priority.High,
     ticker: 'test ticker');

 var iOSChannelSpecifics = IOSNotificationDetails();
 var platformChannelSpecifics = NotificationDetails(
     androidPlatformChannelSpecifics, iOSChannelSpecifics);
  await flutterLocalNotificationsPlugin
    .show(0, title, body, platformChannelSpecifics, payload: 'test');
 }
Share:
322
Dorian Holmes
Author by

Dorian Holmes

Updated on December 18, 2022

Comments

  • Dorian Holmes
    Dorian Holmes over 1 year

    In my fcm cloud function to send notifications to other players, it fails to send when I add the apns layer. When the apns layer is removed, notifications appear on android but for iOS the notifications are received in the front end but fail to trigger the onMessage function to display an alert dialog.

     var payload = {
            data: {
                click_action: "FLUTTER_NOTIFICATION_CLICK",
                notificationType: "friendsRequest",
                fromUsername: fromUsername,
                fromProfilePic: fromProfilePic,
                fromColorIndex: fromColorIndex,
                type: type
            },
        };
    

    This payload returns a server response of "crash"

  • Dorian Holmes
    Dorian Holmes over 3 years
    I did set this app. I will include the other steps I took. Hopefully, you can spot my mistake. I also did not use flutter local notifications. I have a function in onMessage that uses the data received from the notification to trigger an alert dialog.
  • Dorian Holmes
    Dorian Holmes over 3 years
    My notification are failing to send in this case.
  • Shailesh Bhokare
    Shailesh Bhokare over 3 years
    Are you subscribing from app to get notifications..?
  • Dorian Holmes
    Dorian Holmes over 3 years
    I get the app registration token and request to send notifications.