# iOS Push Integration Document
# 1. FCM push
# 1.1 Report "Push ID"
- Track the FCM Token after calling TE 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();
}
# 2. JPush
# 2.1 Report "Push ID"
- Track the Registration ID of JPush after calling TE 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
}
# 3. APNs
# 3.1 Report "Push ID"
- deviceToken is reported after login. The field type is string.
[instance login:<#Login ID#>];
[instance user_set:@{@"#apns_token": deviceTokenString}];
- Report user properties in the callback of the received registered deviceToken.
- (<span style="font-weight:bold;">void</span>)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token;
<span style="font-weight:bold;">if</span> ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13.0) {
<span style="font-weight:bold;">const</span> <span style="font-weight:bold;">unsigned</span> *tokenBytes = [deviceToken bytes];
token = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
}<span style="font-weight:bold;">else</span>{
token = [[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
}
[instance user_set:@{@"#apns_token": token}];
}
# 3.2 Acquire Push Click Events
// iOS 10 previous:
- (<span style="font-weight:bold;">void</span>)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(<span style="font-weight:bold;">void</span> (^)(UIBackgroundFetchResult result))completionHandler {
// report click event,MyClass is the samplename.- trackAppOpenNotification:method show as follows
[MyClass trackAppOpenNotification:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
}
// iOS 10 after:
- (<span style="font-weight:bold;">void</span>)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(<span style="font-weight:bold;">void</span>(^)(<span style="font-weight:bold;">void</span>))completionHandler {
NSDictionary *userInfo = response.notification.request.content.userInfo;
// report click event,MyClass is the samplename.- trackAppOpenNotification:method show as follows
[MyClass trackAppOpenNotification:userInfo];
completionHandler();
}
# 3.3 Handle Push Messages
// iOS 10 previous:
- (<span style="font-weight:bold;">void</span>)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(<span style="font-weight:bold;">void</span> (^)(UIBackgroundFetchResult result))completionHandler {
// deal with ordinary parameters, call the following method,MyClass is the samplename.- handleTEPushAction:method show as follows
[MyClass handleTEPushAction:userInfo];
// deal with Passthrough parameters, call the following method,MyClass is the samplename.- handleTEPassThroughAction:method show as follows
[MyClass handleTEPassThroughAction:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
}
// iOS 10 after:
- (<span style="font-weight:bold;">void</span>)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(<span style="font-weight:bold;">void</span>(^)(<span style="font-weight:bold;">void</span>))completionHandler {
NSDictionary *userInfo = response.notification.request.content.userInfo;
// deal with ordinary parameters, call the following method,MyClass is the samplename.- handleTEPushAction:method show as follows
[MyClass handleTEPushAction:userInfo];
// deal with Passthrough parameters, call the following method, MyClass is the samplename.- handleTEPassThroughAction:method show as follows
[MyClass handleTEPassThroughAction:userInfo];
completionHandler();
}
# 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) {
}
}