{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":"","apiSetting":null},"next":{"description":"","pages":[]},"title":"iOS: Delaying the Push Prompt","type":"basic","slug":"delaying-the-push-prompt","excerpt":"","body":"On iOS, apps can handle and display push alerts (including sounds and badges) only if the users expressed consent to receive them.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/ffb140a-IMG_1344.PNG\",\n        \"IMG_1344.PNG\",\n        810,\n        535,\n        \"#f7f8f8\"\n      ]\n    }\n  ]\n}\n[/block]\nBy 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.\n\nYou can prevent the default behavior by delaying the push prompt in the following way.\n\n# 1. Delaying the push prompt\n\nAs 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:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Call startEngine and pass in STMPushAuthorizationOptionNoRequest for the authorization option\\n[[SailthruMobile new] startEngine:APP_KEY withAuthorizationOption:STMPushAuthorizationOptionNoRequest];\",\n      \"language\": \"objectivec\",\n      \"name\": \"Objective-C\"\n    },\n    {\n      \"code\": \"// Call startEngine and pass in STMPushAuthorizationOptionNoRequest for the authorization option\\nSailthruMobile().startEngine(appKey: APP_KEY, withAuthorizationOption: .noRequest)\",\n      \"language\": \"swift\",\n      \"name\": \"Swift\"\n    }\n  ]\n}\n[/block]\n# 2. Requesting push permission from the user\n\nWhen 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:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];\\n  \\n  [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge + UNAuthorizationOptionAlert +  UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {\\n    if (error != nil) {\\n      // Handle errors here\\n    }\\n  }];\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"let center = UNUserNotificationCenter.current()\\ncenter.requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in\\n  if error != nil {\\n    // Handle errors here\\n  }\\n}\",\n      \"language\": \"swift\",\n      \"name\": \"Swift\"\n    }\n  ]\n}\n[/block]","updates":[],"order":6,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5e6156bf5e4a51006dcd815b","__v":1,"parentDoc":null,"version":{"version":"1.5","version_clean":"1.5.0","codename":"ST Rebrand","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["5e6156bf5e4a51006dcd8120","5e6156bf5e4a51006dcd8121","5e6156bf5e4a51006dcd8122","5e6156bf5e4a51006dcd8123","5e6156bf5e4a51006dcd8124","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","5e6156bf5e4a51006dcd8125","5e6156bf5e4a51006dcd8126","5af0fe494ca2730003cbc98a","5af0fe55ec80af0003804ca2","5e69868cbd5dcb006b35867b","5e6986ca2c6652006791b6e8"],"_id":"5e6156bf5e4a51006dcd818c","project":"55e67aaa9cc7c62b00c4a1ea","__v":2,"forked_from":"5b720760c44b7600034b7a08","createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z"},"project":"55e67aaa9cc7c62b00c4a1ea","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"iOS Techniques","slug":"ios-techniques","order":9999,"from_sync":false,"reference":false,"_id":"5e6986ca2c6652006791b6e8","createdAt":"2020-03-12T00:48:10.039Z","version":"5e6156bf5e4a51006dcd818c","project":"55e67aaa9cc7c62b00c4a1ea","__v":0},"createdAt":"2016-01-22T17:38:55.338Z","githubsync":"","user":"55d2bd8e2463351700f67dd7"}

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. [block:image] { "images": [ { "image": [ "https://files.readme.io/ffb140a-IMG_1344.PNG", "IMG_1344.PNG", 810, 535, "#f7f8f8" ] } ] } [/block] 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: [block:code] { "codes": [ { "code": "// Call startEngine and pass in STMPushAuthorizationOptionNoRequest for the authorization option\n[[SailthruMobile new] startEngine:APP_KEY withAuthorizationOption:STMPushAuthorizationOptionNoRequest];", "language": "objectivec", "name": "Objective-C" }, { "code": "// Call startEngine and pass in STMPushAuthorizationOptionNoRequest for the authorization option\nSailthruMobile().startEngine(appKey: APP_KEY, withAuthorizationOption: .noRequest)", "language": "swift", "name": "Swift" } ] } [/block] # 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: [block:code] { "codes": [ { "code": " UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];\n \n [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge + UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {\n if (error != nil) {\n // Handle errors here\n }\n }];", "language": "objectivec" }, { "code": "let center = UNUserNotificationCenter.current()\ncenter.requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in\n if error != nil {\n // Handle errors here\n }\n}", "language": "swift", "name": "Swift" } ] } [/block]