Skip to content

Instantly share code, notes, and snippets.

@javieranton-zz
Last active October 18, 2020 20:21
Show Gist options
  • Save javieranton-zz/7e6f40a629dc9ffc8fcbf7fa3870c128 to your computer and use it in GitHub Desktop.
Save javieranton-zz/7e6f40a629dc9ffc8fcbf7fa3870c128 to your computer and use it in GitHub Desktop.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
const unsigned *tokenBytes = [deviceToken bytes];
NSString *tokenAsString = [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])];
JAVA_OBJECT str = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG tokenAsString);
your_namespace_MainClass_iosPushRegistered___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG str);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
JAVA_OBJECT str = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG @"error");
your_namespace_MainClass_iosPushRegistered___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG str);
}
NSMutableArray *repliedNotifAcks = nil;
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSString *metaInfo = [userInfo valueForKey:@"meta"];
NSData *data = [metaInfo dataUsingEncoding:NSUTF8StringEncoding];
id metaJson = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *notifId = [metaJson valueForKey:@"notifId"];
NSString *senderId = [metaJson valueForKey:@"senderId"];
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if(state == UIApplicationStateActive)
{
completionHandler(UIBackgroundFetchResultNoData);
JAVA_OBJECT strNotifId = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG notifId);
JAVA_OBJECT strSenderId = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG senderId);
your_namespace_MainClass_iosActivePushReceived___java_lang_String_java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG strNotifId, strSenderId);
}
else
{
BOOL firstBG = YES;
__block UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
if(![repliedNotifAcks containsObject:notifId])
{
[repliedNotifAcks addObject:notifId];
[self Fetchdata:notifId:senderId];
}else
firstBG = NO;
[[UIApplication sharedApplication] endBackgroundTask:(UIBackgroundTaskIdentifier)bgTask];
bgTask = UIBackgroundTaskInvalid;
completionHandler(UIBackgroundFetchResultNewData);
if((firstBG && state == UIApplicationStateInactive) || !firstBG)
{
JAVA_OBJECT str = fromNSString(CN1_THREAD_GET_STATE_PASS_ARG notifId);
your_namespace_MainClass_iosPushReceived___java_lang_String(CN1_THREAD_GET_STATE_PASS_ARG str);
}
}
}
-(void)Fetchdata:(NSString*)notifId:(NSString*)senderId
{
NSURLSessionConfiguration *defaultSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultSessionConfiguration];
NSURL *url = [NSURL URLWithString:@"https://www.yourServer.com/endpoint"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString *postParams = [NSString stringWithFormat:@"%@%@%@%@%@", @"{\"notifId\":\"", notifId, @"\",\"senderId\":\"",senderId,@"\"}"];
NSData *postData = [postParams dataUsingEncoding:NSUTF8StringEncoding];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:postData];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[dataTask resume];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment