# Add Push Notification to your app

The first thing you have to do is implement Push Notifications using the [UserNotifications](https://developer.apple.com/documentation/usernotifications) framework.

{% hint style="info" %}
You will be guided through all the steps needed to have a functional project with the push notifications, if you have already done it you can skip to the [Integrate MPush](https://docs.mpush.cloud/ios-sdk/integrate-mpush) section of this README.
{% endhint %}

## Create a key <a href="#create-a-key" id="create-a-key"></a>

Go to [developer.apple.com](https://www.developer.apple.com/) with an admin account and under Keys -> All click the plus button in the top right corner.&#x20;

Specify a name for your key and enable Apple Push Notifications service (APNs).

&#x20;

![Key creation 2](https://docs.mumbleideas.it/mpush/ios/images/Key-creation-2.png)

![Key creation 1](https://docs.mumbleideas.it/mpush/ios/images/Key-creation-1.png)

Then download the .p8 key file created and upload it in our dashboard.

{% hint style="info" %}
Note that the key created is valid for all the apps of your profile and can't be re-downloaded, keep it in a safe place because you will likely have to resue it.
{% endhint %}

## Add notifications to your app <a href="#add-notifications-to-your-app" id="add-notifications-to-your-app"></a>

Now go to your app settings under Identifier -> AppId and enable the notifications services following the steps. After that, you need to update the Provisioning Profile for your app or create a new one because push notifications don't work for applications signed with a wildcard Provisioning Profile.

Now it's finally time to move to XCode. Open your project and enable push notifications in the capabilities tab.

<div align="center"><img src="https://docs.mumbleideas.it/mpush/ios/images/XCode-capabilities.png" alt="XCode-capabilities"></div>

In `AppDelegate.swift` add

```bash
import UserNotifications

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    ...

    let userNotificationCenter = UNUserNotificationCenter.current()
    userNotificationCenter.delegate = self

    self.registerForPushNotifications()
    ...
}

// MARK: - Notifications

func registerForPushNotifications() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
        guard granted else { return }
        self.getNotificationSettings()
    }
}

func getNotificationSettings() {
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler
                                completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler(UNNotificationPresentationOptions.alert)
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler
                                completionHandler: @escaping () -> Void) {
        completionHandler()
    }
}
```
