目录
此内容是否有帮助?

# iOS Push Integration Document

# I. FCM push

# 1.1 Report "Push ID"

  • Track the FCM Token after calling TA login or switching accounts.
NSString *appid = @"appid";
NSString *url = @"url";
TDConfig *config = [TDConfig new];
config.appid = appid;
config.configureURL = url;
ThinkingAnalyticsSDK *instance = [ThinkingAnalyticsSDK startWithConfig:config];

[[FIRMessaging messaging] tokenWithCompletion:^(NSString *token, NSError *error) {
      if (error != nil) {
        NSLog(@"Error getting FCM registration token: %@", error);
      } else {
        NSLog(@"FCM registration token: %@", token);
        [instance user_set:@{@"fcm_token": token}];
      }
}];

# 1.2. Acquire push click events

When tapping the push notification, the user can send the push click event in the push click callback of the system.

// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler {
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    trackAppOpenNotification(userInfo)
    completionHandler();
}

# 1.3. Handle push messages

When tapping the push notification, the user can get push parameters in the push click callback of the system.

// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler {
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    handleThinkingPushAction(userInfo)
    completionHandler();
}

# II. JPush

# 2.1 Report "Push ID"

  • Track the Registration ID of JPush after calling TA login or switching accounts.
// Dafter login, report registrationID again
[instance login:<#login ID#>];
[instance user_set:@{@"jiguang_id": registrationID}];
    • registrationIDCompletionHandler: track the Registration ID of URORA in the callback
+// after TA SDK initialization, report the registrationID in URORA callback
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
    [instance user_set:@{@"jiguang_id": registrationID}];
}];

# 2.2. Acquire push click events

//  for versions earlier than iOS10, click the notification callback
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

  [JPUSHService handleRemoteNotification:userInfo];
  trackAppOpenNotification(userInfo);
  completionHandler(UIBackgroundFetchResultNewData);
}

// for versions later than iOS10, click the notification callback
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    // Required
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    trackAppOpenNotification(userInfo);
    completionHandler();  // the system requires this method to be executed
}

# 2.3. Handle push messages

//  for versions earlier than iOS10, click the notification callback
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

  // Required, iOS 7 Support
  [JPUSHService handleRemoteNotification:userInfo];
  handleThinkingPushAction(userInfo);
  completionHandler(UIBackgroundFetchResultNewData);
}

// for versions later than iOS10, click the notification callback
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    // Required
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    handleThinkingPushAction(userInfo);
    completionHandler();  // the system requires this method to be executed
}

#

# Appendix

  • trackAppOpenNotification method details
+ (void)trackAppOpenNotification:(NSDictionary *)userInfo{
    NSMutableDictionary *pushProperties = [NSMutableDictionary dictionary]; // track dictionary
    @try {
        NSData *jsonData = [userInfo[@"te_extras"] dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error;
        NSDictionary *sfDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        if (!error && [sfDictionary isKindOfClass:NSDictionary.class]) {
            pushProperties[@"ops_receipt_properties"] = sfDictionary[@"ops_receipt_properties"];
        }
        [[ThinkingAnalyticsSDK sharedInstance]track:@"ops_push_click" properties:pushProperties];
    } @catch (NSException *exception) {

    }
}
  • handleThinkingPushAction method details
+ (void)handleThinkingPushAction:(NSDictionary *)userInfo{
    NSMutableDictionary *pushProperties = [NSMutableDictionary dictionary]; // track dictionary
    @try {
        NSString *jsonString = userInfo[@"te_extras"];
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error;
        NSDictionary *sfDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        if (!sfDictionary || error) {
            return;
        }
        NSString *sf_landing_type = sfDictionary[@"ops_loading_type"];
        if ([sf_landing_type isEqualToString:@"OPEN_APP"]) {
            // open App
        }
        else if ([sf_landing_type isEqualToString:@"OPEN_URL"]) {
            // open URL
           NSString *url = sfDictionary[@"ops_url"];
        }
        else if ([sf_landing_type isEqualToString:@"CUSTOMIZED"]) {
           // handle custom messages
           NSString *customized = sfDictionary[@"ops_customized"];
        }

    } @catch (NSException *exception) {

    }
}