iOS: Delaying the Push Prompt
On iOS, apps can handle and display push alerts (including sounds and badges) only if the users expressed consent to receive them.
By default, Test App will ask the user to opt-in for push notifications as soon as you call startEngine()
. On the other hand, sophisticated apps may want to control when to show the push prompt during their welcome experiences; this way, users can first understand the value of enabling push notifications, and then they will be prompted using the native iOS dialog.
You can prevent the default behavior by delaying the push prompt in the following way.
1. Delaying the push prompt
As usual, start the Marigold engine as early as possible in your app's lifecycle. Change the startEngine()
call to inform the engine you don't want to request authorization to display push notifications right away:
// Call startEngine and pass in MARPushAuthorizationOptionNoRequest for the authorization option
[[Marigold new] startEngine:APP_KEY withAuthorizationOption:MARPushAuthorizationOptionNoRequest error:NSError];
// Call startEngine and pass in MARPushAuthorizationOptionNoRequest for the authorization option
Marigold().startEngine(appKey: APP_KEY, withAuthorizationOption: .noRequest, error: NSError)
2. Requesting push permission from the user
When you are ready to ask the user for push permission (for example as part of the welcome experience), you can summon Apple's push dialog directly:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge + UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error != nil) {
// Handle errors here
}
}];
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in
if error != nil {
// Handle errors here
}
}
Updated 11 months ago