How to allow an App to send notification

Asked

Viewed 99 times

1

Hello,

I am developing an app and before the person login, I ask the user for permission to the location of the same, this is already ok, but I need to put also then a permission to send notifications and I’m not sure how to do. Could somebody give me a hand?

in that way:

inserir a descrição da imagem aqui

  • this warning is precisely for the app to send the device id to the server and the server uses a service similar to the lambda of Amazon to send notifications anyway you have to create within the app a receiver to receive the notifications and show on screen or make a certain action.

  • Post what you already have to the location. It gets easier to help you

1 answer

1

Whereas you skipped the Push Notification configuration step in the project to display the permission notice, add the command below in the . Swift you want to treat the warning, this can be appDelegate.Swift or some controller:

func registrarUsuarioParaPushNotifications() {
  UNUserNotificationCenter.current() // 1
    .requestAuthorization(options: [.alert, .sound, .badge]) { // 2
      granted, error in
      print("Permission granted: \(granted)") // 3
  }
}

Explanation of the above command:

  • Unusernotificationcenter handles all notifications related to App activities - Official documentation;

  • You invoke requestAuthorization(options:completionHandler:) to request authorization to display notifications. Past options indicate the type of notification you want your app to use. In the example, we are requesting alert, sound and badge - Official documentation

  • The Completion Handler receives a boleana in which it indicates if the authorship was successful. In this case, we are only printing the result;

If you need more details, I recommend reading this guide

Browser other questions tagged

You are not signed in. Login or sign up in order to post.