# Push Messaging

## Requirements

### iOS Requirements

iOS 11+ or iPadOS 11+ device (iPhone, iPad, iPod Touch) to test on. Xcode 14+ simulator works running iOS 16+

## Additional: Add NotificationServiceExtension

Within your project's iOS folder, open the `.xcworkspace` file in Xcode.

Select `File` > `New` > `Target`

Select `Notification Service Extension` and press `Next`

<figure><img src="/files/N04oN2Ghx1Mme64ewhlc" alt=""><figcaption></figcaption></figure>

Enter the Product Name (e.g `NotifcationExtension` ) and Language to **Swift** (if you need to use Objective-C that is ok).

Then click **Finish**. Do not select "Activate" on the dialog shown after selecting "Finish".

Press `Cancel` on the `Activate Scheme` prompt. By canceling, you are keeping Xcode set to debug your app instead of the extension. If you selected Activate by accident, you can simply switch back to debug your app in Xcode (next to the Play button).

Open project with Xcode, in the Project Navigator, select the top-level project directory and select the NotificationExtension target. Ensure the Deployment Target is set to the same value as your Main Application Target. Unless you have a specific reason not to, you should set the Deployment Target to be iOS 10 which is the version of iOS that Apple released Rich Media for push. iOS versions under 10 will not be able to get Rich Media.

## Step 1. Add SDK

### **CocoaPods**

**Ensure your installed Cocoapods version is 1.12.1 or newer!**

Make sure your current Xcode project is closed and in the project root, run `sudo gem install cocoapods`.

Run `pod init` from the terminal in your project directory.

Open the newly created `Podfile` with your favorite code editor.

Add the OneSignal dependency under your project name target as well as

`NotificationExtension` target like below.

<pre class="language-ruby"><code class="lang-ruby"><strong>target 'your_project_name' do
</strong> #use_frameworks!
  pod 'AntsomiFramework'
end

target 'NotificationExtension' do
 #use_frameworks!
  pod 'AntsomiFramework'
end
</code></pre>

Run the following commands in your terminal in your project directory.

`pod repo update`

`pod install`

Open the newly created `<project-name>.xcworkspace` file.

## Step 2. Add capabilities

2.1 In the main app target, select Signing & Capabilities > All > + Capability, enable Push Notifications.

<figure><img src="/files/BayBRcF94Kbr8dm0pKkn" alt=""><figcaption></figcaption></figure>

2.2 Next, enable Background Modes and check Remote Notifications.

<figure><img src="/files/iOwMts325fqYtLq89vIT" alt=""><figcaption></figcaption></figure>

Check the boxes below:

<figure><img src="/files/hdmDdeiLmG87j50gAShu" alt=""><figcaption></figcaption></figure>

## Step 3. Add App Group to your app

**3.1** Go to your main app target, select Signing & Capabilities > All > + Capability, search for App Group

<figure><img src="/files/qksCZbEdfxHJcsQMZrZ9" alt=""><figcaption></figcaption></figure>

**3.2** Under App Groups click the "+" button

**3.3** Set the "App Groups" container identifier.

**3.4** Go to the NotificationExtension Target, select **Signing & Capabilities** > **All** > **+ Capability**, search for **App Group** and add App Group to your NotificationExtension target. Check the app group id which you use on your main app target. **Make sure NotificationServiceExtension and your app have the same App Group.**

## Step 4. Initialization

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

```swift
import UIKit
import AntsomiFramework
import UserNotifications
import os.log

@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    let antsomiUNUserNotificationCenter = AntsomiUNUserNotificationCenter()
    let trackEvent = CDPEvent(ec: "<event-category>", ea: "<event-action>")


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
	       
        //Active Antsomi SDK with config
        let config = Antsomi.Configuration(portalId: "<your-portal-id>", propsId: "<your-props-id>", appGroupId: "<your-app-group-id>")
        Antsomi.shared.activate(with: config);
        
        //Active app inbox, listen to incoming message
        Antsomi.shared.setCustomerProperties(customerID: "<customer-id>")
        
        //Track app, ask user permission to show notification
        Antsomi.shared.trackAppLaunch()
        Antsomi.shared.requestNotificationPermission()
        
        UNUserNotificationCenter.current().delegate = antsomiUNUserNotificationCenter
        
        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Register for remote notification       
        Antsomi.shared.registerForNotification(deviceToken)
    }

}
```

```swift
import Foundation
import UserNotifications
import AntsomiFramework

class NotificationService: AntsomiNotificationService {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        super.didReceive(request, withContentHandler: contentHandler)

    }

}
```

{% endtab %}

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

```objectivec
#import "AppDelegate.h"
#import <AntsomiFramework/AntsomiFramework-Swift.h>

@interface AppDelegate ()

@property (nonatomic, strong) AntsomiUNUserNotificationCenter *antsomiUNUserNotificationCenter;
@property (nonatomic, strong) CDPEvent *trackEvent;

@end
```

```objectivec
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //Active Antsomi SDK with config
    Antsomi_Configuration *config = [[Antsomi_Configuration alloc] initWithPortalId:@"<your-portal-id>"
                                                                            propsId:@"<your-props-id>"
                                                                        appGroupId:@"<your-app-group-id>"];
    [Antsomi.shared activateWithConfiguration:config];

    //Active app inbox, listen to incoming message
    [Antsomi.shared setCustomerPropertiesWithCustomerID:@"<customer-id>"];
    [Antsomi.shared appInboxInit];

    //Track app, ask user permission to show notification
    [Antsomi.shared trackAppLaunch];
    UNUserNotificationCenter.currentNotificationCenter.delegate = self.antsomiUNUserNotificationCenter;

    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //Register for remote notification
    [Antsomi.shared registerForNotificationWithDeviceToken:deviceToken];
}
@end

```

{% endtab %}
{% endtabs %}

## Step 5. Send your first Notification


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.antsomi.com/developers-guide/ios/push-messaging.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
