8282/* Omit no app_id error logging, for use with wrapper SDKs. */
8383NSString * const kOSSettingsKeyInOmitNoAppIdLogging = @" kOSSettingsKeyInOmitNoAppIdLogging" ;
8484
85+
86+ @interface OSSubcscriptionStatus : NSObject
87+ @property BOOL anwseredPrompt;
88+ @property BOOL accepted;
89+ @end
90+
91+ @implementation OSSubcscriptionStatus
92+ @end
93+
8594@implementation OneSignal
8695
8796NSString * const ONESIGNAL_VERSION = @" 020305" ;
@@ -171,7 +180,6 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
171180 [OneSignalLocation getLocation: false ];
172181
173182 if (self) {
174- UIApplication* sharedApp = [UIApplication sharedApplication ];
175183 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults ];
176184
177185 httpClient = [[OneSignalHTTPClient alloc ] init ];
@@ -247,10 +255,14 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
247255
248256 if (mUserId != nil )
249257 [self registerUser ];
250- else // Fall back in case Apple does not responds in time.
251- [self performSelector: @selector (registerUser ) withObject: nil afterDelay: 30 .0f ];
252-
253- [OneSignalTracker onFocus: NO ];
258+ else {
259+ [self userAnsweredNotificationPrompt: ^(OSSubcscriptionStatus *status) {
260+ if (status.anwseredPrompt )
261+ [self registerUser ];
262+ else
263+ [self performSelector: @selector (registerUser ) withObject: nil afterDelay: 30 .0f ];
264+ }];
265+ }
254266 }
255267
256268 /*
@@ -655,8 +667,8 @@ + (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock
655667 // iOS 8 - We get a token right away but give the user 30 sec to respond to the system prompt.
656668 // Also check notification types so there is no waiting if user has already answered the system prompt.
657669 // The goal is to only have 1 server call.
658- [self userAnsweredNotificationPrompt: ^(BOOL anwsered ) {
659- if (anwsered )
670+ [self userAnsweredNotificationPrompt: ^(OSSubcscriptionStatus *status ) {
671+ if (status. anwseredPrompt )
660672 [OneSignal registerUser ];
661673 else {
662674 [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector (registerUser ) object: nil ];
@@ -677,9 +689,12 @@ + (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock
677689 NSMutableURLRequest * request;
678690 request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , mUserId]];
679691
692+ int notificationTypes = [self getNotificationTypes ];
693+
680694 NSDictionary * dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
681695 app_id, @" app_id" ,
682696 deviceToken, @" identifier" ,
697+ [NSNumber numberWithInt: notificationTypes], @" notification_types" ,
683698 nil ];
684699
685700 [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: @" Calling OneSignal PUT updated pushToken!" ];
@@ -718,12 +733,12 @@ +(BOOL)shouldRegisterNow {
718733 if (waitingForOneSReg)
719734 return false ;
720735
721- // Figure out if should pass or not
736+ // Figure out if should pass or not
722737 NSTimeInterval now = [[NSDate date ] timeIntervalSince1970 ];
723738 NSTimeInterval lastTimeClosed = [[NSUserDefaults standardUserDefaults ] doubleForKey: @" GT_LAST_CLOSED_TIME" ];
724739 if (!lastTimeClosed) {
725740 [self updateLastSessionDateTime ];
726- return YES ;
741+ return true ;
727742 }
728743
729744 if ([self isHighPriorityCall ])
@@ -881,10 +896,14 @@ +(NSString*) getUsableDeviceToken {
881896}
882897
883898// Updates the server with the new user's notification setting or subscription status changes
884- + (void ) sendNotificationTypesUpdate {
885-
899+ + (BOOL ) sendNotificationTypesUpdate {
886900 // User changed notification settings for the app.
887901 if ([self getNotificationTypes ] != -1 && mUserId && mLastNotificationTypes != [self getNotificationTypes ]) {
902+ if (mDeviceToken == nil ) {
903+ [self registerForAPNsToken ];
904+ return true ;
905+ }
906+
888907 mLastNotificationTypes = [self getNotificationTypes ];
889908 NSMutableURLRequest * request = [httpClient requestWithMethod: @" PUT" path: [NSString stringWithFormat: @" players/%@ " , mUserId]];
890909 NSDictionary * dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
@@ -900,8 +919,11 @@ + (void) sendNotificationTypesUpdate {
900919 idsAvailableBlockWhenReady (mUserId, [self getUsableDeviceToken ]);
901920 idsAvailableBlockWhenReady = nil ;
902921 }
922+
923+ return true ;
903924 }
904925
926+ return false ;
905927}
906928
907929+ (void )sendPurchases : (NSArray *)purchases {
@@ -1127,12 +1149,10 @@ + (int) getNotificationTypes {
11271149 if (!mSubscriptionSet)
11281150 return -2 ;
11291151
1130- if (mDeviceToken) {
1131- if ([OneSignalHelper canGetNotificationTypes ])
1132- return [[UIApplication sharedApplication ] currentUserNotificationSettings ].types ;
1133- else
1134- return NOTIFICATION_TYPE_ALL;
1135- }
1152+ if ([OneSignalHelper canGetNotificationTypes ])
1153+ return [[UIApplication sharedApplication ] currentUserNotificationSettings ].types ;
1154+ else if (mDeviceToken)
1155+ return NOTIFICATION_TYPE_ALL;
11361156
11371157 return 0 ;
11381158}
@@ -1142,21 +1162,34 @@ + (void)setSubscriptionStatus:(int)errorType {
11421162 [self sendNotificationTypesUpdate ];
11431163}
11441164
1145- + (void )userAnsweredNotificationPrompt : (void (^)(BOOL anwsered))completionHandler {
1165+ + (void )userAnsweredNotificationPrompt : (void (^)(OSSubcscriptionStatus * anwsered))completionHandler {
11461166 if ([OneSignalHelper isiOS10Plus ]) {
11471167 Class unUserNotifClass = NSClassFromString (@" UNUserNotificationCenter" );
11481168 [[unUserNotifClass currentNotificationCenter ] getNotificationSettingsWithCompletionHandler: ^(id settings) {
11491169 // Trigger callback on the main thread.
11501170 // Prevents any possiblitity creating thread locks by callling currentUserNotification from this block.
11511171 dispatch_async (dispatch_get_main_queue (), ^{
11521172 [OneSignal onesignal_Log: ONE_S_LL_VERBOSE message: [NSString stringWithFormat: @" getNotificationSettingsWithCompletionHandler Called: %@ " , settings]];
1153- completionHandler ([settings authorizationStatus ] != 0 );
1173+ OSSubcscriptionStatus *status = [OSSubcscriptionStatus alloc ];
1174+ status.anwseredPrompt = [settings authorizationStatus ] != 0 ;
1175+ status.accepted = [settings authorizationStatus ] == 2 ;
1176+ completionHandler (status);
11541177 });
11551178 }];
11561179 }
11571180 else {
11581181 NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults ];
1159- completionHandler ([userDefaults boolForKey: @" OS_NOTIFICATION_PROMPT_ANSWERED" ]);
1182+ OSSubcscriptionStatus *status = [OSSubcscriptionStatus alloc ];
1183+ status.anwseredPrompt = [userDefaults boolForKey: @" OS_NOTIFICATION_PROMPT_ANSWERED" ];
1184+
1185+ if ([OneSignalHelper canGetNotificationTypes ]) {
1186+ status.accepted = [[UIApplication sharedApplication ] currentUserNotificationSettings ].types > 0 ;
1187+ completionHandler (status);
1188+ }
1189+ else {
1190+ status.accepted = mDeviceToken != nil ;
1191+ completionHandler (status);
1192+ }
11601193 }
11611194}
11621195
0 commit comments