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,8 +183,8 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
182183 if (autoPrompt || registeredWithApple)
183184 [self registerForPushNotifications ];
184185
185-
186186 // iOS 8 - Register for remote notifications to get a token now since registerUserNotificationSettings is what shows the prompt.
187+ // If autoprompt disabled, get a token from APNS for silent notifications until user calls regsiterForPushNotifications to request push permissions from user.
187188 else if ([[UIApplication sharedApplication ] respondsToSelector: @selector (registerForRemoteNotifications )])
188189 [[UIApplication sharedApplication ] registerForRemoteNotifications ];
189190
@@ -287,11 +288,13 @@ + (void)registerForPushNotifications {
287288 }
288289}
289290
291+ // Block not assigned if userID nil and there is a device token
290292+ (void )IdsAvailable : (OSIdsAvailableBlock)idsAvailableBlock {
293+
291294 if (mUserId)
292295 idsAvailableBlock (mUserId, [self getUsableDeviceToken ]);
293296
294- if (mUserId == nil || [self getUsableDeviceToken ] == nil )
297+ if (! mUserId || ! [self getUsableDeviceToken ])
295298 idsAvailableBlockWhenReady = idsAvailableBlock;
296299}
297300
@@ -476,7 +479,7 @@ + (void)setSubscription:(BOOL)enable {
476479
477480 mSubscriptionSet = enable;
478481
479- [OneSignal sendNotificationTypesUpdateIsConfirmed :false ];
482+ [OneSignal sendNotificationTypesUpdate :false ];
480483}
481484
482485+ (void ) promptLocation {
@@ -492,17 +495,20 @@ + (void)registerDeviceToken:(id)inDeviceToken onSuccess:(OSResultSuccessBlock)su
492495
493496+ (void )updateDeviceToken : (NSString *)deviceToken onSuccess : (OSResultSuccessBlock)successBlock onFailure : (OSFailureBlock)failureBlock {
494497
498+ // Do not block next registration as there's a new token in hand
499+ nextRegistrationIsHighPriority = YES ;
500+
495501 if (mUserId == nil ) {
496502 mDeviceToken = deviceToken;
497503 tokenUpdateSuccessBlock = successBlock;
498504 tokenUpdateFailureBlock = failureBlock;
499505
500- // iOS 8 - We get a token right away but give the user 20 sec to responsed to the system prompt.
506+ // iOS 8 - We get a token right away but give the user 10 sec to responsed to the system prompt.
501507 // Also check mNotificationTypes so there is no waiting if user has already answered the system prompt.
502508 // The goal is to only have 1 server call.
503509 if ([OneSignalHelper isCapableOfGettingNotificationTypes ] && mNotificationTypes == -1 ) {
504510 [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector (registerUser ) object: nil ];
505- [self performSelector: @selector (registerUser ) withObject: nil afterDelay: 20 .0f ];
511+ [self performSelector: @selector (registerUser ) withObject: nil afterDelay: 10 .0f ];
506512 }
507513 else
508514 [OneSignal registerUser ];
@@ -534,17 +540,19 @@ + (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock
534540
535541 if (idsAvailableBlockWhenReady) {
536542 mNotificationTypes = [self getNotificationTypes ];
537- if ([self getUsableDeviceToken ])
538- idsAvailableBlockWhenReady (mUserId, [self getUsableDeviceToken ]);
539- idsAvailableBlockWhenReady = nil ;
543+ if ([self getUsableDeviceToken ]) {
544+ idsAvailableBlockWhenReady (mUserId, [self getUsableDeviceToken ]);
545+ idsAvailableBlockWhenReady = nil ;
546+ }
540547 }
548+
541549}
542550
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
551+ // Set to yes whenever a high priority registration fails ... need to make the next one a high priority to disregard the timer delay
544552bool nextRegistrationIsHighPriority = NO ;
545553
546554+ (BOOL )isHighPriorityCall {
547- return mUserId == nil || (mDeviceToken == nil && mNotificationTypes > 0 ) || nextRegistrationIsHighPriority;
555+ return mUserId == nil || (mDeviceToken == nil && mNotificationTypes > NOTIFICATION_TYPE_NONE ) || nextRegistrationIsHighPriority;
548556}
549557
550558+(BOOL )shouldRegisterNow {
@@ -570,7 +578,6 @@ + (void)registerUser {
570578 if (waitingForOneSReg || ![self shouldRegisterNow ])
571579 return ;
572580
573-
574581 waitingForOneSReg = true ;
575582
576583 NSMutableURLRequest * request;
@@ -677,8 +684,8 @@ + (void)registerUser {
677684
678685 if (idsAvailableBlockWhenReady) {
679686 idsAvailableBlockWhenReady (mUserId, [self getUsableDeviceToken ]);
680- if ([ self getUsableDeviceToken ] )
681- idsAvailableBlockWhenReady = nil ;
687+ if (mDeviceToken )
688+ idsAvailableBlockWhenReady = nil ;
682689 }
683690 }
684691 } onFailure: ^(NSError * error) {
@@ -689,16 +696,16 @@ + (void)registerUser {
689696 nextRegistrationIsHighPriority = YES ;
690697 }];
691698}
692-
699+
693700+(NSString *) getUsableDeviceToken {
694- if (mNotificationTypes > 0 )
695- return mDeviceToken;
696- return nil ;
701+ return (mNotificationTypes > 0 ) ? mDeviceToken : NULL ;
697702}
703+
704+ // Updates the server with the new user's notification Types
705+ + (void ) sendNotificationTypesUpdate : (BOOL )isNewType {
698706
699- + (void ) sendNotificationTypesUpdateIsConfirmed : (BOOL )isConfirm {
700707 // User changed notification settings for the app.
701- if (mNotificationTypes != -1 && mUserId && (isConfirm || mNotificationTypes != [self getNotificationTypes ])) {
708+ if (mNotificationTypes != -1 && mUserId && (isNewType || mNotificationTypes != [self getNotificationTypes ])) {
702709 mNotificationTypes = [self getNotificationTypes ];
703710 NSMutableURLRequest * request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , mUserId]];
704711
@@ -910,20 +917,21 @@ + (int) getNotificationTypes {
910917
911918// iOS 8.0+ only
912919+ (void ) updateNotificationTypes : (int )notificationTypes {
920+
913921 if (mNotificationTypes == -2 )
914- return ;
922+ return ;
915923
916924 BOOL changed = (mNotificationTypes != notificationTypes);
917925
918926 mNotificationTypes = notificationTypes;
919927
920- if (mUserId == nil && mDeviceToken)
928+ if (! mUserId && mDeviceToken)
921929 [OneSignal registerUser ];
922930 else if (mDeviceToken)
923- [self sendNotificationTypesUpdateIsConfirmed : changed];
931+ [self sendNotificationTypesUpdate : changed];
924932
925933 if (idsAvailableBlockWhenReady && mUserId && [self getUsableDeviceToken ])
926- idsAvailableBlockWhenReady (mUserId, [self getUsableDeviceToken ]);
934+ idsAvailableBlockWhenReady (mUserId, [self getUsableDeviceToken ]);
927935}
928936
929937+ (void )didRegisterForRemoteNotifications : (UIApplication*)app deviceToken : (NSData *)inDeviceToken {
0 commit comments