Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prompt the user for push notification permission after a button is pushed?

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?

like image 574
jfjldsfjljfkls Avatar asked Apr 26 '17 05:04

jfjldsfjljfkls


People also ask

When should I ask for push notification permissions?

You don't have to ask for push notification permissions. While Contacts/Locations are the dangerous permissions because you are accessing user data. So it is always needed to ask the user to allow it.

What is prompt for permission?

"Prompting" is asking user's permission to send them push notifications. Prompts are pop-up messages presented by the browser or mobile app and require the user to click "Allow" to be subscribed. If you have a mobile app, see our iOS Prompt Guide.

What is push message prompt?

A push message is a notification that pops up on your screen even when you're not using an app. Samsung push messages come up on your device in several ways. They display in your phone's notification bar, show application icons at the top of the screen, and generate text-based notification messages.


1 Answers

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.
like image 104
Pedro Castilho Avatar answered Sep 19 '22 13:09

Pedro Castilho