{"_id":"5b720760c44b7600034b79e5","__v":1,"parentDoc":null,"version":{"_id":"5b720760c44b7600034b7a08","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"forked_from":"5b1f2cbdfd653400031d8d9f","createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z","categories":["5b720760c44b7600034b79a7","5b720760c44b7600034b79a8","5b720760c44b7600034b79a9","5b720760c44b7600034b79aa","5b720760c44b7600034b79ab","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","5b720760c44b7600034b79ac","5b720760c44b7600034b79ad","5af0fe494ca2730003cbc98a","5af0fe55ec80af0003804ca2"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"API V6","version_clean":"1.4.0","version":"1.4"},"project":"55e67aaa9cc7c62b00c4a1ea","category":{"_id":"5b720760c44b7600034b79ac","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"version":"5b720760c44b7600034b7a08","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-02-08T21:31:11.878Z","from_sync":false,"order":6,"slug":"advanced-techniques","title":"Advanced Techniques"},"githubsync":"","user":"55d2bd8e2463351700f67dd7","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-22T17:38:55.338Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","apiSetting":null,"auth":"required","params":[],"url":""},"isReference":false,"order":16,"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, Carnival will ask the user to opt-in for push notifications as soon as you call `Carnival.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 Carnival engine as early as possible in your app's lifecycle. Change the `Carnival.startEngine()` call to inform the engine you don't want to register the user for push notifications right away:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Carnival.startEngine(appKey: APP_KEY, registerForPushNotifications: false)\\n\\nif UIApplication.shared.isRegisteredForRemoteNotifications {\\n  // Register for push notifications here\\n}\",\n      \"language\": \"swift\",\n      \"name\": \"Swift\"\n    },\n    {\n      \"code\": \"// Call startEngine and pass in NO for registerForPushNotifications\\n[Carnival startEngine:APP_KEY registerForPushNotifications:NO];\\n\\n// Ensure that push notifications are requested *IF* the user has accepted this in the past\\nif ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {\\n    // Register for push notifications in this block      \\n\\t\\t// Apple recommends to request a new push token every launch\\n\\t\\t// https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW14\\n}\",\n      \"language\": \"objectivec\",\n      \"name\": \"Objective-C\"\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 expedience), you can summon Apple's push dialog directly:\n[block:code]\n{\n  \"codes\": [\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      \"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}\n[/block]","excerpt":"","slug":"delaying-the-push-prompt","type":"basic","title":"iOS: Delaying the Push Prompt"}

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, Carnival will ask the user to opt-in for push notifications as soon as you call `Carnival.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 Carnival engine as early as possible in your app's lifecycle. Change the `Carnival.startEngine()` call to inform the engine you don't want to register the user for push notifications right away: [block:code] { "codes": [ { "code": "Carnival.startEngine(appKey: APP_KEY, registerForPushNotifications: false)\n\nif UIApplication.shared.isRegisteredForRemoteNotifications {\n // Register for push notifications here\n}", "language": "swift", "name": "Swift" }, { "code": "// Call startEngine and pass in NO for registerForPushNotifications\n[Carnival startEngine:APP_KEY registerForPushNotifications:NO];\n\n// Ensure that push notifications are requested *IF* the user has accepted this in the past\nif ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {\n // Register for push notifications in this block \n\t\t// Apple recommends to request a new push token every launch\n\t\t// https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW14\n}", "language": "objectivec", "name": "Objective-C" } ] } [/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 expedience), you can summon Apple's push dialog directly: [block:code] { "codes": [ { "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" }, { "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" } ] } [/block]