35
35
36
36
#define DEFAULT_PUSH_HOST @" https://onesignal.com/api/v1/"
37
37
38
- NSString * const VERSION = @" 010900 " ;
38
+ NSString * const VERSION = @" 011000 " ;
39
39
40
40
#define NOTIFICATION_TYPE_BADGE 1
41
41
#define NOTIFICATION_TYPE_SOUND 2
@@ -160,7 +160,10 @@ - (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
160
160
161
161
mUserId = [defaults stringForKey: @" GT_PLAYER_ID" ];
162
162
mDeviceToken = [defaults stringForKey: @" GT_DEVICE_TOKEN" ];
163
- registeredWithApple = mDeviceToken != nil || [defaults boolForKey: @" GT_REGISTERED_WITH_APPLE" ];
163
+ if (([[UIApplication sharedApplication ] respondsToSelector: @selector (currentUserNotificationSettings )]))
164
+ registeredWithApple = [[UIApplication sharedApplication ] currentUserNotificationSettings ].types != (NSUInteger )nil ;
165
+ else
166
+ registeredWithApple = mDeviceToken != nil || [defaults boolForKey: @" GT_REGISTERED_WITH_APPLE" ];
164
167
mSubscriptionSet = [defaults objectForKey: @" ONESIGNAL_SUBSCRIPTION" ] == nil ;
165
168
mNotificationTypes = getNotificationTypes ();
166
169
@@ -184,7 +187,7 @@ - (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
184
187
[self notificationOpened: userInfo isActive: false ];
185
188
}
186
189
187
- clearBadgeCount ();
190
+ clearBadgeCount (false );
188
191
189
192
if ([OneSignalTrackIAP canTrack ])
190
193
trackIAPPurchase = [[OneSignalTrackIAP alloc ] init ];
@@ -303,6 +306,7 @@ - (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OneSignalResultSucce
303
306
deviceToken, @" identifier" ,
304
307
nil ];
305
308
309
+ Log (ONE_S_LL_VERBOSE, @" Calling OneSignal PUT updated pushToken!" );
306
310
NSData * postData = [NSJSONSerialization dataWithJSONObject: dataDic options: 0 error: nil ];
307
311
[request setHTTPBody: postData];
308
312
@@ -398,6 +402,7 @@ - (void)registerUser {
398
402
if (releaseMode == UIApplicationReleaseDev || releaseMode == UIApplicationReleaseAdHoc)
399
403
dataDic[@" test_type" ] = [NSNumber numberWithInt: releaseMode];
400
404
405
+ Log (ONE_S_LL_VERBOSE, @" Calling OneSignal create/on_session" );
401
406
NSData * postData = [NSJSONSerialization dataWithJSONObject: dataDic options: 0 error: nil ];
402
407
[request setHTTPBody: postData];
403
408
@@ -606,7 +611,7 @@ - (void)onFocus:(NSString*)state {
606
611
lastTrackedTime = [NSNumber numberWithLongLong: [[NSDate date ] timeIntervalSince1970 ]];
607
612
608
613
[self sendNotificationTypesUpdateIsConfirmed: false ];
609
- wasBadgeSet = clearBadgeCount ();
614
+ wasBadgeSet = clearBadgeCount (false );
610
615
}
611
616
else {
612
617
NSNumber * timeElapsed = @(([[NSDate date ] timeIntervalSince1970 ] - [lastTrackedTime longLongValue ]) + 0.5 );
@@ -770,7 +775,7 @@ - (void) handleNotificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isAc
770
775
771
776
self.lastMessageReceived = messageDict;
772
777
773
- clearBadgeCount ();
778
+ clearBadgeCount (true );
774
779
[[UIApplication sharedApplication ] cancelAllLocalNotifications ];
775
780
776
781
@@ -779,18 +784,19 @@ - (void) handleNotificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isAc
779
784
handleNotification ([self getMessageString ], [self getAdditionalData ], isActive);
780
785
}
781
786
782
- bool clearBadgeCount () {
787
+ bool clearBadgeCount (bool fromNotifOpened ) {
783
788
if (mNotificationTypes == -1 || (mNotificationTypes & NOTIFICATION_TYPE_BADGE) == 0 )
784
789
return false ;
785
790
786
- bool wasBadgeSet = false ;
787
-
788
- if ([UIApplication sharedApplication ].applicationIconBadgeNumber > 0 )
789
- wasBadgeSet = true ;
791
+ bool wasBadgeSet = [UIApplication sharedApplication ].applicationIconBadgeNumber > 0 ;
790
792
791
- // Clear bages and nofiications from this app. Setting to 1 then 0 was needed to clear the notifications.
792
- [[UIApplication sharedApplication ] setApplicationIconBadgeNumber: 1 ];
793
- [[UIApplication sharedApplication ] setApplicationIconBadgeNumber: 0 ];
793
+ if ((!(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) && fromNotifOpened) || wasBadgeSet) {
794
+ // Clear bages and nofiications from this app.
795
+ // Setting to 1 then 0 was needed to clear the notifications on iOS 6 & 7. (Otherwise you can click the notification multiple times.)
796
+ // iOS 8+ auto dismisses the notificaiton you tap on so only clear the badge (and notifications [side-effect]) if it was set.
797
+ [[UIApplication sharedApplication ] setApplicationIconBadgeNumber: 1 ];
798
+ [[UIApplication sharedApplication ] setApplicationIconBadgeNumber: 0 ];
799
+ }
794
800
795
801
return wasBadgeSet;
796
802
}
@@ -842,12 +848,20 @@ - (NSDictionary*)getAdditionalData {
842
848
additionalData[@" sound" ] = self.lastMessageReceived [@" aps" ][@" sound" ];
843
849
if (self.lastMessageReceived [@" custom" ][@" u" ] != nil )
844
850
additionalData[@" launchURL" ] = self.lastMessageReceived [@" custom" ][@" u" ];
851
+ if ([self .lastMessageReceived[@" aps" ][@" alert" ] isKindOfClass: [NSDictionary class ]])
852
+ additionalData[@" title" ] = self.lastMessageReceived [@" aps" ][@" alert" ][@" title" ];
845
853
846
854
return additionalData;
847
855
}
848
856
849
857
- (NSString *)getMessageString {
850
- return self.lastMessageReceived [@" aps" ][@" alert" ];
858
+ id alertObj = self.lastMessageReceived [@" aps" ][@" alert" ];
859
+ if ([alertObj isKindOfClass: [NSString class ]])
860
+ return alertObj;
861
+ else if ([alertObj isKindOfClass: [NSDictionary class ]])
862
+ return alertObj[@" body" ];
863
+
864
+ return @" " ;
851
865
}
852
866
853
867
- (void )postNotification : (NSDictionary *)jsonData {
@@ -982,7 +996,6 @@ - (void)setSubscription:(BOOL)enable {
982
996
[self sendNotificationTypesUpdateIsConfirmed: false ];
983
997
}
984
998
985
-
986
999
- (void )didRegisterForRemoteNotifications : (UIApplication*)app deviceToken : (NSData *)inDeviceToken {
987
1000
NSString * trimmedDeviceToken = [[inDeviceToken description ] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" <>" ]];
988
1001
NSString * parsedDeviceToken = [[trimmedDeviceToken componentsSeparatedByString: @" " ] componentsJoinedByString: @" " ];
@@ -995,6 +1008,7 @@ - (void)didRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSDat
995
1008
}
996
1009
997
1010
- (void ) remoteSilentNotification : (UIApplication*)application UserInfo : (NSDictionary *)userInfo {
1011
+ // If 'm' present then the notification has action buttons attached to it.
998
1012
if (userInfo[@" m" ]) {
999
1013
NSDictionary * data = userInfo;
1000
1014
@@ -1026,7 +1040,13 @@ - (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio
1026
1040
1027
1041
UILocalNotification* notification = [[UILocalNotification alloc ] init ];
1028
1042
notification.category = [category identifier ];
1029
- notification.alertBody = data[@" m" ];
1043
+ if ([data[@" m" ] isKindOfClass: [NSDictionary class ]]) {
1044
+ if ([notification respondsToSelector: NSSelectorFromString (@" alertTitle" )])
1045
+ notification.alertTitle = data[@" m" ][@" title" ];
1046
+ notification.alertBody = data[@" m" ][@" body" ];
1047
+ }
1048
+ else
1049
+ notification.alertBody = data[@" m" ];
1030
1050
notification.userInfo = userInfo;
1031
1051
notification.soundName = data[@" s" ];
1032
1052
if (notification.soundName == nil )
@@ -1036,7 +1056,7 @@ - (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio
1036
1056
1037
1057
[[UIApplication sharedApplication ] scheduleLocalNotification: notification];
1038
1058
}
1039
- else
1059
+ else if (application. applicationState != UIApplicationStateBackground)
1040
1060
[self notificationOpened: userInfo isActive: [application applicationState ] == UIApplicationStateActive];
1041
1061
}
1042
1062
@@ -1232,11 +1252,15 @@ + (void)load {
1232
1252
static Class delegateClass = nil ;
1233
1253
1234
1254
- (void ) setOneSignalDelegate : (id <UIApplicationDelegate>)delegate {
1235
- if (delegateClass != nil )
1236
- return ;
1255
+ if (delegateClass != nil ) {
1256
+ [self setOneSignalDelegate: delegate];
1257
+ return ;
1258
+ }
1259
+
1237
1260
1238
1261
delegateClass = getClassWithProtocolInHierarchy ([delegate class ], @protocol (UIApplicationDelegate));
1239
1262
1263
+
1240
1264
injectSelector (self.class , @selector (oneSignalRemoteSilentNotification:UserInfo:fetchCompletionHandler: ),
1241
1265
delegateClass, @selector (application:didReceiveRemoteNotification:fetchCompletionHandler: ));
1242
1266
0 commit comments