@@ -132,7 +132,6 @@ @implementation OneSignal
132132static NSString * app_id;
133133NSString * emailToSet;
134134NSMutableDictionary * tagsToSend;
135- OneSignalHTTPClient *httpClient;
136135OSResultSuccessBlock tokenUpdateSuccessBlock;
137136OSFailureBlock tokenUpdateFailureBlock;
138137
@@ -143,6 +142,15 @@ @implementation OneSignal
143142BOOL disableBadgeClearing = NO ;
144143BOOL mShareLocation = YES ;
145144
145+
146+
147+ static OneSignalHTTPClient *_httpClient;
148+ + (OneSignalHTTPClient*)httpClient {
149+ if (!_httpClient)
150+ _httpClient = [OneSignalHTTPClient new ];
151+ return _httpClient;
152+ }
153+
146154static OSNotificationDisplayType _inFocusDisplayType = OSNotificationDisplayTypeInAppAlert;
147155+ (void )setInFocusDisplayType : (OSNotificationDisplayType)value {
148156 NSInteger op = value;
@@ -200,6 +208,7 @@ + (OSSubscriptionState*)currentSubscriptionState {
200208 if (!_currentSubscriptionState) {
201209 _currentSubscriptionState = [OSSubscriptionState alloc ];
202210 _currentSubscriptionState = [_currentSubscriptionState initAsToWithPermision: self .currentPermissionState.accepted];
211+ mLastNotificationTypes = _currentPermissionState.notificationTypes ;
203212 [self .currentPermissionState.observable addObserver: _currentSubscriptionState];
204213 [_currentSubscriptionState.observable addObserver: [OSSubscriptionChangedInternalObserver alloc ]];
205214 }
@@ -271,8 +280,11 @@ + (void) setWaitingForApnsResponse:(BOOL)value {
271280}
272281
273282+ (void )clearStatics {
283+ app_id = nil ;
284+ _httpClient = nil ;
274285 _osNotificationSettings = nil ;
275286 waitingForApnsResponse = false ;
287+ mLastNotificationTypes = -1 ;
276288
277289 _lastPermissionState = nil ;
278290 _currentPermissionState = nil ;
@@ -321,8 +333,6 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
321333 if (self) {
322334 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults ];
323335
324- httpClient = [[OneSignalHTTPClient alloc ] init ];
325-
326336 [OneSignalHelper notificationBlocks: receivedCallback : actionCallback];
327337
328338 if (appId)
@@ -633,7 +643,7 @@ + (void) sendTagsToServer {
633643 pendingSendTagCallbacks = nil ;
634644
635645
636- NSMutableURLRequest * request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
646+ NSMutableURLRequest * request = [self . httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
637647
638648 NSDictionary * dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
639649 app_id, @" app_id" ,
@@ -676,7 +686,7 @@ + (void)getTags:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)fai
676686 return ;
677687
678688 NSMutableURLRequest * request;
679- request = [httpClient requestWithMethod: @" GET" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
689+ request = [self . httpClient requestWithMethod: @" GET" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
680690
681691 [OneSignalHelper enqueueRequest: request onSuccess: ^(NSDictionary * results) {
682692 if ([results objectForKey: @" tags" ] != nil )
@@ -735,7 +745,7 @@ + (void)postNotification:(NSDictionary*)jsonData {
735745}
736746
737747+ (void )postNotification : (NSDictionary *)jsonData onSuccess : (OSResultSuccessBlock)successBlock onFailure : (OSFailureBlock)failureBlock {
738- NSMutableURLRequest * request = [httpClient requestWithMethod: @" POST" path: @" notifications" ];
748+ NSMutableURLRequest * request = [self . httpClient requestWithMethod: @" POST" path: @" notifications" ];
739749
740750 NSMutableDictionary * dataDic = [[NSMutableDictionary alloc ] initWithDictionary: jsonData];
741751 dataDic[@" app_id" ] = dataDic[@" app_id" ] ?: app_id;
@@ -807,7 +817,8 @@ + (void)setSubscription:(BOOL)enable {
807817
808818 self.currentSubscriptionState .userSubscriptionSetting = enable;
809819
810- [OneSignal sendNotificationTypesUpdate ];
820+ if (app_id)
821+ [OneSignal sendNotificationTypesUpdate ];
811822}
812823
813824
@@ -847,11 +858,6 @@ + (void) handleDidFailRegisterForRemoteNotification:(NSError*)err {
847858 [self .osNotificationSettings onAPNsResponse: false ];
848859}
849860
850- + (void )registerDeviceToken : (id )inDeviceToken onSuccess : (OSResultSuccessBlock)successBlock onFailure : (OSFailureBlock)failureBlock {
851- waitingForApnsResponse = false ;
852- [self updateDeviceToken: inDeviceToken onSuccess: successBlock onFailure: failureBlock];
853- }
854-
855861+ (void )updateDeviceToken : (NSString *)deviceToken onSuccess : (OSResultSuccessBlock)successBlock onFailure : (OSFailureBlock)failureBlock {
856862 onesignal_Log (ONE_S_LL_VERBOSE, @" updateDeviceToken:onSuccess:onFailure:" );
857863
@@ -886,7 +892,7 @@ + (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock
886892 self.currentSubscriptionState .pushToken = deviceToken;
887893
888894 NSMutableURLRequest * request;
889- request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
895+ request = [self . httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
890896
891897 int notificationTypes = [self getNotificationTypes ];
892898
@@ -980,9 +986,9 @@ + (void)registerUserInternal {
980986
981987 NSMutableURLRequest * request;
982988 if (!self.currentSubscriptionState .userId )
983- request = [httpClient requestWithMethod: @" POST" path: @" players" ];
989+ request = [self . httpClient requestWithMethod: @" POST" path: @" players" ];
984990 else
985- request = [httpClient requestWithMethod: @" POST" path: [NSString stringWithFormat: @" players/%@ /on_session" , self .currentSubscriptionState.userId]];
991+ request = [self . httpClient requestWithMethod: @" POST" path: [NSString stringWithFormat: @" players/%@ /on_session" , self .currentSubscriptionState.userId]];
986992
987993 NSDictionary * infoDictionary = [[NSBundle mainBundle ]infoDictionary];
988994 NSString * build = infoDictionary[(NSString *)kCFBundleVersionKey ];
@@ -1115,11 +1121,12 @@ + (BOOL) sendNotificationTypesUpdate {
11151121 }
11161122
11171123 mLastNotificationTypes = [self getNotificationTypes ];
1118- NSMutableURLRequest * request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
1124+ NSMutableURLRequest * request = [self . httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
11191125 NSDictionary * dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
11201126 app_id, @" app_id" ,
11211127 @([self getNotificationTypes ]), @" notification_types" ,
11221128 nil ];
1129+ NSLog (@" dataDic: %@ " , dataDic);
11231130 NSData * postData = [NSJSONSerialization dataWithJSONObject: dataDic options: 0 error: nil ];
11241131 [request setHTTPBody: postData];
11251132
@@ -1138,7 +1145,7 @@ + (void)sendPurchases:(NSArray*)purchases {
11381145 if (!self.currentSubscriptionState .userId )
11391146 return ;
11401147
1141- NSMutableURLRequest * request = [httpClient requestWithMethod: @" POST" path: [NSString stringWithFormat: @" players/%@ /on_purchase" , self .currentSubscriptionState.userId]];
1148+ NSMutableURLRequest * request = [self . httpClient requestWithMethod: @" POST" path: [NSString stringWithFormat: @" players/%@ /on_purchase" , self .currentSubscriptionState.userId]];
11421149
11431150 NSDictionary *dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
11441151 app_id, @" app_id" ,
@@ -1284,7 +1291,7 @@ + (void)submitNotificationOpened:(NSString*)messageId {
12841291 NSString * lastMessageId = [[NSUserDefaults standardUserDefaults ] objectForKey: @" GT_LAST_MESSAGE_OPENED_" ];
12851292 // Only submit request if messageId not nil and: (lastMessage is nil or not equal to current one)
12861293 if (messageId && (!lastMessageId || ![lastMessageId isEqualToString: messageId])) {
1287- NSMutableURLRequest * request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" notifications/%@ " , messageId]];
1294+ NSMutableURLRequest * request = [self . httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" notifications/%@ " , messageId]];
12881295 NSDictionary * dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
12891296 app_id, @" app_id" ,
12901297 self .currentSubscriptionState.userId, @" player_id" ,
@@ -1391,8 +1398,15 @@ + (void)updateNotificationTypes:(int)notificationTypes {
13911398+ (void )didRegisterForRemoteNotifications : (UIApplication*)app deviceToken : (NSData *)inDeviceToken {
13921399 NSString * trimmedDeviceToken = [[inDeviceToken description ] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" <>" ]];
13931400 NSString * parsedDeviceToken = [[trimmedDeviceToken componentsSeparatedByString: @" " ] componentsJoinedByString: @" " ];
1401+
13941402 [OneSignal onesignal_Log: ONE_S_LL_INFO message: [NSString stringWithFormat: @" Device Registered with Apple: %@ " , parsedDeviceToken]];
1395- [OneSignal registerDeviceToken: parsedDeviceToken onSuccess: ^(NSDictionary * results) {
1403+
1404+ waitingForApnsResponse = false ;
1405+
1406+ if (!app_id)
1407+ return ;
1408+
1409+ [OneSignal updateDeviceToken: parsedDeviceToken onSuccess: ^(NSDictionary * results) {
13961410 [OneSignal onesignal_Log: ONE_S_LL_INFO message: [NSString stringWithFormat: @" Device Registered with OneSignal: %@ " , self .currentSubscriptionState.userId]];
13971411 } onFailure: ^(NSError * error) {
13981412 [OneSignal onesignal_Log: ONE_S_LL_ERROR message: [NSString stringWithFormat: @" Error in OneSignal Registration: %@ " , error]];
@@ -1478,7 +1492,7 @@ + (void)syncHashedEmail:(NSString *)email {
14781492
14791493 onesignal_Log (ONE_S_LL_DEBUG, [NSString stringWithFormat: @" %@ - MD5: %@ , SHA1:%@ " , lowEmail, md5, sha1]);
14801494
1481- NSMutableURLRequest * request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
1495+ NSMutableURLRequest * request = [self . httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , self .currentSubscriptionState.userId]];
14821496 NSDictionary * dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
14831497 app_id, @" app_id" ,
14841498 md5, @" em_m" ,
0 commit comments