All Products
Search
Document Center

iOS

Last Updated: Mar 23, 2022

Notice

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 guide introduces how to integrate MPS to iOS client. You can integrate MPS to iOS client based on native project with CocoaPods.

Procedure

To use MPS, you should complete the following steps.

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

  2. Configure the project.

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

    • Capabilities > Push Notificationspush-ca

    • Capabilities > Background Modes > Remote notificationspush-back

  3. Use the SDK.

    1. (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.

      - (void)registerRemoteNotification
      {
          // Push notification registration
          if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {// 10.0+
              UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
              center.delegate = self;
              [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
      
                      [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)
                                            completionHandler:^(BOOL granted, NSError * _Nullable error) {
                          // Enable or disable features based on authorization.
                          if (granted) {
                              dispatch_async(dispatch_get_main_queue(), ^{
                                  [[UIApplication sharedApplication] registerForRemoteNotifications];
                              });
                          }
                      }];
      
              }];
          } else {// 8.0,9.0
              UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge                                                                                         |UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
              [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
              [[UIApplication sharedApplication] registerForRemoteNotifications];
          }
      }
    2. 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.

      // import <PushService/PushService.h>
      - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
      {
          [[PushService sharedService] setDeviceToken:deviceToken];
          [[PushService sharedService] pushBindWithUserId:@"your userid(to be replaced)" completion:^(NSException *error) {
          }];
      
      }

      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 the user switches to another account.

    3. 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 the system versions earlier than iOS 10, the methods of processing notification bar messages or silent messages are as follows:

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

          // Register UNUserNotificationCenter delegate 
          if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 10.0) {
                  UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
                  center.delegate = self;
            }
        
           // Receive remote push messages when the app runs in the foreground
          - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
          {
              NSDictionary *userInfo = notification.request.content.userInfo;
        
              if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
                  // Receive remote push messages when the app runs in the foreground
        
              } else {
                  // Receive local push messages when the app runs in the foreground
        
              }
              completionHandler(UNNotificationPresentationOptionNone);
          }
        
          // Receive remote push messages when the app runs in the background or uses cold start mode
          - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
          {
              NSDictionary *userInfo = response.notification.request.content.userInfo;
        
              if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
                  // Receive remote push messages when the app runs in the background or uses cold start mode
        
              } else {
                  // Receive local push messages when the app runs in the foreground
        
              }
              completionHandler();
        
          }
    4. Calculate message open rate.

      To calculate the message open rate, call the API pushOpenLogReport of PushService class (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 Mobile Push Service > Overview in the mPaaS console to view the statistics on the message open rate.

      /**
       * Enable the API for reporting push messages so that the message open rate can be calculated.
       * @param  userInfo userInfo of a message
       * @return
       */
      - (void)pushOpenLogReport:(NSDictionary *)userInfo;
  4. 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.

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 with device tokens, messages can be pushed to applications in user dimension.

Code sample

Click here to download the code sample.