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

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.

target 'your_project_name' do
 #use_frameworks!
  pod 'AntsomiFramework'
end

target 'NotificationExtension' do
 #use_frameworks!
  pod 'AntsomiFramework'
end

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.

2.2 Next, enable Background Modes and check Remote Notifications.

Check the boxes below:

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

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

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()
        UNUserNotificationCenter.current().delegate = antsomiUNUserNotificationCenter
        
        return true
    }

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

}
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)

    }

}

Step 5. Send your first Notification

Last updated