In order to use SDK built-in support for Push notifications, you must configure your app to support Apple's Push Notifications, and then you need to pass the deviceToken
which your application has registered with a call to setPushToken
in the SDK.
Example
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSMutableString *token = [NSMutableString string];
const char *data = [deviceToken bytes];
for (NSUInteger i = 0; i < [deviceToken length]; i++) {
[token appendFormat:@"%.2hhx", data[i]];
}
if (token) {
[client setPushToken:token completion:^(CMPResult<NSNumber *> * result) {
BOOL success = [result.object boolValue];
if (result.error || !success) {
// error occurred
} else {
// configuration successful
}
}];
}
// rest of you push notification code
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
client.set(pushToken: token, completion: { (success, error) in
if error != nil || !success {
// error
} else {
// successfully configured push notifications
}
})
// rest of you push notification code
}
Display push notifications when the app is in the foreground
Push notifications are displayed only while the app is in the background. These notifications are sent to the notification center and launch your app when users tap them.
It’s possible to display the push notification message while the app is in the foreground, or get access to the push notification to process the data, such as deep links. The method to do this depends on the iOS version that your app is running on:
For iOS 9
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateActive) {
}
}
For iOS 10 and above
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
completionHandler();
}