LogoLogo
  • Home
  • Integration & SDKs
    • Web / Browser Push
      • Wordpress Integration Steps
      • Wix Integration Steps
      • Safari
        • Safari on Desktop
        • Safari on Mobile (iOS / iPadOS)
      • SDK
        • SDK Methods
        • SDK Events
        • Customizing Prompt CSS
        • AMP Support
        • E-Commerce / Abandoned Cart
    • Native App Push
      • Apple / iOS
        • P8 Key or P12 Cert Setup
        • SDK: Swift / Obj-C
          • Activity Tracking
          • Deep Linking
          • Live Activities
          • App Messages
          • Identity Synchronization
          • SDK Methods
          • SDK Delegates
        • SDK: React Native
        • SDK: Flutter
        • Advanced
          • Self-Managed Integration
        • iOS SDK Changelog
      • Android
        • Firebase App Setup
        • SDK: Kotlin / Java
          • Deep Linking
          • Activity Tracking
          • App Messages
          • Identity Synchronization
          • SDK Methods
          • SDK Callbacks
          • Live Activities
        • SDK: React Native
        • SDK: Flutter
        • Advanced
          • Self-Managed Integration
        • Android SDK Changelog
      • React Native
        • Android: Firebase App Setup
        • iOS: P8 Key or P12 Cert Setup
        • SDK: React Native
          • Deep Linking
          • Activity Tracking
          • Subscriber Matching
          • Live Activities
          • App Messages
          • SDK Methods
      • Flutter
        • Android: Firebase App Setup
        • iOS: P8 Key or P12 Cert Setup
        • SDK: Flutter
          • Deep Linking
          • Activity Tracking
          • Subscriber Matching
          • Live Activities
          • App Messages
          • SDK Methods
  • API
    • API Access Management
    • API Reference
  • Platform
    • Dashboard
    • Notifications
      • A/B Testing
      • Custom Buttons
      • Notification Previews
      • Inline Segmentation
      • Notification Templates
      • Macros
      • Native App Push Notifications
    • App Messages
    • Segments
      • Custom Geo Segmentation
    • Campaigns
    • Insights
    • User Management
    • Organizations
      • Multi-Factor Authentication
      • Single Sign On
        • Google Workspace
        • Microsoft Azure AD
        • Okta Workforce
      • Multi-Domain Notifications
      • Multi-Domain Segments
      • API Access
    • Multi-Channel Notifications
  • Info Center
    • Launch Guide & Best Practices
      • Landing Domain Whitelist
    • Web / Browser Push
      • Common Questions
      • Browser Support
      • Retrieve Push User ID
      • Not Getting Prompted
      • Not Receiving Notifications
      • How to Unsubscribe
    • Native App Push
      • Retrieve Push User ID
    • Workflow Planning
Powered by GitBook
On this page
  • Prerequisites
  • Step 1: Import the PushSDK Framework
  • Step 2: Add the Notification Service Extension
  • Step 3: Add Capabilities to Primary Application Target
  • Step 4: Add Capabilities to The Notification Service Extension
  • Step 5: SDK Initialization
  • Step 6: Test the Implementation
  • Next Steps
  1. Integration & SDKs
  2. Native App Push
  3. Apple / iOS

SDK: Swift / Obj-C

PreviousP8 Key or P12 Cert SetupNextActivity Tracking

Last updated 3 months ago

The iOS (Swift / Obj-C) SDK is currently in closed beta. Contact your account manager for more information on becoming a beta partner.

Prerequisites

Before you can integrate the SDK with your app the following requirements must be met:

  • An iOS 11+ device (iPhone, iPad, or iPod Touch) to develop and test on.

    • Note: The XCode Simulator does not support remote push notifications. A physical device must be used.

  • A Mac OS device with XCode 14+ (Swift 5.7, if using Swift) installed.

  • An iOS P12 Push Certificate or P8 Key. If you do not already have these follow our documentation to generate them.

  • SDK Key: Obtained from the Pushly Platform in the Domain Settings page under the Overview > Details > Domain Information section.

Step 1: Import the PushSDK Framework

The PushSDK Framework is compatible with both Swift and Objective-C.

  • Select the root project and then your primary application target > General

  • Click + > Add Other > Add Package Dependency under Frameworks, Libraries, and Embedded Content

  • Search using the Package URL: https://github.com/pushly/push-sdk-ios

  • Set the Dependency Rule to: Range of Versions and 1.0.0 < 2.0.0

  • Click Add Package

  • Ensure the Pushly option is checked and click Add Package

  • Finally, add the Pushly library to the Primary Application Target

The PushSDK Framework is compatible with both Swift and Objective-C.

CocoaPods Link: https://cocoapods.org/pods/Pushly

Add the following to your Podfile:

target 'MyApp' do
  pod 'Pushly', '~> 1.1'
end

Then run a pod install inside your terminal, or from CocoaPods.app.

Step 2: Add the Notification Service Extension

The Notification Service Extension is required to enable rich notifications that support images and custom actions.

Within your app's XCode project select File > New > Target. Select Notification Service Extension inside the iOS templates tab and click Next.

Enter "NotificationServiceExtension" for the Product Name and any other configuration details for your app extension and then click Finish but do not click activate on the subsequent dialog.

Click Cancel on the dialoging prompting you to activate the service extension so that you can set the deployment target once the modal is closed.

Make sure to set your deployment target to the same as your primary application target. Unless you have a specific reason not to, you should set the Deployment Target to be iOS 11 which is the lowest supported version of iOS in the PushSDK Framework and in the most recent releases of XCode.

Next, add the Pushly library to your Notification Service Extension:

Click + under Frameworks and Libraries and select the already imported Pushly library.

Now, open the newly created Notification Service Extension and replace the code with the following:

import Pushly

class NotificationService: PNNotificationServiceExtension {
}
// Make sure to keep the header file import
#import "NotificationService.h"

@import Pushly;

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    [PNNotificationServiceExtensionHandler didReceiveExtensionRequest:request content:self.bestAttemptContent withContentHandler:contentHandler];
}

- (void)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.
    [PNNotificationServiceExtensionHandler didRecieveExtensionTimeWillExpire:self.bestAttemptContent withContentHandler:self.contentHandler];

    self.contentHandler(self.bestAttemptContent);
}

@end

You can ignore dependency errors, such as Module Pushly not found, at this point as they will be resolved during the next build.

Step 3: Add Capabilities to Primary Application Target

The Push Notifications capability should only be added to the primary application target.

Select the root project and then your primary application target > Signing & Capabilities

Click the + Capability and add Push Notifications.

Click the + Capability and add Background Modes.

After adding the Background Modes capability ensure that Remote Notifications are enabled.

Click the + Capability and add App Groups.

Click the + symbol located inside the App Groups section to add a new named container. To ensure that the PushSDK can properly capture information the container should be named group.{app-bundle-id}.push.

Check the box next to the newly created App Group in the primary application target.

If Xcode complains about the new app group at this point you may need to refresh the list after it is done provisioning.

Step 4: Add Capabilities to The Notification Service Extension

Select the root project and then the NotificationServiceExtension > Signing & Capabilities

Click the + Capability and add App Groups.

Finally, check the box next to the newly created App Group in the NotificationServiceExtension.

Step 5: SDK Initialization

First import the Pushly library in your AppDelegate

import Pushly
@import Pushly;

Add the following code to the AppDelegate's didFinishLaunchingWithOptions method of your application.

Replace the REPLACE_WITH_SDK_KEY in the setConfiguration method with the SDK Key from the platform settings page.

PushSDK.logLevel = .info
PushSDK.setConfiguration(appKey: "REPLACE_WITH_SDK_KEY", withLaunchOptions: launchOptions)

PushSDK.PushNotifications.showPermissionPrompt() { granted, settings, error in
    // optional callback
    print("User accepted permissions: \(granted)")
}
PushSDK.logLevel = PNLogLevelInfo;
[PushSDK setConfigurationAppKey:@"REPLACE_WITH_SDK_KEY" withLaunchOptions:launchOptions];

[PushSDKPushNotifications showPermissionPrompt:^(BOOL granted, UNNotificationSettings * _Nonnull settings, NSError * _Nullable error) {
    NSLog(@"User accepted permissions: %@", granted ? @"YES" : @"NO")
}];

Step 6: Test the Implementation

Run your app on a physical iOS device to make sure it builds correctly.

Note: The XCode Simulator does not support remote push notifications. A physical device must be used.

After accepting the dialog, log into the platform and navigate to Notifications > Create Notification and send your first notification, targeting the Native: iOS channel, to your device.

Next Steps

Once you have confirmed the SDK is working properly you may continue to add additional optional functionality like:

  • Handling notification opens / app links / deep linking

The code you added in the previous step will show the push permission dialog upon app open. This can be customized using to control when the dialog shows.

SDK methods
Attaching attributes to a subscriber's profile
Sending information about what content a subscriber interacts with