flutter-apns
flutter-apns copied to clipboard
onMessage does not get called on iOS
Hello, I'm having the same problem reported on #40.
My app receives push notifications correctly when on background, while nothing is called when in foreground.
This tells me that permissions are ok, and the token is sent correctly.
I've also looked for errors on MacOS Console app, but I've found nothing.
My app has the following configuration;
flutter: ^3.0.5 flutter_apns: ^1.6.0
I initialize my connector with:
_pushConnector.shouldPresent = (m) {
// This never prints anything, I suppose I doesn't get called
print(
"$runtimeType.shouldPresent\n\n"
"ApnsRemoteMessage\n"
"$m\n"
"Payload\n"
"${m.payload}\n",
);
return Future.value(true);
};
// onLaunch is getting called, when I tap on notification with a closed app
// onResume is getting called, when I tap on notification with app in background
// onMessage gives no joy! 😞
_pushConnector.configure(
onLaunch: (m) => _onPush("onLaunch", m),
onResume: (m) => _onPush("onResume", m),
onMessage: (m) => _onPush("onMessage", m),
);
Future<void> _onPush(String name, RemoteMessage message) async {
print("$runtimeType.$name\n\n"
"${message.notification?.title}\n"
"${message.notification?.body}\n"
"${message.data}\n");
onPushMessage.call(message.data);
}
Can someone help me on this?
Finally I've found it! 🎉
The problem is the following:
in AppDelegate.swift, when registering UNUserNotificationCenter delegate, ORDER DOES MATTER!
so, to make it work, this is the correct way to register it:
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// Needed for flutter_apns package, to enable push notifications
// WARNING: This must be called AFTER GeneratedPluginRegistrant.register,
// otherwise 'onMessage' callbacks wouldn't work
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
My suggestion is to update the README.md, and to specify how important order is.