Reputation: 141
I have implemented Firebase Cloud Messaging in my Flutter app and in most cases it seems to work. I can receive notifications in my app when:
flutter run
flutter run --release
all targeting my iPhone that's linked to the computer. I am using flavors (dev, qa, prod), and it works across all flavors.
When it doesn't work is when I package it up for Firebase App Distribution or Testflight. It's easier focus on App Distribution, so I'll address what I've done there.
In setting up messaging, I have enabled push notifications in XCode (I originally had the problem of hitting the development APNs, but I changed the configuration to use the production service). Here's my entitlement file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>production</string>
<key>com.apple.developer.applesignin</key>
<array>
<string>Default</string>
</array>
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:N/A</string>
</array>
</dict>
</plist>
And Background Modes are enabled in the Info.plist file as well:
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
I have a key created in the Apple Developer Portal as required:
And I have that uploaded into Firebase as required:
When deploying to App Distribution, I run the following commands:
flutter build ios --flavor <env>
cd ios
xcodebuild -workspace Runner.xcworkspace -scheme <env> archive -archivePath Runner.xcarchive
xcodebuild -exportArchive -archivePath Runner.xcarchive -exportOptionsPlist exportOptions.plist -exportPath ../build/ios -allowProvisioningUpdates
where is one of my flavors, as mentioned above and my exportOptions.plist is
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>compileBitcode</key>
<false/>
<key>method</key>
<string>ad-hoc</string>
</dict>
</plist>
I'm happy to provide more clues, configurations, explaination, but right now, I'm at a loss as to what's missing and could be causing this behavior. So the question is, why do push notification not work when deployed through an archive?
Upvotes: 4
Views: 1915
Reputation: 141
TLDR: I was missing application.registerForRemoteNotifications()
in my AppDelegate.swift.
Looks like my AppDelegate.swift would have been helpful to post:
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
let controller : FlutterViewController = window.rootViewController as! FlutterViewController
let flavorChannel = FlutterMethodChannel(name: "flavor", binaryMessenger: controller.binaryMessenger)
flavorChannel.setMethodCallHandler({ (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
result(Bundle.main.infoDictionary?["Flavor"])
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
We had implemented the directions according to the README in the firebase_messaging project (https://pub.dev/packages/firebase_messaging). This necessitated adding if #available
lines. Yes, this is only for doing method swizzling, and we weren't (i.e. we didn't have FirebaseAppDelegateProxyEnabled
in our Info.plist file), but I stumbled upon Flutter FCM on iOS 14 (they are implementing swizzling), and noticed that they had an extra line in their file: application.registerForRemoteNotifications()
. Once I added that line, (on the line before the return), push notifications started working.
Upvotes: 3