All Products
Search
Document Center

Access iOS

Last Updated: Jun 28, 2021
Important: Since June 28, 2020, mPaaS has stopped support for the baseline 10.1.32. Please use 10.1.68 or 10.1.60 instead. For how to upgrade the baseline from version 10.1.32 to 10.1.68 or 10.1.60, see mPaaS 10.1.68 upgrade guide or mPaaS 10.1.60 upgrade guide.

This topic describes how to integrate MPS into an iOS client. To use MPS, you must complete the following process:

  1. Add SDK
  2. Configure a project
  3. Use the SDK
  4. Configure a push certificate

Add SDK

Integrate the MPS SDK with reference to Access based on native framework and using Cocoapods.

Configure a project

Enable the following functions in the TARGETS directory of your project:

  • Capabilities > Push Notifications
    push-ca
  • Capabilities > Background Modes > Remote notifications
    push-back`

Use SDK

If you have an existing project and use the CocoaPods plugin to access the iOS client, you need to complete the following operations.

(Optional) Register device token

After the app is started, the push SDK automatically requests to register a device token. Generally, you do not need to manually request for device token registration. However, in special cases, for example, when all network requests are blocked due to privacy management and control at startup, you need to trigger device token registration again after grating authorization.

 
  1. - (void)registerRemoteNotification
  2. {
  3. // Push notification registration
  4. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {// 10.0+
  5. UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
  6. center.delegate = self;
  7. [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  8. [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)
  9. completionHandler:^(BOOL granted, NSError * _Nullable error) {
  10. // Enable or disable features based on authorization.
  11. if (granted) {
  12. dispatch_async(dispatch_get_main_queue(), ^{
  13. [[UIApplication sharedApplication] registerForRemoteNotifications];
  14. });
  15. }
  16. }];
  17. }];
  18. } else {// 8.0, 9.0
  19. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
  20. [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  21. [[UIApplication sharedApplication] registerForRemoteNotifications];
  22. }
  23. }

Obtain the device token and bind it with user ID

The logic for registering to the APNs server is encapsulated in the push SDK provided by mPaaS. After your application is started, the push SDK automatically registers to the APNs server. You can obtain the device token issued by the APNs server in the registration success callback method, and call an API provided by PushService to report the bound user ID to the mobile push core.

Compared with the native-framework-based applications, the life cycle of the applications based on the mPaaS is taken over by the mPaaS framework, and the callback methods for the applications to obtain the deviceTokens are different. The code sample is as follows:

 
  1. // import <PushService/PushService.h>
  2. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  3. {
  4. [[PushService sharedService] setDeviceToken:deviceToken];
  5. [[PushService sharedService] pushBindWithUserId:@"your userid(to be replaced)" completion:^(NSException *error) {
  6. }];
  7. }
Note: The push SDK also provides the API - (void)pushUnBindWithUserId:(NSString *)userId completion:(void (^)(NSException *error))completion; for unbinding the device token from the user ID of the app. For example, you can call the unbind API after a user switches to another account.

Receive push messages

When a user taps a message received on the client, the system starts the corresponding application. You can implement the processing after a message is received in the callback method of AppDelegate.

  • In system versions earlier than iOS 10, the methods of processing notification bar messages or silent messages are as follows:

       
    1. // Cold start for push messages in system versions earlier than iOS 10
    2. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    3. NSDictionary *userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    4. if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 10.0) {
    5. // Cold start for push messages in system versions earlier than iOS 10
    6. }
    7. return YES;
    8. }
    9. // Method of processing common push messages when the app runs in the foreground; method of processing silent messages when the app runs in the background or foreground; method of processing notification bar messages for system versions earlier than iOS 10
    10. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
    11. {
    12. // Process received messages
    13. }
  • In iOS 10 and later versions, you need to implement the following delegate methods to listen to notification bar messages:

       
    1. // Register UNUserNotificationCenter delegate
    2. if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 10.0) {
    3. UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    4. center.delegate = self;
    5. }
    6. // Receive remote push messages when the app runs in the foreground
    7. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    8. {
    9. NSDictionary *userInfo = notification.request.content.userInfo;
    10. if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    11. // Receive remote push messages when the App runs in the foreground
    12. } else {
    13. // Receive local push messages when the App runs in the foreground
    14. }
    15. completionHandler(UNNotificationPresentationOptionNone);
    16. }
    17. // Receive remote push messages when the app runs in the background or uses cold start mode
    18. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
    19. {
    20. NSDictionary *userInfo = response.notification.request.content.userInfo;
    21. if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    22. // Receive remote push messages when the app runs in the background or uses cold start mode
    23. } else {
    24. // Receive local push messages when the app runs in the foreground
    25. }
    26. completionHandler();
    27. }

Calculate message open rate

To measure the message open rate, call the PushService class API pushOpenLogReport (available in 10.1.32 and later versions) to report a message open event when a message is opened on the client. After the event is reported, you can choose Backend Service Management > Mobile Push Service > Usage analysis in the mPaaS console to view the statistics on the message open rate.

 
  1. /**
  2. * Enable the API for reporting push messages so that the message open rate can be measured.
  3. * @param userInfo userInfo of a message
  4. * @return
  5. */
  6. - (void)pushOpenLogReport:(NSDictionary *)userInfo;

Configure a push certificate

To push messages through the MPS console of mPaaS, you need to configure an APNs push certificate in the console. This certificate must match the signature on the client. Otherwise, the client cannot receive push messages.

For more information about the configuration, see Configure an iOS push certificate.

Code sample

Click here to download the code sample.

Follow-up steps

  • After an APNs certificate is configured on the MPS console of mPaaS, messages can be pushed to applications in device dimension. MPS pushes messages to clients through Apple APNs. For more information, see Push process for Apple devices and Android devices outside China.
  • After user IDs are reported and the server binds them to device tokens, messages can be pushed to applications in user dimension.

The supported push types are as follows:

  • Simple push
  • Template push
  • Multiple push
  • Broadcast push

For definitions of these push types, see Create a message.