API Documentation

Last updated: 2021-10-29 10:05:17

    Description

    The account, tag, and user attribute features in this document are available for SDK v1.2.9.0 and later. For SDK v1.2.7.2 and earlier, please see API Documentation.

    Launching the TPNS Service

    The following are device registration API methods. For more information on the timing and principle of calls, see Device registration flow.

    API description

    This API is used to launch the TPNS service by using the information of the application registered at the official website of TPNS.
    (This API is newly added in SDK v1.2.7.2. For v1.2.7.1 and below, please see the startXGWithAppID API in the XGPush.h file in the SDK package.)

    /// @note TPNS SDK1.2.7.2+
    - (void)startXGWithAccessID:(uint32_t)accessID accessKey:(nonnull NSString *)accessKey delegate:(nullable id<XGPushDelegate>)delegate;
    

    Parameter description

    • accessID: AccessID applied through the frontend
    • accessKey: AccessKey applied through the frontend
    • Delegate: callback object
    Note:

    The parameters required by the API must be entered correctly; otherwise, TPNS will not be able to push messages correctly for the application.

    Sample code

    [[XGPush defaultManager] startXGWithAccessID:<your AccessID> accessKey:<your AccessKey> delegate:self];
    

    Terminating the TPNS Service

    The following are device unregistration API methods. For more information on the timing and principle of calls, please see Device unregistration flow.

    API description

    After the TPNS service is stopped, the application will not be able to push messages to devices through TPNS. To receive messages pushed by TPNS again, you must call the startXGWithAccessID:accessKey:delegate: method again to restart the TPNS service.

    - (void)stopXGNotification;
    

    Sample code

    [[XGPush defaultManager] stopXGNotification];
    

    TPNS Token and Registration Result

    Querying the TPNS token

    API description

    This API is used to query the token string generated by the current application on the TPNS server.

    @property (copy, nonatomic, nullable, readonly) NSString *xgTokenString;
    

    Sample code

    NSString *token = [[XGPushTokenManager defaultTokenManager] xgTokenString];
    

    Registration result callback

    API description

    After the SDK is started, use this method callback to return the registration result and token.

    - (void)xgPushDidRegisteredDeviceToken:(nullable NSString *)deviceToken xgToken:(nullable NSString *)xgToken error:(nullable NSError *)error
    

    Response parameters

    • deviceToken: device token generated by APNs.
    • xgToken: token generated by TPNS, which needs to be used during message push. TPNS maintains the mapping relationship between this value and the device token generated by APNs.
    • error: error message. If error is nil, TPNS has been successfully registered.

    Registration failure callback

    API description

    This callback is new in SDK v1.2.7.2 and used for TPNS registration failures.

    /// @note TPNS SDK1.2.7.2+
    - (void)xgPushDidFailToRegisterDeviceTokenWithError:(nullable NSError *)error
    

    Notification pop-up window authorization callback

    API description

    This API is new in SDK v1.3.1.0 and used to call back the result of notification authorization pop-up window.

    - (void)xgPushDidRequestNotificationPermission:(bool)isEnable error:(nullable NSError *)error;
    

    Response parameters

    • isEnable: whether authorization is approved or not.
    • error: error message. If error is nil, the pop-up authorization result has been successfully obtained.

    Account Feature

    The following are account API methods. For more information on the timing and principle of calls, please see Account flow.

    Adding an account

    API description

    If there is no account of this type, this API will add a new one; otherwise, it will overwrite the existing one. (Newly added in TPNS SDK v1.2.9.0+)

    - (void)upsertAccountsByDict:(nonnull NSDictionary<NSNumber *, NSString *> *)accountsDict;
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Parameter description

    accountsDict: account dictionary

    Note:

    • The account type and account name together serve as the composite primary key.
    • You need to use the dictionary type, where key is the account type and value is the account, for example, @{@(accountType):@"account"}.
    • Syntax for Objective-C: @{@(0):@"account0",@(1):@"account1"}; syntax for Swift:[NSNumber(0):@"account0",NSNumber(1):@"account1"]
    • For more accountType values, see the XGPushTokenAccountType enumeration in the SDK demo package or Account Type Value Table.

    Sample code

    XGPushTokenAccountType accountType = XGPushTokenAccountTypeUNKNOWN;
    NSString *account = @"account";
    [[XGPushTokenManager defaultTokenManager] upsertAccountsByDict:@{ @(accountType):account }];
    

    Adding a mobile number

    API description

    This API is used to add or update a mobile number. It is equivalent to calling upsertAccountsByDict:@{@(1002):@"specific mobile number"}.

    /// @note TPNS SDK1.3.2.0+
    - (void)upsertPhoneNumber:(nonnull NSString *)phoneNumber;
    

    Parameter description

    • phoneNumber: an E.164 mobile number in the format of [+][country code or area code][mobile number], for example, +8613711112222. The SDK will encrypt the mobile number for transmission.

    Sample code

    [[XGPushTokenManager defaultTokenManager] upsertPhoneNumber:@"+8613712345678"];;
    
    Note:

    1. This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    1. You can call delAccountsByKeys:[[NSSet alloc] initWithObjects:@(1002), nil] to delete a mobile number.

    Deleting accounts

    API description

    This API is used to delete all accounts of a specified account type. (Newly added in TPNS SDK v1.2.9.0+)

    - (void)delAccountsByKeys:(nonnull NSSet<NSNumber *> *)accountsKeys;
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Parameter description

    • accountsKeys: set of account types
    Note:

    • A set is required, and the key is fixed.
    • For more values of accountType, please see the enumerated values of XGPushTokenAccountType in the XGPush.h file in the SDK package.

    Sample code

    XGPushTokenAccountType accountType = XGPushTokenAccountTypeUNKNOWN;
    NSSet *accountsKeys = [[NSSet alloc] initWithObjects:@(accountType), nil];
    [[XGPushTokenManager defaultTokenManager] delAccountsByKeys:accountsKeys];
    

    Clearing accounts

    API description

    This API is used to clear all set accounts.

    - (void)clearAccounts;
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Sample code

    [[XGPushTokenManager defaultTokenManager] clearAccounts];
    

    Tagging Feature

    The following are tag API methods. For more information on the timing and principle of calls, please see Tag flow.

    Binding/Unbinding tags

    API description

    This API is used to bind tags to different users so that push can be performed based on specific tags.

    - (void)appendTags:(nonnull NSArray<NSString *> *)tags
    - (void)delTags:(nonnull NSArray<NSString *> *)tags
    
    Note:

    • This API works in an appending manner.
    • This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.
    • One app can have up to 10,000 custom tags. One device token can be bound to up to 100 custom tags. If you want to increase this limit, please contact customer service. One custom tag can be bound to an unlimited number of device tokens.

    Parameter description

    tags: tag array

    Note:

    For tag operations, tags is a tag string array, which cannot contain spaces or tabs.

    Sample code

    // Bind tags
    [[XGPushTokenManager defaultTokenManager] appendTags:@[ tagStr ]];
    // Unbind tags
    [[XGPushTokenManager defaultTokenManager] delTags:@[ tagStr ]];
    

    Updating tags

    API description

    This API is used to clear all the existing tags and then add tags in batches.

    - (void)clearAndAppendTags:(nonnull NSArray<NSString *> *)tags
    
    Note:

    • This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.
    • This API will replace all the old tags corresponding to the current token with the current tag.

    Parameter description

    tags: tag array

    Note:

    For tag operations, tags is a tag string array, which cannot contain spaces or tabs.

    Sample code

    [[XGPushTokenManager defaultTokenManager] clearAndAppendTags:@[ tagStr ]];
    

    Clearing all tags

    API description

    This API is used to clear all set tags.

    - (void)clearTags
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Sample code

    [[XGPushTokenManager defaultTokenManager] clearTags];
    

    Querying tags

    API description

    This API is new in SDK v1.3.1.0 and used to query the tags bound to the device.

    - (void)queryTags:(NSUInteger)offset limit:(NSUInteger)limit;
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Parameter description

    • offset: the offset of this query
    • limit: the page size for this query; maximum value: 200

    Sample code

    [[XGPushTokenManager defaultTokenManager] queryTags:0 limit:100];
    

    Tag query callback

    API description

    This API is new in SDK v1.3.1.0 and used to call back the result of tag query.

    - (void)xgPushDidQueryTags:(nullable NSArray<NSString *> *)tags totalCount:(NSUInteger)totalCount error:(nullable NSError *)error;
    

    Response parameters

    • tags: tags returned for the query
    • totalCount: total number of the tags bound to the device
    • error: error message. If error is nil, the query is successful.

    User Attribute Feature

    The following are user attribute API methods. For more information on the timing and principle of calls, please see User attribute flow.

    Adding user attributes

    API description

    This API is used to add or update user attributes in the key-value structure (if there is no user attribute value corresponding to the key, it will add a new one; otherwise, it will update the value).

    - (void)upsertAttributes:(nonnull NSDictionary<NSString *,NSString *> *)attributes
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Parameter description

    attributes: dictionary of user attribute strings, which cannot contain spaces or tabs

    Note:

    • You need to configure user attribute keys in the console first before the operation can succeed.
    • Both key and value can contain up to 50 characters.
    • A dictionary is required, and key is fixed.
    • Syntax for Objective-C: @{@"gender": @"Female", @"age": @"29"};
    • Syntax for Swift: ["gender":"Female", "age": "29"]

    Sample code

    [[XGPushTokenManager defaultTokenManager] upsertAttributes:attributes];
    

    Deleting a user attribute

    API description

    The API is used to delete existing user attributes.

    - (void)delAttributes:(nonnull NSSet<NSString *> *)attributeKeys
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Parameter description

    attributeKeys: set of user attribute keys, which cannot contain spaces or tabs

    Note:

    It is required to use a key set.

    Sample code

    [[XGPushTokenManager defaultTokenManager] delAttributes:attributeKeys];
    

    Clearing all user attributes

    API description

    This API is used to clear all existing user attributes.

    - (void)clearAttributes;
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Sample code

    [[XGPushTokenManager defaultTokenManager] clearAttributes];
    

    Updating user attributes

    API description

    This API is used to clear all the existing user attributes and then add user attributes in batches.

    - (void)clearAndAppendAttributes:(nonnull NSDictionary<NSString *,NSString *> *)attributes
    
    Note:

    This API should be called after xgPushDidRegisteredDeviceToken:error: returns a success.

    Sample code

    [[XGPushTokenManager defaultTokenManager] clearAndAppendAttributes:attributes];
    

    Badge Feature

    Syncing badges

    API description

    This API is used to sync the modified local badge value of an application to the TPNS server for the next push. You can choose Create Push > Advanced Settings > Badge Number in the console to configure the badge number.

    - (void)setBadge:(NSInteger)badgeNumber;
    

    Parameter description

    badgeNumber: badge number of an application

    Note:

    After the local badge number is set for the application, call this API to sync it to the TPNS server, which will take effect in the next push. This API must be called after successful TPNS registration (xgPushDidRegisteredDeviceToken).

    Sample code

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      /// Zero the badge number every time the application is started (you should set the local badge number for the application in the main thread)
      if ([XGPush defaultManager].xgApplicationBadgeNumber > 0) {
          [XGPush defaultManager].xgApplicationBadgeNumber = 0;
      }
      return YES;
    }
    - (void)xgPushDidRegisteredDeviceToken:(nullable NSString *)deviceToken xgToken:(nullable NSString *)xgToken error:(nullable NSError *)error {
      /// Sync the badge number to TPNS after registration
      if (!error) {
          [[XGPush defaultManager] setBadge:0];
      }
    }
    

    In-app message display

    Polling time setting

    API description

    This API can be used to set the polling time (minimum: 10s; default: 258s) of in-app messages.

    /// Set the message polling time interval (minimum: 10s). This API should be called before the singleton initialization.
    - (void)setMessageTimerInterval:(NSTimeInterval)interval;
    

    Parameter description

    NSTimeInterval: NSTimeInterval type; the in-app message polling time interval

    Custom event handling

    XGInAppMessageActionDelegate proxy description

    You can obtain custom event parameters to handle related businesses through the proxy method onClickWithCustomAction.

    /// Button event response proxy
    @property (weak, nonatomic, nullable) id<XGInAppMessageActionDelegate> actionDelegate;
    

    Querying Device Notification Permission

    API description

    This API is used to query whether the user allows device notifications.

    - (void)deviceNotificationIsAllowed:(nonnull void (^)(BOOL isAllowed))handler;
    

    Parameter description

    handler: result return method

    Sample code

    [[XGPush defaultManager] deviceNotificationIsAllowed:^(BOOL isAllowed) {
          <#code#>
      }];
    

    Querying the SDK Version

    API description

    This API is used to query the current SDK version.

    - (nonnull NSString *)sdkVersion;
    

    Sample code

    [[XGPush defaultManager] sdkVersion];
    

    Log Reporting API

    API description

    If you encounter push exceptions, you can call this API to trigger reporting of local push logs. For any problems, contact customer service and provide the file address to facilitate troubleshooting.

    /// @note TPNS SDK1.2.4.1+
    - (void)uploadLogCompletionHandler:(nullable void(^)(BOOL result,  NSString * _Nullable errorMessage))handler;
    

    Parameter description

    • @brief: report log information (SDK v1.2.4.1+).
    • @param handler: report callback

    Sample code

    [[XGPush defaultManager] uploadLogCompletionHandler:nil];
    

    TPNS Log Hosting

    API description

    This method is used to get TPNS logs, which is irrelevant to XGPush > enableDebug.

    Parameter description

    logInfo: log information

    Sample code

    - (void)xgPushLog:(nullable NSString *)logInfo;
    

    Customizing Notification Bar Message Actions

    Creating a message action

    API description

    This API is used to create a click event in the notification message.

    + (nullable id)actionWithIdentifier:(nonnull NSString *)identifier title:(nonnull NSString *)title options:(XGNotificationActionOptions)options;
    

    Parameter description

    • identifier: unique ID of the action.
    • title: action name.
    • options: options supported by the action.

    Sample code

    XGNotificationAction *action1 = [XGNotificationAction actionWithIdentifier:@"xgaction001" title:@"xgAction1" options:XGNotificationActionOptionNone];
    
    Note:

    The notification bar has the event click feature, which is only supported in iOS 8.0 and later. For iOS 7.x or earlier, this method will return null.

    Creating a category object

    API description

    This API is used to create a category object to manage the action object of the notification bar.

    + (nullable id)categoryWithIdentifier:(nonnull NSString *)identifier actions:(nullable NSArray<id> *)actions intentIdentifiers:(nullable NSArray<NSString *> *)intentIdentifiers options:(XGNotificationCategoryOptions)options
    

    Parameter description

    • identifier: category object ID.
    • actions: action object group included in the current category.
    • intentIdentifiers: identifiers that can be recognized by Siri.
    • options: category characteristics.
    Note:

    The notification bar has the event click feature, which is only supported in iOS 8.0 and later. For versions earlier than iOS 8.0, this method will return null.

    Sample code

    XGNotificationCategory *category = [XGNotificationCategory categoryWithIdentifier:@"xgCategory" actions:@[action1, action2] intentIdentifiers:@[] options:XGNotificationCategoryOptionNone];
    

    Creating a configuration class

    API description

    This API is used to manage the style and characteristics of the push message notification bar.

    + (nullable instancetype)configureNotificationWithCategories:(nullable NSSet<id> *)categories types:(XGUserNotificationTypes)types;
    

    Parameter description

    • categories: a collection of categories supported by the notification bar.
    • types: the style of the device registration notification.

    Sample code

    XGNotificationConfigure *configure = [XGNotificationConfigure configureNotificationWithCategories:[NSSet setWithObject:category] types:XGUserNotificationTypeAlert|XGUserNotificationTypeBadge|XGUserNotificationTypeSound];
    

    Local Push

    For more information about the local push feature, please click here.