Skip to content

Commit

Permalink
- OneSignal will now detect and show an error message for 'No Push To…
Browse files Browse the repository at this point in the history
…ken' issues when the Push Notification capability is not enabled for the app target.

- Fixed an iOS 10 issue where silent notifications fail to load properly when the app is in the foreground.
- Fixed a 'No Push Token' issue in 2.1.10 that would happen when OneSignal was downloaded through CocoaPods and included other pods that used the -ObjC flag.
  • Loading branch information
Joseph Kalash committed Sep 23, 2016
1 parent 7e7f6f8 commit 2c02026
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 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 = "2.1.10"
s.version = "2.1.11"
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.
36 changes: 29 additions & 7 deletions iOS_SDK/OneSignal/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
#import <sys/sysctl.h>
#import <objc/runtime.h>

#import "UIApplication+Swizzling.m"

#define NOTIFICATION_TYPE_NONE 0
#define NOTIFICATION_TYPE_BADGE 1
#define NOTIFICATION_TYPE_SOUND 2
#define NOTIFICATION_TYPE_ALERT 4
#define NOTIFICATION_TYPE_ALL 7

#define ERROR_PUSH_CAPABLILITY_DISABLED -13

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"

Expand All @@ -75,7 +75,7 @@

@implementation OneSignal

NSString* const ONESIGNAL_VERSION = @"020110";
NSString* const ONESIGNAL_VERSION = @"020111";
static NSString* mSDKType = @"native";
static BOOL coldStartFromTapOnNotification = NO;
static BOOL registeredWithApple = NO; //Has attempted to register for push notifications with Apple.
Expand Down Expand Up @@ -719,6 +719,10 @@ + (void)registerUser {
if (mDeviceToken)
idsAvailableBlockWhenReady = nil;
}

//If we got a userId after the notificationTypes were set to error -13, update server
if(mNotificationTypes == ERROR_PUSH_CAPABLILITY_DISABLED)
[self setErrorNotificationType];
}
} onFailure:^(NSError* error) {
waitingForOneSReg = false;
Expand All @@ -740,12 +744,10 @@ + (void) sendNotificationTypesUpdate:(BOOL)isNewType {
if (mNotificationTypes != -1 && mUserId && (isNewType || mNotificationTypes != [self getNotificationTypes])) {
mNotificationTypes = [self getNotificationTypes];
NSMutableURLRequest* request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", mUserId]];

NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
app_id, @"app_id",
[NSNumber numberWithInt:mNotificationTypes], @"notification_types",
@(mNotificationTypes), @"notification_types",
nil];

NSData* postData = [NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil];
[request setHTTPBody:postData];

Expand Down Expand Up @@ -938,8 +940,12 @@ + (BOOL) clearBadgeCount:(BOOL)fromNotifOpened {
}

+ (int) getNotificationTypes {

if(mNotificationTypes == ERROR_PUSH_CAPABLILITY_DISABLED)
return mNotificationTypes;

if (!mSubscriptionSet)
return -2;
return -2;

if (mDeviceToken) {
if ([OneSignalHelper isCapableOfGettingNotificationTypes])
Expand All @@ -951,6 +957,22 @@ + (int) getNotificationTypes {
return -1;
}

+ (void)setErrorNotificationType {
mNotificationTypes = ERROR_PUSH_CAPABLILITY_DISABLED;

if([self mUserId]) {
NSMutableURLRequest* request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", mUserId]];
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
app_id, @"app_id",
@(mNotificationTypes), @"notification_types",
nil];
NSData* postData = [NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil];
[request setHTTPBody:postData];

[OneSignalHelper enqueueRequest:request onSuccess:nil onFailure:nil];
}
}

// iOS 8.0+ only
+ (void) updateNotificationTypes:(int)notificationTypes {

Expand Down
11 changes: 9 additions & 2 deletions iOS_SDK/OneSignal/UIApplication+Swizzling.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ + (void) remoteSilentNotification:(UIApplication*)application UserInfo:(NSDictio
+ (void) updateNotificationTypes:(int)notificationTypes;
+ (void) notificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isActive;
+ (void) processLocalActionBasedNotification:(UILocalNotification*) notification identifier:(NSString*)identifier;
+ (void) setErrorNotificationType;
@end

@interface OneSignalTracker ()
Expand Down Expand Up @@ -150,7 +151,13 @@ - (void)oneSignalDidRegisterForRemoteNotifications:(UIApplication*)app deviceTok

- (void)oneSignalDidFailRegisterForRemoteNotification:(UIApplication*)app error:(NSError*)err {

if([OneSignal app_id])
if(err.code == 3000 && [(NSString*)[err.userInfo objectForKey:NSLocalizedDescriptionKey] containsString:@"no valid 'aps-environment'"]) {
//User did not enable push notification capability
[OneSignal setErrorNotificationType];
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:@"'Push Notification' capability not turned on. Make sure it is enabled by going to your Project Target -> Capability."];
}

else if([OneSignal app_id])
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat: @"Error registering for Apple push notifications. Error: %@", err]];

if ([self respondsToSelector:@selector(oneSignalDidFailRegisterForRemoteNotification:error:)])
Expand Down Expand Up @@ -184,7 +191,7 @@ - (void) oneSignalRemoteSilentNotification:(UIApplication*)application UserInfo:

//Call notificationAction if app is active -> not a silent notification but rather user tap on notification
//Unless iOS 10+ then call remoteSilentNotification instead.
if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
if([UIApplication sharedApplication].applicationState == UIApplicationStateActive && userInfo[@"aps"][@"alert"])
[OneSignal notificationOpened:userInfo isActive:YES];
else [OneSignal remoteSilentNotification:application UserInfo:userInfo];

Expand Down

0 comments on commit 2c02026

Please sign in to comment.