Skip to content

Commit 21a026b

Browse files
committed
iOS update - Title support and bug fixes
* Fixed issue where the iOS notification permission prompt would show on the 2nd cold start of the app when autoRegister is set to false. * Added support for notification title on iOS 8.2+ devices. * Fixed bug where notifications would clear when opening / resuming the app. - Notifications will still be cleared in this way when a badge is set. - This is due to an iOS limitation where clearing the badge count has a side effect of clearing all notifications from the app. - iOS 6 & 7 - When a notification is opened all other notifications from the app will still be cleared. - This logic remains otherwise notifications can be tapped on multiple times. * Fixed bug where if content-available was set the notification would be marked opened when received and again when it was opened.
1 parent c927638 commit 21a026b

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.7.7",
2+
"version": "1.8.0",
33
"name": "onesignal-cordova-plugin",
44
"cordova_name": "OneSignal Push Notifications",
55
"description": "OneSignal is a high volume Push Notification service for mobile apps. In addition to basic notification delivery, OneSignal also provides tools to localize, target, schedule, and automate notifications that you send.",

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
id="com.onesignal.plugins.onesignal"
5-
version="1.7.7">
5+
version="1.8.0">
66

77
<name>OneSignal Push Notifications</name>
88
<author>Josh Kasten</author>

src/ios/OneSignal.m

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
#define DEFAULT_PUSH_HOST @"https://onesignal.com/api/v1/"
3737

38-
NSString* const VERSION = @"010900";
38+
NSString* const VERSION = @"011000";
3939

4040
#define NOTIFICATION_TYPE_BADGE 1
4141
#define NOTIFICATION_TYPE_SOUND 2
@@ -160,7 +160,10 @@ - (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
160160

161161
mUserId = [defaults stringForKey:@"GT_PLAYER_ID"];
162162
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"];
164167
mSubscriptionSet = [defaults objectForKey:@"ONESIGNAL_SUBSCRIPTION"] == nil;
165168
mNotificationTypes = getNotificationTypes();
166169

@@ -184,7 +187,7 @@ - (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
184187
[self notificationOpened:userInfo isActive:false];
185188
}
186189

187-
clearBadgeCount();
190+
clearBadgeCount(false);
188191

189192
if ([OneSignalTrackIAP canTrack])
190193
trackIAPPurchase = [[OneSignalTrackIAP alloc] init];
@@ -303,6 +306,7 @@ - (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OneSignalResultSucce
303306
deviceToken, @"identifier",
304307
nil];
305308

309+
Log(ONE_S_LL_VERBOSE, @"Calling OneSignal PUT updated pushToken!");
306310
NSData* postData = [NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil];
307311
[request setHTTPBody:postData];
308312

@@ -398,6 +402,7 @@ - (void)registerUser {
398402
if (releaseMode == UIApplicationReleaseDev || releaseMode == UIApplicationReleaseAdHoc)
399403
dataDic[@"test_type"] = [NSNumber numberWithInt:releaseMode];
400404

405+
Log(ONE_S_LL_VERBOSE, @"Calling OneSignal create/on_session");
401406
NSData* postData = [NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil];
402407
[request setHTTPBody:postData];
403408

@@ -606,7 +611,7 @@ - (void)onFocus:(NSString*)state {
606611
lastTrackedTime = [NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]];
607612

608613
[self sendNotificationTypesUpdateIsConfirmed:false];
609-
wasBadgeSet = clearBadgeCount();
614+
wasBadgeSet = clearBadgeCount(false);
610615
}
611616
else {
612617
NSNumber* timeElapsed = @(([[NSDate date] timeIntervalSince1970] - [lastTrackedTime longLongValue]) + 0.5);
@@ -770,7 +775,7 @@ - (void) handleNotificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isAc
770775

771776
self.lastMessageReceived = messageDict;
772777

773-
clearBadgeCount();
778+
clearBadgeCount(true);
774779
[[UIApplication sharedApplication] cancelAllLocalNotifications];
775780

776781

@@ -779,18 +784,19 @@ - (void) handleNotificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isAc
779784
handleNotification([self getMessageString], [self getAdditionalData], isActive);
780785
}
781786

782-
bool clearBadgeCount() {
787+
bool clearBadgeCount(bool fromNotifOpened) {
783788
if (mNotificationTypes == -1 || (mNotificationTypes & NOTIFICATION_TYPE_BADGE) == 0)
784789
return false;
785790

786-
bool wasBadgeSet = false;
787-
788-
if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0)
789-
wasBadgeSet = true;
791+
bool wasBadgeSet = [UIApplication sharedApplication].applicationIconBadgeNumber > 0;
790792

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+
}
794800

795801
return wasBadgeSet;
796802
}
@@ -842,12 +848,20 @@ - (NSDictionary*)getAdditionalData {
842848
additionalData[@"sound"] = self.lastMessageReceived[@"aps"][@"sound"];
843849
if (self.lastMessageReceived[@"custom"][@"u"] != nil)
844850
additionalData[@"launchURL"] = self.lastMessageReceived[@"custom"][@"u"];
851+
if ([self.lastMessageReceived[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
852+
additionalData[@"title"] = self.lastMessageReceived[@"aps"][@"alert"][@"title"];
845853

846854
return additionalData;
847855
}
848856

849857
- (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 @"";
851865
}
852866

853867
- (void)postNotification:(NSDictionary*)jsonData {
@@ -982,7 +996,6 @@ - (void)setSubscription:(BOOL)enable {
982996
[self sendNotificationTypesUpdateIsConfirmed:false];
983997
}
984998

985-
986999
- (void)didRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSData*)inDeviceToken {
9871000
NSString* trimmedDeviceToken = [[inDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
9881001
NSString* parsedDeviceToken = [[trimmedDeviceToken componentsSeparatedByString:@" "] componentsJoinedByString:@""];
@@ -995,6 +1008,7 @@ - (void)didRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSDat
9951008
}
9961009

9971010
- (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictionary*)userInfo {
1011+
// If 'm' present then the notification has action buttons attached to it.
9981012
if (userInfo[@"m"]) {
9991013
NSDictionary* data = userInfo;
10001014

@@ -1026,7 +1040,13 @@ - (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio
10261040

10271041
UILocalNotification* notification = [[UILocalNotification alloc] init];
10281042
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"];
10301050
notification.userInfo = userInfo;
10311051
notification.soundName = data[@"s"];
10321052
if (notification.soundName == nil)
@@ -1036,7 +1056,7 @@ - (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio
10361056

10371057
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
10381058
}
1039-
else
1059+
else if (application.applicationState != UIApplicationStateBackground)
10401060
[self notificationOpened:userInfo isActive:[application applicationState] == UIApplicationStateActive];
10411061
}
10421062

@@ -1232,11 +1252,15 @@ + (void)load {
12321252
static Class delegateClass = nil;
12331253

12341254
- (void) setOneSignalDelegate:(id<UIApplicationDelegate>)delegate {
1235-
if(delegateClass != nil)
1236-
return;
1255+
if (delegateClass != nil) {
1256+
[self setOneSignalDelegate:delegate];
1257+
return;
1258+
}
1259+
12371260

12381261
delegateClass = getClassWithProtocolInHierarchy([delegate class], @protocol(UIApplicationDelegate));
12391262

1263+
12401264
injectSelector(self.class, @selector(oneSignalRemoteSilentNotification:UserInfo:fetchCompletionHandler:),
12411265
delegateClass, @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:));
12421266

0 commit comments

Comments
 (0)