# Push Notifications Configuration

***

You can create and send event-based push notifications via Appflow\.ai without coding. Please, follow this doc to complete the one-time configuration.

## **Upload .p12 certificate to Appflow\.ai**

Appflow\.ai requires Apple Push Notifications service (APNs) with the .p12 certificate to send push notifications on the iOS platform, which is the unified key to send push notifications in sandbox and production.

{% hint style="warning" %}

#### <mark style="color:orange;">.p12 certificate uploaded to Appflow\.ai should be the same as the .p12 certificate in your app</mark>

{% endhint %}

### 1. Create a distribution certificate

a. Go to Apple Developer Center, navigate to [**Certificates, Identifiers & Profiles**](https://developer.apple.com/account/resources/certificates/list), in the **Certificates** section, click the "plus" button to create a new Distribution Certificate.

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FmMu1RnC51zoTkVeNktn0%2Fimage.png?alt=media&#x26;token=9ef328b3-602d-44e0-aa28-bc84291d09f8" alt=""><figcaption></figcaption></figure>

b. Choose **Apple Push Notification service SSL (Sandbox & Production)** in **Services**, then "Continue";

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FnT1MZxujNhPADDBwnJT1%2Fimage.png?alt=media&#x26;token=31342923-a8fe-484e-9888-86ba59b2962a" alt=""><figcaption></figcaption></figure>

c. Select the **App ID** to "Continue";

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2F1dI1cNKDHv70dNuE4am1%2Fimage.png?alt=media&#x26;token=6cbc4fa9-3d64-4d9f-95c4-af2cc61b7466" alt=""><figcaption></figcaption></figure>

Then, you will be asked to Upload a Certificate Signing Request (CSR) file to continue.

### 2. Create a Certificate Signing Request(CSR) file

a. Open KeyChain Access on your Mac\
b. Choose Keychain Access > Certificate Assistant > Request a Certificate from a Certificate Authority.

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FJT1ZzvyikXERy9yfpKFp%2Fimage.png?alt=media&#x26;token=d5a252fb-7efc-4599-8ae6-5301873a8658" alt=""><figcaption></figcaption></figure>

c. Fill info and "**Saved to disk**" to continue. You can learn more from this [Apple doc](https://developer.apple.com/help/account/create-certificates/create-a-certificate-signing-request):

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FJ9b0WLJtZf1I064Yprr5%2Fimage.png?alt=media&#x26;token=53e6d9b9-5fbe-456d-8398-62b760160370" alt=""><figcaption></figcaption></figure>

Now you have the *Certificate Signing Request (CSR) file* on your Mac.

### 3. Upload CSR file to continue

a. Back to **Apple Developer Center**, upload the *Certificate Signing Request (CSR) file* to continue.

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FNTPhrXyK6G6gaugxfqCY%2Fimage.png?alt=media&#x26;token=4c67118e-b142-49b6-9790-02ebb6b8f48e" alt=""><figcaption></figcaption></figure>

b. Download your Certificate, you will have an \_**aps.cer** \_file on your Mac

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FFWfGkXMgyuXpZkZatjgW%2Fimage.png?alt=media&#x26;token=38fb9cb7-96de-4ade-86fc-529bf56a21a2" alt=""><figcaption></figcaption></figure>

### 4. Convert **aps.cer** to **.p12** file:

a. double click "aps.cer" file to install in Keychain Access;\
b. if the certificate is not trusted, you need to trust the certificate.\
c. Export the file int&#x6F;**.p12** file,

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FWwwQSCTVWTgT6mO7ParF%2Fimage.png?alt=media&#x26;token=a38e18b5-7820-42dc-93a0-5cc312bd6718" alt=""><figcaption></figcaption></figure>

d. You'll be asked "Enter a password",**Please DO NOT enter a password here**, just click "OK" to proceed. Then the .p12 file is saved on your Mac.

### 5. Upload .p12 file to Appflow\.ai

Go to Appflow\.ai Dash => Settings => Applications => Select the App to **Edit**

Now you can upload the **.p12** file to Appflow\.ai.

***

## **Configure Notifications on Your App**

### 1. Capabilities

You need to enable Push Notifications services on your project:

a. Your App Target => Signing & Capabilities => click " + Capabilities"

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FoSfylpxbQB0xGjOBLtuH%2Fimage.png?alt=media&#x26;token=ecb07cee-56c3-40ff-a24c-d99cd3775717" alt=""><figcaption></figcaption></figure>

b. Double click to add "Push Notification"

<figure><img src="https://1902080327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUSTYqtR8RNNkx3qqWf9%2Fuploads%2FXnANAgqa8rQqAiKmGMzG%2Fimage.png?alt=media&#x26;token=1ef176f9-e874-4e9b-a07c-3f267ac1e817" alt=""><figcaption></figcaption></figure>

### 2. Request PushNotifications Permissions in AppDelegate

```swift
private func requestPushNotificationsPermissions() {
    let userNotificationCenter = UNUserNotificationCenter.current()
    userNotificationCenter.requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, error in
        print("Permission granted: \(granted)")
        if granted {
            self?.getNotificationSettings()
        }
    }
}
```

### 3. Permission request completed, get notification settings

```swift
private func getNotificationSettings() {
    UNUserNotificationCenter.current().getNotificationSettings { settings in
        print("Notification settings: \(settings)")
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}
```

### 4. Successfully register APNs and report DeviceToken

```swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let deviceToken = deviceToken.reduce("", { $0 + String(format: "%02X", $1) })
    Appflow.shared.purchases.uploadDeviceInfo(deviceToken: deviceToken) { _, _ in
    }
}
```

Congrats! You've finished the remote Push Notifications configuration on Appflow\.ai. You can create push notifications in: Appflow\.ai Dash => Experiments => Push Notifications.
