Skip to content

Commit

Permalink
Title support, notification clearing & open count
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jkasten2 committed Aug 11, 2015
1 parent a4fa1a3 commit 8040e6f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion OneSignal.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "OneSignal"
s.version = "1.9.2"
s.version = "1.10.0"
s.summary = "OneSignal push notification library for mobile apps."
s.homepage = "https://onesignal.com"
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
Binary file modified iOS_SDK/Framework/OneSignal.framework/Versions/A/OneSignal
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
<key>IDESourceControlProjectIdentifier</key>
<string>82DF9B41-8B65-40E7-832A-C61D19DFA081</string>
<key>IDESourceControlProjectName</key>
<string>project</string>
<string>OneSignal</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>5A4AFF41A71B5F8D4741699EC46766398BC4FE44</key>
<string>https://github.com/Hiptic/GameThrive</string>
<string>https://github.com/Hiptic/OneSignal</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>SDK/iOS/iOS_SDK/OneSignal.xcodeproj/project.xcworkspace</string>
<string>SDK/iOS/iOS_SDK/OneSignal.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>5A4AFF41A71B5F8D4741699EC46766398BC4FE44</key>
<string>../../../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/Hiptic/GameThrive</string>
<string>https://github.com/Hiptic/OneSignal</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
Expand Down
46 changes: 31 additions & 15 deletions iOS_SDK/OneSignal/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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

NSString* const VERSION = @"010902";
NSString* const VERSION = @"011000";

#define NOTIFICATION_TYPE_BADGE 1
#define NOTIFICATION_TYPE_SOUND 2
Expand Down Expand Up @@ -187,7 +187,7 @@ - (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
[self notificationOpened:userInfo isActive:false];
}

clearBadgeCount();
clearBadgeCount(false);

if ([OneSignalTrackIAP canTrack])
trackIAPPurchase = [[OneSignalTrackIAP alloc] init];
Expand Down Expand Up @@ -611,7 +611,7 @@ - (void)onFocus:(NSString*)state {
lastTrackedTime = [NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]];

[self sendNotificationTypesUpdateIsConfirmed:false];
wasBadgeSet = clearBadgeCount();
wasBadgeSet = clearBadgeCount(false);
}
else {
NSNumber* timeElapsed = @(([[NSDate date] timeIntervalSince1970] - [lastTrackedTime longLongValue]) + 0.5);
Expand Down Expand Up @@ -775,7 +775,7 @@ - (void) handleNotificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isAc

self.lastMessageReceived = messageDict;

clearBadgeCount();
clearBadgeCount(true);
[[UIApplication sharedApplication] cancelAllLocalNotifications];


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

bool clearBadgeCount() {
bool clearBadgeCount(bool fromNotifOpened) {
if (mNotificationTypes == -1 || (mNotificationTypes & NOTIFICATION_TYPE_BADGE) == 0)
return false;

bool wasBadgeSet = false;

if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0)
wasBadgeSet = true;
bool wasBadgeSet = [UIApplication sharedApplication].applicationIconBadgeNumber > 0;

// Clear bages and nofiications from this app. Setting to 1 then 0 was needed to clear the notifications.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
if ((!(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) && fromNotifOpened) || wasBadgeSet) {
// Clear bages and nofiications from this app.
// Setting to 1 then 0 was needed to clear the notifications on iOS 6 & 7. (Otherwise you can click the notification multiple times.)
// iOS 8+ auto dismisses the notificaiton you tap on so only clear the badge (and notifications [side-effect]) if it was set.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

return wasBadgeSet;
}
Expand Down Expand Up @@ -847,12 +848,20 @@ - (NSDictionary*)getAdditionalData {
additionalData[@"sound"] = self.lastMessageReceived[@"aps"][@"sound"];
if (self.lastMessageReceived[@"custom"][@"u"] != nil)
additionalData[@"launchURL"] = self.lastMessageReceived[@"custom"][@"u"];
if ([self.lastMessageReceived[@"aps"][@"alert"] isKindOfClass:[NSDictionary class]])
additionalData[@"title"] = self.lastMessageReceived[@"aps"][@"alert"][@"title"];

return additionalData;
}

- (NSString*)getMessageString {
return self.lastMessageReceived[@"aps"][@"alert"];
id alertObj = self.lastMessageReceived[@"aps"][@"alert"];
if ([alertObj isKindOfClass:[NSString class]])
return alertObj;
else if ([alertObj isKindOfClass:[NSDictionary class]])
return alertObj[@"body"];

return @"";
}

- (void)postNotification:(NSDictionary*)jsonData {
Expand Down Expand Up @@ -999,6 +1008,7 @@ - (void)didRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSDat
}

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

Expand Down Expand Up @@ -1030,7 +1040,13 @@ - (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio

UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.category = [category identifier];
notification.alertBody = data[@"m"];
if ([data[@"m"] isKindOfClass:[NSDictionary class]]) {
if ([notification respondsToSelector:NSSelectorFromString(@"alertTitle")])
notification.alertTitle = data[@"m"][@"title"];
notification.alertBody = data[@"m"][@"body"];
}
else
notification.alertBody = data[@"m"];
notification.userInfo = userInfo;
notification.soundName = data[@"s"];
if (notification.soundName == nil)
Expand All @@ -1040,7 +1056,7 @@ - (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio

[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
else
else if (application.applicationState != UIApplicationStateBackground)
[self notificationOpened:userInfo isActive:[application applicationState] == UIApplicationStateActive];
}

Expand Down

0 comments on commit 8040e6f

Please sign in to comment.