Push Notifications for iOS apps
Add Apptics framework to your Apple project
By now, you would have already integrated Apptics with your Apple project. If not, refer to the integration guide.
- Open the .xcworkspace file and make sure you have enabled push notification and app group identifier in your application.
- Go to your root project in Xcode and select the main app target. Navigate to the Signing & Capabilities tab.
- If push notifications are not enabled, click + Capability and add push notifications.
- Click + Capability again and add Background modes.
- Also, enable the remote notifications.
Add notification service extension
The AppticsNotificationServiceExtension enables your iOS app to receive the rich notifications with images, buttons, and badges. It is also essential for Apptics' confirmed devliery analytics stats.
- In Xcode, navigate to File > New > Target.
- Select the Notification Service Extension and click Next.
- Give a name to the NotificationServiceExtension and click Finish.
- When prompted to activate the scheme after selecting Finish, click Cancel to avoid activating it.
- In Xcode, select the NotificationServiceExtension target.
- Go to general settings. Set the minimum deployment to match your main application target. This should be iOS 14.5 or higher.
Add app groups
App groups enable your app and the NotificationServiceExtension to share data when a notification is received, even if the app is not running. This is essential for implementing the badges and confirmed deliveries.
- Select your main app target in Xcode.
- Go to Signing & Capabilities and click on + Capability.
- Choose App groups.
- Click on + to add a new group.
- Set the app groups container name to 'group.MAIN_BUNDLE_IDENTIFIER.apptics', where the MAIN_BUNDLE_IDENTIFIER should match the bundle identifier of your main application.
- Click OK to save the app group for your main app target. Repeat the steps for the NotificationServiceExtension Target.
- Select the NotificationServiceExtension Target > Signing & Capabilities > + Capability > App groups.
- In app groups, click + button to add a new group.
- Set the app group container to 'group.MAIN_BUNDLE_IDENTIFIER.apptics', making sure NOT to include 'NotificationServiceExtension' in the name.
- Replace the MAIN_BUNDLE_IDENTIFIER with the bundle identifier of your main application.
Add AppticsMessaging and AppticsNotificationServiceExtension framework
- Install the Apptics iOS SDK with Cocoapod.
- Specify pod 'AppticsMessaging and AppticsNotificationServiceExtension' in your podfile.
- The podfile will look something similar to the one shown below.
Copiedsource 'https://github.com/CocoaPods/Specs.git'
target 'MAIN TARGET' do
pod 'Apptics-SDK'
pod 'AppticsMessaging'
# Pre build script will register the app version, upload dSYM file to the server and add apptics specific information to the main info.plist which will be used by the SDK.
script_phase :name => 'Apptics pre build', :script => 'sh "./Pods/Apptics-SDK/scripts/run" --upload-symbols-for-configurations="Release, Appstore" --app-group-identifier="group.MAIN_BUNDLE_IDENTIFIER.apptics"', :execution_position => :before_compile
end
target 'NOTIFICATION EXTENSION TARGET' do
pod 'AppticsNotificationServiceExtension'
end
Note: Pass the app group identifier to the run script as mentioned in the podfile above --app-group-identifier="group.MAIN_BUNDLE_IDENTIFIER.apptics".
- From your terminal, navigate to the project root folder and run the pod install --repo-update.
Import and initialize in main application
Copied#import <Apptics/Apptics.h>
#import <AppticsMessaging/AppticsMessaging.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions{
[Apptics initializeWithVerbose:<#(BOOL)#>];
[AppticsMessaging startService];
return true;
}
Copiedimport Apptics
import AppticsMessaging
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Apptics.initialize(withVerbose: true)
AppticsMessaging.startService()
return true
}
Integrate the notification service extension
- In the Xcode project navigator, select the NotificationServiceExtension folder.
- Locate and open the NotificatioService.m (for Obj-C) or NotificationService.swift (for Swift) file.
- Replace the whole content of the file with the following code.
Copied#import "NotificationService.h"
#import <AppticsNotificationServiceExtension/AppticsNotificationServiceExtension.h>
@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];
// Modify the notification content here...
APPushNotificationExtension *apext = [APPushNotificationExtension new];
apext.appGroup = @"group.MAIN_BUNDLE_IDENTIFIER.apptics";
if ([apext isNotificationFromApptics:request]){
NSLog(@"AdditionalPayload : %@", [apext getAdditionalPayload]);
[apext didReceiveNotificationExtensionWithContent:self.bestAttemptContent contentHandler:contentHandler];
}else{
self.contentHandler(self.bestAttemptContent);
}
}
- (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.
self.contentHandler(self.bestAttemptContent);
}
@end
Copiedimport UserNotifications
import AppticsNotificationServiceExtension
class NotificationService: UNNotificationServiceExtension {
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)
guard let bestAttemptContent = bestAttemptContent else { return }
// Initialise Apptics notification extension
let apext = APPushNotificationExtension()
// Pass the app group identifier share and update the badge count.
apext.appGroup = "group.MAIN_BUNDLE_IDENTIFIER.apptics"
// Check and pass the notification request to Apptics extension handler
if apext.isNotification(fromApptics: request) {
apext.didReceive(with: bestAttemptContent, contentHandler: contentHandler)
} else {
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
if let bestAttemptContent = bestAttemptContent {
contentHandler?(bestAttemptContent)
}
}
}
Configure notification service
Upload p12 certificate
Refer to this link for detailed steps on how to upload your p12 certificate to Apptics.