UnityRuntime.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // PushRuntime.m
  3. // Pushwoosh SDK
  4. // (c) Pushwoosh 2012
  5. //
  6. #import "PushNotificationManager.h"
  7. #import <objc/runtime.h>
  8. static char * g_pw_tokenStr = 0;
  9. static char * g_pw_registerErrStr = 0;
  10. static char * g_pw_pushMessageStr = 0;
  11. static char * g_pw_listenerName = 0;
  12. static NSString * g_pw_launchNotification = nil;
  13. void pw_registerForRemoteNotifications() {
  14. [[PushNotificationManager pushManager] registerForPushNotifications];
  15. }
  16. void pw_initializePushManager(char *appId, char *appName) {
  17. NSString *appCodeStr = [[NSString alloc] initWithUTF8String:appId];
  18. NSString *appNameStr = [[NSString alloc] initWithUTF8String:appName];
  19. [PushNotificationManager initializeWithAppCode:appCodeStr appName:appNameStr];
  20. [[PushNotificationManager pushManager] sendAppOpen];
  21. [PushNotificationManager pushManager].delegate = (NSObject<PushNotificationDelegate> *)[UIApplication sharedApplication];
  22. }
  23. void pw_unregisterForRemoteNotifications() {
  24. [[PushNotificationManager pushManager] unregisterForPushNotifications];
  25. }
  26. void *pw_getPushToken() {
  27. return (void *)[[[PushNotificationManager pushManager] getPushToken] UTF8String];
  28. }
  29. void *pw_getPushwooshHWID() {
  30. return (void *)[[[PushNotificationManager pushManager] getHWID] UTF8String];
  31. }
  32. void *pw_getLaunchNotification() {
  33. if (g_pw_launchNotification) {
  34. return (void *)[g_pw_launchNotification UTF8String];
  35. }
  36. return NULL;
  37. }
  38. void pw_clearLaunchNotification() {
  39. g_pw_launchNotification = nil;
  40. }
  41. void pw_setUserId(char *userId) {
  42. NSString *userIdStr = [[NSString alloc] initWithUTF8String:userId];
  43. [[PushNotificationManager pushManager] setUserId:userIdStr];
  44. }
  45. void pw_postEvent(char *event, char *attributes) {
  46. NSString *eventStr = [[NSString alloc] initWithUTF8String:event];
  47. NSString *attributesStr = [[NSString alloc] initWithUTF8String:attributes];
  48. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[attributesStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
  49. if ([json isKindOfClass:[NSDictionary class]]) {
  50. [[PushNotificationManager pushManager] postEvent:eventStr withAttributes:json];
  51. }
  52. else {
  53. NSLog(@"Invalid postEvent attribute argument: %@", json);
  54. }
  55. }
  56. void pw_sendPurchase(char *productId, double price, char *currency) {
  57. NSString *productIdStr = [[NSString alloc] initWithUTF8String:productId];
  58. NSDecimalNumber *priceDecimal = [[NSDecimalNumber alloc] initWithDouble:price];
  59. NSString *currencyStr = [[NSString alloc] initWithUTF8String:currency];
  60. [[PushNotificationManager pushManager] sendPurchase:productIdStr withPrice:priceDecimal currencyCode:currencyStr andDate:[NSDate date]];
  61. }
  62. void pw_setListenerName(char *listenerName) {
  63. free(g_pw_listenerName); g_pw_listenerName = 0;
  64. int len = strlen(listenerName);
  65. g_pw_listenerName = malloc(len + 1);
  66. strcpy(g_pw_listenerName, listenerName);
  67. if(g_pw_tokenStr) {
  68. UnitySendMessage(g_pw_listenerName, "onRegisteredForPushNotifications", g_pw_tokenStr);
  69. free(g_pw_tokenStr); g_pw_tokenStr = 0;
  70. }
  71. if(g_pw_registerErrStr) {
  72. UnitySendMessage(g_pw_listenerName, "onFailedToRegisteredForPushNotifications", g_pw_registerErrStr);
  73. free(g_pw_registerErrStr); g_pw_registerErrStr = 0;
  74. }
  75. if(g_pw_pushMessageStr) {
  76. UnitySendMessage(g_pw_listenerName, "onPushNotificationsReceived", g_pw_pushMessageStr);
  77. free(g_pw_pushMessageStr); g_pw_pushMessageStr = 0;
  78. }
  79. }
  80. void pw_setIntTag(char *tagName, int tagValue) {
  81. NSString *tagNameStr = [[NSString alloc] initWithUTF8String:tagName];
  82. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:tagValue], tagNameStr, nil];
  83. [[PushNotificationManager pushManager] setTags:dict];
  84. #if !__has_feature(objc_arc)
  85. [tagNameStr release];
  86. #endif
  87. }
  88. void pw_setStringTag(char *tagName, char *tagValue) {
  89. NSString *tagNameStr = [[NSString alloc] initWithUTF8String:tagName];
  90. NSString *tagValueStr = [[NSString alloc] initWithUTF8String:tagValue];
  91. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:tagValueStr, tagNameStr, nil];
  92. [[PushNotificationManager pushManager] setTags:dict];
  93. #if !__has_feature(objc_arc)
  94. [tagNameStr release];
  95. [tagValueStr release];
  96. #endif
  97. }
  98. void pw_internalSendStringTags (char *tagName, char **tags) {
  99. size_t length = 0;
  100. while (tags[length] != NULL) length++;
  101. NSMutableArray *tagsArray = [NSMutableArray array];
  102. NSString *tagNameStr = [[NSString alloc] initWithUTF8String:tagName];
  103. for (int i = 0; i < length; i++) {
  104. char *tagValue = tags[i];
  105. NSString *tagValueStr = [[NSString alloc] initWithUTF8String:tagValue];
  106. if (tagValueStr) {
  107. [tagsArray addObject:tagValueStr];
  108. }
  109. #if !__has_feature(objc_arc)
  110. [tagValueStr release];
  111. #endif
  112. }
  113. if (tagsArray.count) {
  114. [[PushNotificationManager pushManager] setTags:@{tagNameStr : tagsArray}];
  115. }
  116. #if !__has_feature(objc_arc)
  117. [tagNameStr release];
  118. #endif
  119. }
  120. void pw_startLocationTracking() {
  121. [[PushNotificationManager pushManager] startLocationTracking];
  122. }
  123. void pw_clearNotificationCenter() {
  124. [PushNotificationManager clearNotificationCenter];
  125. }
  126. void pw_stopLocationTracking() {
  127. [[PushNotificationManager pushManager] stopLocationTracking];
  128. }
  129. void pw_setBadgeNumber(int badge) {
  130. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
  131. }
  132. void pw_addBadgeNumber(int deltaBadge) {
  133. int badge = [UIApplication sharedApplication].applicationIconBadgeNumber + deltaBadge;
  134. pw_setBadgeNumber(badge);
  135. }
  136. @implementation UIApplication(InternalPushRuntime)
  137. - (NSObject<PushNotificationDelegate> *)getPushwooshDelegate {
  138. return (NSObject<PushNotificationDelegate> *)[UIApplication sharedApplication];
  139. }
  140. - (BOOL)pushwooshUseRuntimeMagic {
  141. return YES;
  142. }
  143. //succesfully registered for push notifications
  144. - (void)onDidRegisterForRemoteNotificationsWithDeviceToken:(NSString *)token {
  145. const char * str = [token UTF8String];
  146. if(!g_pw_listenerName) {
  147. g_pw_tokenStr = malloc(strlen(str)+1);
  148. strcpy(g_pw_tokenStr, str);
  149. return;
  150. }
  151. UnitySendMessage(g_pw_listenerName, "onRegisteredForPushNotifications", str);
  152. }
  153. //failed to register for push notifications
  154. - (void)onDidFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  155. const char * str = [[error description] UTF8String];
  156. if(!g_pw_listenerName) {
  157. if (str) {
  158. g_pw_registerErrStr = malloc(strlen(str)+1);
  159. strcpy(g_pw_registerErrStr, str);
  160. }
  161. return;
  162. }
  163. UnitySendMessage(g_pw_listenerName, "onFailedToRegisteredForPushNotifications", str);
  164. }
  165. //handle push notification, display alert, if this method is implemented onPushAccepted will not be called, internal message boxes will not be displayed
  166. - (void)onPushAccepted:(PushNotificationManager *)pushManager withNotification:(NSDictionary *)pushNotification onStart:(BOOL)onStart {
  167. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:pushNotification options:0 error:nil];
  168. NSString *jsonRequestData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  169. if (onStart) {
  170. g_pw_launchNotification = jsonRequestData;
  171. }
  172. const char * str = [jsonRequestData UTF8String];
  173. if(!g_pw_listenerName) {
  174. g_pw_pushMessageStr = malloc(strlen(str)+1);
  175. strcpy(g_pw_pushMessageStr, str);
  176. return;
  177. }
  178. UnitySendMessage(g_pw_listenerName, "onPushNotificationsReceived", str);
  179. }
  180. @end