4545#import < sys/sysctl.h>
4646#import < objc/runtime.h>
4747
48+ #define NOTIFICATION_TYPE_NONE 0
4849#define NOTIFICATION_TYPE_BADGE 1
4950#define NOTIFICATION_TYPE_SOUND 2
5051#define NOTIFICATION_TYPE_ALERT 4
8182
8283@implementation OneSignal
8384
84- NSString * const ONESIGNAL_VERSION = @" 020008 " ;
85+ NSString * const ONESIGNAL_VERSION = @" 020009 " ;
8586
8687static bool registeredWithApple = false ; // Has attempted to register for push notifications with Apple.
8788static OneSignalTrackIAP* trackIAPPurchase;
@@ -182,11 +183,6 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
182183 if (autoPrompt || registeredWithApple)
183184 [self registerForPushNotifications ];
184185
185-
186- // iOS 8 - Register for remote notifications to get a token now since registerUserNotificationSettings is what shows the prompt.
187- else if ([[UIApplication sharedApplication ] respondsToSelector: @selector (registerForRemoteNotifications )])
188- [[UIApplication sharedApplication ] registerForRemoteNotifications ];
189-
190186 [OneSignalTracker onFocus: NO ];
191187 }
192188
@@ -287,11 +283,13 @@ + (void)registerForPushNotifications {
287283 }
288284}
289285
286+ // Block not assigned if userID nil and there is a device token
290287+ (void )IdsAvailable : (OSIdsAvailableBlock)idsAvailableBlock {
288+
291289 if (mUserId)
292- idsAvailableBlock (mUserId, [ self getUsableDeviceToken ] );
290+ idsAvailableBlock (mUserId, mDeviceToken );
293291
294- if (mUserId == nil || [ self getUsableDeviceToken ] == nil )
292+ if (! mUserId || !mDeviceToken )
295293 idsAvailableBlockWhenReady = idsAvailableBlock;
296294}
297295
@@ -476,7 +474,7 @@ + (void)setSubscription:(BOOL)enable {
476474
477475 mSubscriptionSet = enable;
478476
479- [OneSignal sendNotificationTypesUpdateIsConfirmed :false ];
477+ [OneSignal sendNotificationTypesUpdate :false ];
480478}
481479
482480+ (void ) promptLocation {
@@ -492,17 +490,20 @@ + (void)registerDeviceToken:(id)inDeviceToken onSuccess:(OSResultSuccessBlock)su
492490
493491+ (void )updateDeviceToken : (NSString *)deviceToken onSuccess : (OSResultSuccessBlock)successBlock onFailure : (OSFailureBlock)failureBlock {
494492
493+ // Do not block next registration as there's a new token in hand
494+ nextRegistrationIsHighPriority = YES ;
495+
495496 if (mUserId == nil ) {
496497 mDeviceToken = deviceToken;
497498 tokenUpdateSuccessBlock = successBlock;
498499 tokenUpdateFailureBlock = failureBlock;
499500
500- // iOS 8 - We get a token right away but give the user 20 sec to responsed to the system prompt.
501+ // iOS 8 - We get a token right away but give the user 10 sec to responsed to the system prompt.
501502 // Also check mNotificationTypes so there is no waiting if user has already answered the system prompt.
502503 // The goal is to only have 1 server call.
503504 if ([OneSignalHelper isCapableOfGettingNotificationTypes ] && mNotificationTypes == -1 ) {
504505 [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector (registerUser ) object: nil ];
505- [self performSelector: @selector (registerUser ) withObject: nil afterDelay: 20 .0f ];
506+ [self performSelector: @selector (registerUser ) withObject: nil afterDelay: 10 .0f ];
506507 }
507508 else
508509 [OneSignal registerUser ];
@@ -534,17 +535,19 @@ + (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock
534535
535536 if (idsAvailableBlockWhenReady) {
536537 mNotificationTypes = [self getNotificationTypes ];
537- if ([self getUsableDeviceToken ])
538- idsAvailableBlockWhenReady (mUserId, [self getUsableDeviceToken ]);
539- idsAvailableBlockWhenReady = nil ;
538+ if (mDeviceToken) {
539+ idsAvailableBlockWhenReady (mUserId, mDeviceToken);
540+ idsAvailableBlockWhenReady = nil ;
541+ }
540542 }
543+
541544}
542545
543- // Set to yes whenever a high priority registration fails ... need to make the next one a high priority to disregard the 1 hour timer delay
546+ // Set to yes whenever a high priority registration fails ... need to make the next one a high priority to disregard the timer delay
544547bool nextRegistrationIsHighPriority = NO ;
545548
546549+ (BOOL )isHighPriorityCall {
547- return mUserId == nil || (mDeviceToken == nil && mNotificationTypes > 0 ) || nextRegistrationIsHighPriority;
550+ return mUserId == nil || (mDeviceToken == nil && mNotificationTypes > NOTIFICATION_TYPE_NONE ) || nextRegistrationIsHighPriority;
548551}
549552
550553+(BOOL )shouldRegisterNow {
@@ -570,7 +573,6 @@ + (void)registerUser {
570573 if (waitingForOneSReg || ![self shouldRegisterNow ])
571574 return ;
572575
573-
574576 waitingForOneSReg = true ;
575577
576578 NSMutableURLRequest * request;
@@ -676,9 +678,9 @@ + (void)registerUser {
676678 }
677679
678680 if (idsAvailableBlockWhenReady) {
679- idsAvailableBlockWhenReady (mUserId, [ self getUsableDeviceToken ] );
680- if ([ self getUsableDeviceToken ] )
681- idsAvailableBlockWhenReady = nil ;
681+ idsAvailableBlockWhenReady (mUserId, mDeviceToken );
682+ if (mDeviceToken )
683+ idsAvailableBlockWhenReady = nil ;
682684 }
683685 }
684686 } onFailure: ^(NSError * error) {
@@ -689,16 +691,12 @@ + (void)registerUser {
689691 nextRegistrationIsHighPriority = YES ;
690692 }];
691693}
694+
695+ // Updates the server with the new user's notification Types
696+ + (void ) sendNotificationTypesUpdate : (BOOL )isNewType {
692697
693- +(NSString *) getUsableDeviceToken {
694- if (mNotificationTypes > 0 )
695- return mDeviceToken;
696- return nil ;
697- }
698-
699- + (void ) sendNotificationTypesUpdateIsConfirmed : (BOOL )isConfirm {
700698 // User changed notification settings for the app.
701- if (mNotificationTypes != -1 && mUserId && (isConfirm || mNotificationTypes != [self getNotificationTypes ])) {
699+ if (mNotificationTypes != -1 && mUserId && (isNewType || mNotificationTypes != [self getNotificationTypes ])) {
702700 mNotificationTypes = [self getNotificationTypes ];
703701 NSMutableURLRequest * request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , mUserId]];
704702
@@ -712,8 +710,8 @@ + (void) sendNotificationTypesUpdateIsConfirmed:(BOOL)isConfirm {
712710
713711 [OneSignalHelper enqueueRequest: request onSuccess: nil onFailure: nil ];
714712
715- if ([ self getUsableDeviceToken ] && idsAvailableBlockWhenReady) {
716- idsAvailableBlockWhenReady (mUserId, [ self getUsableDeviceToken ] );
713+ if (mDeviceToken && idsAvailableBlockWhenReady) {
714+ idsAvailableBlockWhenReady (mUserId, mDeviceToken );
717715 idsAvailableBlockWhenReady = nil ;
718716 }
719717 }
@@ -910,20 +908,21 @@ + (int) getNotificationTypes {
910908
911909// iOS 8.0+ only
912910+ (void ) updateNotificationTypes : (int )notificationTypes {
911+
913912 if (mNotificationTypes == -2 )
914- return ;
913+ return ;
915914
916915 BOOL changed = (mNotificationTypes != notificationTypes);
917916
918917 mNotificationTypes = notificationTypes;
919918
920- if (mUserId == nil && mDeviceToken)
919+ if (! mUserId && mDeviceToken)
921920 [OneSignal registerUser ];
922921 else if (mDeviceToken)
923- [self sendNotificationTypesUpdateIsConfirmed : changed];
922+ [self sendNotificationTypesUpdate : changed];
924923
925- if (idsAvailableBlockWhenReady && mUserId && [ self getUsableDeviceToken ] )
926- idsAvailableBlockWhenReady (mUserId, [ self getUsableDeviceToken ] );
924+ if (idsAvailableBlockWhenReady && mUserId && mDeviceToken )
925+ idsAvailableBlockWhenReady (mUserId, mDeviceToken );
927926}
928927
929928+ (void )didRegisterForRemoteNotifications : (UIApplication*)app deviceToken : (NSData *)inDeviceToken {
0 commit comments