How to allow an App to send notification

0

Hello,

I'm developing an app and before the person's login, I ask the user for permission to locate it, this is already ok, but I need to also put a permission to send notifications and I'm not sure how do. Could someone help me?

this way:

    
asked by anonymous 28.11.2018 / 21:00

1 answer

0

Considering that you skipped the Push Notification configuration step in the project, in this way to display the permission notice add the command below in the .swift you want to handle 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 a>;

  • You request requestAuthorization (options: completionHandler:) to request permission to view 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 boolean indicating whether the authorization was successful. In this case, we're just printing the result;

If you need more details, please read this guide

    
20.12.2018 / 01:43