8

I read How to control when to prompt user for push notification permissions in iOS and similar questions but they weren't written in Swift 3.

I have read Registering for Push Notifications in Xcode 8/Swift 3.0? five times.

I ran my app in Simulator over and over but after the first time, the app never prompted the user for push notification permission.

I used the code from Registering for Push Notifications in Xcode 8/Swift 3.0? and kept trying different answers and moving the code to different places so that the user would be asked for permission only after they pressed a certain button, and not immediately when the app launched.

Do I have to be registered with an Apple ID etc. for the push notification code to even work in Simulator?

Community
  • 1
  • 1
jfjldsfjljfkls
  • 81
  • 1
  • 1
  • 5

1 Answers1

3

First off, you should only prompt your user once on whether they want to get push notifications or not. So tying that to a button and trying to run it every time is a bad UX decision. Only tie your Push Notification prompt to a button if the button actually does more stuff which would require Push Notifications to be enabled.

To register your user for push notifications, you should use the UIApplication registerForRemoteNotifications method. When this method runs, your user will be prompted on whether they want to receive push notifications - but only once! The setting they set will be considered definitive and your user will not be prompted again. If they want to start receiving push notifications, they have to change that in the app's settings. This is expected behavior and is how you should do it.

If you want to prompt the user only when they press a button, then remove any calls to registerForRemoteNotifications from your app's startup code and call the method from the action tied to your button instead.

If you want to reset the push notification permissions for testing purposes, this is how Apple says it's possible:

Resetting the Push Notifications Permissions Alert on iOS

The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

If you want to simulate a first-time run of your app, you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by following these steps:

  1. Delete your app from the device.
  2. Turn the device off completely and turn it back on.
  3. Go to Settings > General > Date & Time and set the date ahead a day or more.
  4. Turn the device off completely again and turn it back on.
Community
  • 1
  • 1
Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
  • 1
    "First off, you should only prompt your user once on whether they want to get push notifications or not. So tying that to a button and trying to run it every time is a bad UX decision. " Absolutely! I wasn't trying to run it each time the app launches, it's part of the initial onboarding experience. "to prompt the user only when they press a button...call the method from the action tied to your button." Would that mean putting the code that I would put in the App Delegate in the View Controller instead? I tried that but it still asked for permission as soon as the app opened. – jfjldsfjljfkls Apr 26 '17 at 18:32
  • It might be asking based on the notification types specified in your `Info.plist`. It's strange, though, because every single available resource seems to indicate that the push notification prompt should only appear when you call `registerForRemoteNotifications`. Unfortunately, I can't test this right now. – Pedro Castilho Apr 26 '17 at 18:42
  • 2
    I disagree, maybe the first time, I don't want push notifications, but after a while I might change my mind. Do I have to reinstall the app just to enable push notifications again? Not really, I might just want to press a button to prompt me to activate them again – Denny Apr 08 '19 at 13:46
  • The user can reenable push notifications from the "Settings" app on their iPhone. – Pedro Castilho Apr 09 '19 at 02:49
  • 1
    It's far more convenient to do this without switching apps – Denny Apr 09 '19 at 08:08
  • Well, that's a valid opinion. – Pedro Castilho Apr 25 '19 at 17:06
  • That would be nice @Denny, but Apple doesn't give you the option to ask again, "just in case they change their mind". I assume due to abuse from apps built by "dollar comes first" companies. Better to do a pre-ask alert, and let them say "yes" or "later" to that so you don't waste your one and only option to ask. – eric Aug 20 '19 at 17:35