Setup¶
Push Notifications capability¶
- In Xcode, add the Push Notifications capability to the app 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
Start the SDK¶
Start the SDK and forward the APNs callbacks in your AppDelegate. There's no swizzling.
Swift
import PlingBuddySDK
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
PlingBuddy.initialize(Configuration(applicationId: "<your application id>"))
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
PlingBuddy.didRegisterForRemoteNotifications(deviceToken: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
PlingBuddy.didFailToRegisterForRemoteNotifications(error: error)
}
initializeruns on the main actor. Call it at every launch.- When the app knows its user at launch, pass it as well, so the first request already names the user:
initialize(configuration, externalId: "user-42"). See Users and data. initializecan be called again. Each call replaces the whole configuration, so pass every setting again,baseUrlincluded: a configuration without it goes to production, which starts a new installation there.
Configuration¶
| Parameter | Default | |
|---|---|---|
applicationId |
Your application's id, from its settings page in the dashboard (Getting started). | |
baseUrl |
production | PlingBuddy.stagingUrl for staging. |
showNotificationsInForeground |
nil: hidden |
Whether the service's notifications show while the app is open. See Notifications. |
fetchLocaleAndTimezone |
false |
Fill in the user's country, language and time zone from the device. See Users and data. |
identityTokenProvider |
nil |
For applications with identity verification. |
For staging:
Swift
PlingBuddy.initialize(Configuration(
applicationId: "<your staging application id>",
baseUrl: PlingBuddy.stagingUrl
))
Ask for permission¶
Ask when the app wants to offer notifications:
Swift
let allowed = await PlingBuddy.askForPermission()
- It shows the system prompt while iOS still can, and once the user has denied it, an alert offering the app's notification settings. It answers whether notifications are allowed afterwards.
- It also upgrades provisional (quiet) delivery to full authorization.
- Change the alert's texts with a
SettingsPrompt:
Swift
let allowed = await PlingBuddy.askForPermission(settingsPrompt: SettingsPrompt(
title: "Notifications are off",
message: "Open Settings to turn them on.",
openButton: "Open Settings",
cancelButton: "Not now"
))
PlingBuddy.permissionStatus() reads the permission without prompting: .granted, .provisional, .denied or .notDetermined. PlingBuddy.pushToken() answers the APNs token, as lowercase hex, while notifications are allowed, and otherwise nil.