Push.Express Documentation
  • Push.Express Documentation
  • Quckstart
    • How to use
    • Analytics
    • Demo apps
  • SDK
    • PWA
    • Kotlin
    • Swift
    • Unity
    • Flutter
    • Onesignal
  • API
    • Applications
    • App Instances (Devices)
    • Pushes
    • One-time sending in future
    • Smart weekly sending
    • Instant sending
  • SDK API
    • SDK API specs
Powered by GitBook
On this page
  • PushExpress -- SDK Swift
  • Create application in Push.Express
  • Add sdk in your application
  • Sandbox or Production mode
  1. SDK

Swift

PreviousKotlinNextUnity

Last updated 27 days ago

PushExpress -- SDK Swift

Create application in Push.Express

  1. Go to your account

  2. Open existing App settings or create a new App

  3. Switch type application iOS

  4. Enter the Bundle Identifier of your application in the Bundle ID field

You can view the Bundle Identifier of your application in the XCode TARGETS of your application > General tab in the Identity section, you will see the Bundle Identifier field.

  1. Enter your Team ID in the Team ID field.

    You can view your Team ID in the XCode TARGETS of your application > Signing & Capabilities tab in the Signing section, you will see the Team field.

  2. Enter your ID APNs Authentication Key in the Key ID field.

7. Enter your Private key APNs Authentication Key from the file .p8 in the Private key field.

You can created Private key APNs Authentication Key in your Apple Developer Account > Certificates, Identifiers & Profiles > Keys section

Add sdk in your application

  1. Add the SDK to Xcode using Swift PackageManager. To do this, go to File > Add Packages... > Enter the repository URL SDK (GitHub) https://github.com/pushexpress/pushexpress-swift-sdk.git > Click the button Add Package

  2. Add Remote Notifications in Background Modes. To do this, go to the XCode TARGETS of your application > Signing & Capabilities > press the button + Capability > select Background Mode from the list > check the box Remote Notifications.

  3. Enable Push Notifications in Xcode. Go to the XCode TARGETS of your application > Signing & Capabilities > press the button + Capability > in the list, find and select Push Notifications.

  4. Add sdk initialization to AppDelegate.swift

   //
//  AppDelegate.swift
//
//

import Foundation
import UIKit
import SdkPushExpress

class AppDelegate: NSObject, UIApplicationDelegate {
    private let PUSHEXPRESS_APP_ID = "1234-23" // set YOUR OWN ID from Push.Express account page
    private var myOwnDatabaseExternalId = ""      // you can just leave it empty in most cases
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // Initialize SDK with PushExpress Application ID
        
        // For minimal initialization (without requesting notification perms
        // and registering for remote notifications) set 'essentialsOnly: true'
        try! PushExpressManager.shared.initialize(appId: PUSHEXPRESS_APP_ID, essentialsOnly: true)
        
        // To call only notification perms request, set 'registerForRemoteNotifications: false'
        // Do not call this if you will request permissions by yourself
        PushExpressManager.shared.requestNotificationsPermission(registerForRemoteNotifications: true)
        
        // DO NOT SET UNLESS YOU ARE 100% SURE WHAT THAT IS!!!
        /*
        myOwnDatabaseExternalId = "my_external_user_id:321"
        PushExpressManager.shared.tags["audiences"] = "my_aud_123"
        PushExpressManager.shared.tags["webmaster"] = "webmaster_name"
        PushExpressManager.shared.tags["ad_id"] = "advertising_id"
        PushExpressManager.shared.tags["my_custom_tag"] = "my_custom_value"
        */
        
        // You can also disable notifications while app is on screen (foreground)
        /*PushExpressManager.shared.foregroundNotifications = false*/
        
        // Activate SDK (starting all network activities)
        try! PushExpressManager.shared.activate(extId: myOwnDatabaseExternalId)
        print("externalId: '\(PushExpressManager.shared.externalId)'")
        
        // If and only if you want to use same app for multiple users
        // call .deactivate() first, than activate() with new extId
        // Better talk with our support, we always ready to help =)
        /*try! PushExpressManager.shared.deactivate()*/
        
        if !PushExpressManager.shared.notificationsPermissionGranted {
            // show your custom message like "Go to iOS Settings and enable notifications"
        }
        
        return true
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
        PushExpressManager.shared.transportToken = tokenParts.joined()
    }
}
  1. Add the Notification Service Extension to the Xcode project, which will allow you to modify push notifications before displaying them. To do this, File > New > Target > In the window that opens, select Notification Service Extension (under iOS → App Extensions) > Click Next > Specify the extension name (for example, NotificationService) > Click Finish > activate Notification Service Extension

  2. Add sdk in TARGETS of your Notification Service Extension > Build Phases > Link Binary With Libraries section. Press " + " and enter the link to the push express sdk in the search box > select SdkPushExpress > click Add

  3. Add the sdk code to Notification Service Extension.

   //
//  NotificationService.swift
//  NotificationService
//
//

import UserNotifications
import SdkPushExpress

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    let notificationServiceManager = NotificationManager()

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        notificationServiceManager.handleNotification(request: request, contentHandler: contentHandler)
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

Sandbox or Production mode

  1. Sandbox (Debug): Used when developing and testing an application via Xcode. The application runs on a device connected to Xcode. If you are using this mode of operation of the application, then set the Sandbox switch in the application settings of your personal account Push Express.

  1. Production (Release): Used when the app is published in the App Store or TestFlight.If you are using this mode of operation of the application, then set the Production switch in the application settings of your personal account Push Express.

Create an application (App ID). Go to > go to the Identifiers section > create a new ID for your app (App ID) > enable the option Push Notifications.

To configure the sdk, use the following parameters: PUSHEXPRESS_APP_ID - The application ID in the Push service.Express, displayed in the section . myOwnDatabaseExternalId - You can use this parameter to set your own device ID. PushExpressManager.shared.tags - Use tags to set additional attribution. Use the PushExpressManager.shared.tags["audiences"] tag to transfer the segment code, and Push Express Manager.shared.tags["webmaster"] to transfer the webmaster code. PushExpressManager.shared.activate и PushExpressManager.shared.deactivate - Activate and deactivate the sdk at the right moment.

It is worth considering in which environment your application was launched. Sandbox or Production APNs tokens are not interchangeable. APNs tokens are not generated on Xcode simulators. This is an Apple limitation because simulators do not support registration for remote notifications. Only real devices can request and receive APNs tokens. Make sure that the devices have appeared in the Push Express service in the and each device has a Token field filled in.

Apple Developer - Certificates, Identifiers & Profiles
Applications
devices
Example Application with Swift SDK
Push.Express
Create application in Push.Express
Add sdk in your application
Sandbox or Production mode