Notifications¶
The service sends alert pushes, which iOS shows by itself. The SDK decides whether they show while the app is open, reports what happens to them, and, with a Notification Service Extension, reports that they arrived.
Notifications while the app is open¶
The service's notifications are hidden while the app is open, iOS's default, unless the configuration says so:
PlingBuddy.initialize(Configuration(
applicationId: "<your application id>",
showNotificationsInForeground: true
))
A hidden notification is reported to the service as suppressed.
Notification delegate¶
iOS tells an app about a notification arriving while it's open, and about taps, through a single delegate on UNUserNotificationCenter.
- At
initializethe SDK makes itself the delegate, and passes 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. - If you set your own delegate after
initialize, let the SDK answer first:
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
Push SDKs that take over the delegate after initialize and pass every call on to the one they replaced send their own notifications to the SDK, which hides them. Set your own delegate first and answer as above.
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 reports them received. It waits for that report for at most 3 seconds; anything not sent by then is sent when the app next opens.
- Add the target: File → New → Target… → Notification Service Extension, with a deployment target of iOS 15 or later.
- Add an App Group: add the App Groups capability to both the app and the extension, 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 both targets'Info.plist. The SDK keeps its state file and event queue in the group, named after the app's bundle id. An app that adds the group in an update keeps its installation: the SDK moves its state into the group. -
Link the helper: give the extension target
pod 'PlingBuddyExtension'in the Podfile, outside the app's target, or thePlingBuddyExtensionpackage product.Ruby (Podfile)target 'NotificationService' do use_frameworks! pod 'PlingBuddyExtension', :path => 'path/to/plingbuddy-sdks/ios' end -
Forward pushes to it:
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) { contentHandler(request.content) // not from the notification service } } override func serviceExtensionTimeWillExpire() { plingBuddy.serviceExtensionTimeWillExpire() } }didReceivereturnsfalse, 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.
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.