iOS setup¶
- In Xcode, add the Push Notifications capability to the Runner target. That gives the app the
aps-environmententitlement APNs needs. - Use a provisioning profile with push enabled. APNs tokens only work in the environment that issued them, so the SDK reads
aps-environmentfrom the embedded profile and tells the service which one the token belongs to:- Development profiles (builds run from Xcode) and simulator builds report
sandbox. - Ad hoc and enterprise profiles report
production, as do builds without an embedded profile (App Store, TestFlight).
- Development profiles (builds run from Xcode) and simulator builds report
The plugin passes the APNs callbacks of your FlutterAppDelegate on to the SDK; there's no swizzling.
Notification delegate¶
iOS tells an app about a notification arriving while it's open, and about taps, through a single delegate on UNUserNotificationCenter. The SDK needs those calls, so when the plugin registers it makes the SDK the delegate, passing everything that isn't the service's on to the delegate that was there before. Taps on the service's notifications are passed on too, so your own code still sees them.
Which setup you need depends on what else in your app uses that delegate:
| Your app | What to do |
|---|---|
| Nothing else uses the delegate | Nothing |
flutter_local_notifications, or anything else that makes your AppDelegate the delegate |
Nothing |
| Your own delegate, set after the plugin registered | Let the SDK answer first |
firebase_messaging, or another push SDK that takes over the delegate |
Use the AppDelegate setup |
Your own delegate¶
Setting the delegate yourself replaces the SDK's. Let the SDK answer for the service's notifications first:
import PlingBuddySDK
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
if PlingBuddy.handleWillPresent(notification, withCompletionHandler: completionHandler) { return }
completionHandler([.banner, .list, .sound]) // other senders' notifications
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
if PlingBuddy.handleDidReceive(response, withCompletionHandler: completionHandler) { return }
completionHandler() // other senders' notifications
}
Both return false, without calling completionHandler, for notifications that aren't the service's.
Other push SDKs¶
Some push SDKs, such as firebase_messaging, take over the delegate when the app finishes launching, after this plugin registered, and pass every call on to the delegate they replaced. Their own notifications then reach the SDK, which has nothing to pass them on to and hides them. Without changes, their notifications don't show while your app is open. The service's notifications, taps and events keep working.
The fix is to make your AppDelegate the delegate before the plugins register. Neither this plugin nor firebase_messaging replaces a FlutterAppDelegate; it passes each call on to every plugin. Let the SDK answer for the service's notifications, and hand the rest to super, which reaches firebase_messaging:
import Flutter
import PlingBuddySDK
import UIKit
import UserNotifications
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Before the plugins register, so none of them takes over the delegate.
UNUserNotificationCenter.current().delegate = self
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
if PlingBuddy.handleWillPresent(notification, withCompletionHandler: completionHandler) { return }
super.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
}
override func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
if PlingBuddy.handleDidReceive(response, withCompletionHandler: completionHandler) { return }
super.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
}
If your app registers its plugins elsewhere, for example in didInitializeImplicitFlutterEngine with scenes, keep that and only add the first line of didFinishLaunchingWithOptions and the two overrides. The service's notifications never reach firebase_messaging this way, so its onMessage and onMessageOpenedApp don't see them. Its own notifications are presented as you set with setForegroundNotificationPresentationOptions.
Notification service extension¶
Optional. Notifications show without it, but then the service never learns that they arrived.
A Notification Service Extension is a small separate program in your app that iOS runs for each push before showing it. The service's pushes ask for it ("mutable-content": 1) and carry a notification_id. The SDK's helper, PlingBuddyNotificationService, shows them unchanged and tells the service the notification was received, which only an extension can do while the app isn't running. It waits for that for at most 3 seconds before handing the notification to iOS; what isn't sent by then is sent when the app next opens.
A Flutter plugin can't add an extension to your app, so you add it in Xcode. The helper doesn't depend on Flutter, which extensions can't link. The extension reads this install's credentials from an App Group, a storage area your app and its extension share, where the SDK keeps its state.
- Add the target. Open
ios/Runner.xcworkspace, choose File → New → Target… → Notification Service Extension and name it, for example,NotificationService. Set its deployment target to iOS 15 or later. If Xcode offers to activate the new scheme, choose Cancel to stay on Runner. - Fix the build order. In the Runner target's Build Phases, drag Embed Foundation Extensions above Thin Binary. Otherwise Xcode reports a cycle inside Runner.
-
Match the app's version. iOS requires an extension to have its app's version and build number.
- In the extension's build settings, set Marketing Version (
MARKETING_VERSION) to$(FLUTTER_BUILD_NAME)and Current Project Version (CURRENT_PROJECT_VERSION) to$(FLUTTER_BUILD_NUMBER). -
These come from
Flutter/Generated.xcconfig, so give the extension an xcconfig per build configuration that includes it, and set each one as the configuration's base in the project's Info tab. Include the extension's Pods xcconfig too. ForDebug:Xcode build settings (.xcconfig)#include? "../Pods/Target Support Files/Pods-NotificationService/Pods-NotificationService.debug.xcconfig" #include "../Flutter/Generated.xcconfig"Do the same for
ReleaseandProfile.
- In the extension's build settings, set Marketing Version (
-
Add an App Group. In Signing & Capabilities, add the App Groups capability to both the Runner and the extension target, with the same group, for example
group.com.example.myapp. - Tell the SDK the group. Add the key
PlingBuddyAppGroup, with the group id as its value, to bothRunner/Info.plistand the extension'sInfo.plist. The SDK keeps its state and event queue in the group; an app that adds the group in an update keeps its installation, because the SDK moves its state there. -
Link the helper. Add the extension target to
ios/Podfile, outside theRunnertarget so it doesn't get Flutter, then runflutter build ios --config-onlyorpod install:Ruby (Podfile)target 'NotificationService' do use_frameworks! pod 'PlingBuddyExtension', :path => 'path/to/plingbuddy-sdks/ios' end -
Forward pushes to the helper. Replace the extension's
NotificationService.swiftwith:Swiftimport PlingBuddyExtension import UserNotifications class NotificationService: UNNotificationServiceExtension { private let plingBuddy = PlingBuddyNotificationService() override func didReceive( _ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void ) { if !plingBuddy.didReceive(request, withContentHandler: contentHandler) { // Not from the notification service; handle other senders' pushes here. contentHandler(request.content) } } override func serviceExtensionTimeWillExpire() { plingBuddy.serviceExtensionTimeWillExpire() } }didReceivereturns false, without callingcontentHandler, for pushes without anotification_id, so an extension that also serves other senders keeps handling theirs.
To check it, run the app on a device, put it in the background and send a test notification from the dashboard. It should show as before, and the dashboard should count it as received. In Console.app, select the device and filter on the process name of the extension to see it run.
If the App Group can't be opened (the key is missing, or the group isn't in a target's capabilities or provisioning profile), the extension still shows the notification but reports nothing, and logs plingbuddy: can't open the App Group … once per process.