# Push setup

### Setting up the Push

First of all, open your project in Xcode by clicking on it in the sidebar, then click on the `Signing & Capabilities` tab. If `Push Notifications` isn't already there, click on `+ Capability` and pick `Push Notifications`. If there is a `Fix` button shown, press it.

![Xcode Capabilities](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2F5rD6R07dV3ZbcDkknfKb%2Fpush_setup_ios_xcode_capabilities.png?alt=media\&token=9bd34ecc-40d1-4225-a5ea-73f4e448e13e)

Then, you need to add a few lines to your app delegate in order to receive push notifications.

{% tabs %}
{% tab title="Swift" %}
{% code title="AppDelegate.swift" %}

```swift
import Batch

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Start Batch.
    BatchSDK.start(withAPIKey: "YOUR_API_KEY")
    
    // Ask for the permission to display notifications
    // The push token will automatically be fetched by the SDK
    BatchPush.requestNotificationAuthorization()
    
    // Alternatively, you can call requestNotificationAuthorization later
    // But, you should always refresh your token on each application start
    // This will make sure that even if your user's token changes, you still get notifications
    // BatchPush.refreshToken();
    
    // Sets Batch as your UNUserNotificationCenterDelegate.
    // This will disable the legacy callbacks on your app delegate (didReceiveRemoteNotification, ...).
    // If you rely on those, do not add this line and please consider migrating to the UserNotification framework.
    //
    // If you already have a UNUserNotificationCenterDelegate implementation, do not add this line.
    // Instead, add Batch's callbacks in your implementation. See 'Advanced > Intercepting notifications', 
    BatchUNUserNotificationCenterDelegate.registerAsDelegate()

    return true
}
```

{% endcode %}
{% endtab %}

{% tab title="Objective-C" %}
{% code title="AppDelegate.m" %}

```objectivec
@import Batch;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {  
    // Start Batch.
    [BatchSDK startWithAPIKey:@"YOUR_API_KEY"]; 
 	
    // Ask for the permission to display notifications
    // The push token will automatically be fetched by the SDK
    [BatchPush requestNotificationAuthorization];

    // Alternatively, you can call requestNotificationAuthorization later
    // But, you should always refresh your token on each application start
    // This will make sure that even if your user's token changes, you still get notifications
    // [BatchPush refreshToken];

    // Sets Batch as your UNUserNotificationCenterDelegate.
    // This will disable the legacy callbacks on your app delegate (didReceiveRemoteNotification, ...).
    // If you rely on those, do not add this line and please consider migrating to the UserNotification framework.
    //
    // If you already have a UNUserNotificationCenterDelegate implementation, do not add this line.
    // Instead, add Batch's callbacks in your implementation. See 'Advanced > Intercepting notifications', 
    [BatchUNUserNotificationCenterDelegate registerAsDelegate];

    return YES;
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note:** If you already implement an `UNUserNotificationCenterDelegate` class, please read the [intercepting notifications](https://doc.batch.com/developer/sdk/ios/advanced/intercepting-notifications) documentation to properly integrate Batch into it.
{% endhint %}

#### Requesting notification permission

To ask for the permission to display notifications and register the current device for push you can use one of the following APIs:

{% tabs %}
{% tab title="Swift" %}

```swift
    // Ask for the permission to display notifications
    // The push token will automatically be fetched by the SDK
    BatchPush.requestNotificationAuthorization()
    
    // Ask for the permission to display notifications with a completion handler.
    // The push token will automatically be fetched by the SDK
    BatchPush.requestNotificationAuthorization { success, error in
      // Handle permission result
    }

    // Ask for the provisionnal permission to display notifications
    // Notifications will NOT be displayed on the lock screen, or as a banner when the phone is unlocked.
    // They will directly be sent to the notification center, accessible when the user swipes up on the lockscreen, or down
    // The push token will automatically be fetched by the SDK.
    BatchPush.requestProvisionalNotificationAuthorization()
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
    // Ask for the permission to display notifications
    // The push token will automatically be fetched by the SDK
    [BatchPush requestNotificationAuthorization];
    
    // Ask for the permission to display notifications with a completion handler.
    // The push token will automatically be fetched by the SDK
    [BatchPush requestNotificationAuthorizationWithCompletionHandler:^(BOOL granted, NSError * _Nullable error) {
        // Handle permission result
    }];    
    // Ask for the provisionnal permission to display notifications
    // Notifications will NOT be displayed on the lock screen, or as a banner when the phone is unlocked.
    // They will directly be sent to the notification center, accessible when the user swipes up on the lockscreen, or down
    // The push token will automatically be fetched by the SDK.
    [BatchPush requestProvisionalNotificationAuthorization];
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note:** It's preferable to make the request in a context that helps people understand why your app needs authorization.
{% endhint %}

### Your first notification

**1. Obtaining your device token**

You can find your device's token using the [debug tool](https://doc.batch.com/developer/sdk/ios/profile-data/debug) or locating the token Batch posts to the Xcode console:

```
[Batch] - Push token (Apple Push Production): <push token>
```

Based on your *Provisioning Profile*, the token shown in the console will be **Development** *("Sandbox/Development")* or **Production** *("Production")*.

If you don't see a push token, there might be an error in the logs describing what happened. See our [troubleshooting documentation](https://doc.batch.com/developer/sdk/ios/troubleshooting) for more info.

{% hint style="info" %}
**Note:** Push notifications in an iOS Simulator are only supported when using an iOS 16 or higher simulator on macOS 13 on a T2 or Apple Silicon mac. Older simulators, macOS versions or computers do not support Push in the simulator: please use a physical device.
{% endhint %}

**2. Obtaining your Installation ID**

You can then retrieve the Installation ID, which represents an installation of your app on a device, by calling the following methods:

{% tabs %}
{% tab title="Swift" %}

```swift
BatchUser.installationID()
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[BatchUser installationID];
```

{% endtab %}
{% endtabs %}

While Batch prints this in the debug console on start, displaying it in a settings or about page enables users to send you this identifier. This is useful for debugging, sending test notifications, etc.

**3. Sending a test push notification**

Batch enables you to send a test notification to the application installation currently running on your device.

To do so, open the dashboard and go to ⚙ Settings → Debug. Enter your Installation ID, hit `Debug` and then click on **"Send Test Push"**.

![Send Test from Debug](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2FetDbxbYjbuHkX59eKJ63%2Fdebug_send_test.png?alt=media\&token=434e31b5-5e0a-43f8-8dcb-2e4777787a79)

You should receive a notification on your device. If not, or if you can't find your Installation ID, the SDK might not be properly configured.

If you need to send push notifications to your device on a regular basis, then you should add your ID as a test device by clicking the **"Save as a test device"** button.

{% hint style="info" %}
**Troubleshooting**\
If you're having trouble sending test notifications, you can check our [troubleshooting documentation](https://github.com/BatchLabs/product.tech-documentation-gitbook/blob/master/ios/troubleshooting/README.md#_test-push-issues).
{% endhint %}

#### What's next

*Congratulations on finishing the integration of Batch Push!*

Here are a couple of extra steps you can take before releasing your app:

* **Rich notifications**: Add support for iOS [rich push notifications](https://doc.batch.com/developer/sdk/ios/sdk-integration/rich-notifications-setup).
* **Mobile Landings**: Make sure [Mobile Landings](https://doc.batch.com/developer/sdk/ios/mobile-landings) are set up correctly.
* **Custom user identifier**: Add support for [custom user identifiers](https://github.com/BatchLabs/product.tech-documentation-gitbook/blob/master/ios/custom-data/customid/README.md) if you are planning to use the [Transactional](https://doc.batch.com/developer/api/mep/transactional/send) or the [Profile](https://doc.batch.com/developer/api/cep/profiles) APIs.
* **Analytics**: Add an [event dispatcher](https://doc.batch.com/developer/sdk/ios/event-dispatchers) to automatically track your campaigns in your third-party analytics tool.
* **Universal Links**: [Declare your Universal Links](https://doc.batch.com/developer/sdk/advanced/deeplinking#universal-links) domains to Batch to open then inside of your app.
