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.

810810

By default, Sailthru Mobile 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 Sailthru Mobile 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 STMPushAuthorizationOptionNoRequest for the authorization option
[[SailthruMobile new] startEngine:APP_KEY withAuthorizationOption:STMPushAuthorizationOptionNoRequest];
// Call startEngine and pass in STMPushAuthorizationOptionNoRequest for the authorization option
SailthruMobile().startEngine(appKey: APP_KEY, withAuthorizationOption: .noRequest)

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
  }
}

Did this page help you?