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

Viewed 372

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"

1 Answers

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');
 }
Related