Last active
August 29, 2015 14:13
-
-
Save zats/f75b3e3667bc0d62b119 to your computer and use it in GitHub Desktop.
Push notifications registration: iOS7 & iOS8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface WMLPushNotificationRegistrationService : NSObject | |
+ (instancetype)sharedInstance; | |
@property (nonatomic, readonly, getter=isRegisteredForPushNotifications) BOOL registeredForPushNotifications; | |
/** | |
* Must be called every app startup, once got user's permission to send push notificaitons | |
*/ | |
- (void)registerForPushNotifications; | |
/** | |
* Updates APN token. This method perform work only if @c isRegisteredForPushNotifications is @c YES. | |
*/ | |
- (void)requestUpdatedTokenForPushNotifications; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "WMLPushNotificationRegistrationService.h" | |
#import "WMLPushNotificationRegistrationService_iOS7.h" | |
#import "WMLPushNotificationRegistrationService_iOS8.h" | |
@implementation WMLPushNotificationRegistrationService | |
+ (instancetype)sharedInstance { | |
// your IOC, static instance etc | |
return [JSObjection defaultInjector][ self ]; | |
} | |
- (instancetype)init { | |
if (![self isMemberOfClass:[WMLPushNotificationRegistrationService class]]) { | |
return [super init]; | |
} | |
NSOperatingSystemVersion version = {8,0,0}; | |
// I backported this method to iOS7, but you can also use -[UIDevice systemVersion] | |
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:version]) { | |
return [[WMLPushNotificationRegistrationService_iOS8 alloc] init]; | |
} | |
return [[WMLPushNotificationRegistrationService_iOS7 alloc] init]; | |
} | |
- (void)registerForPushNotifications { | |
// no-op | |
} | |
- (void)requestUpdatedTokenForPushNotifications { | |
if (self.isRegisteredForPushNotifications) { | |
[self registerForPushNotifications]; | |
} | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "WMLPushNotificationRegistrationService_iOS7.h" | |
@implementation WMLPushNotificationRegistrationService_iOS7 | |
- (BOOL)isRegisteredForPushNotifications { | |
UIRemoteNotificationType registeredTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; | |
return ((registeredTypes & UIRemoteNotificationTypeBadge) | | |
(registeredTypes & UIRemoteNotificationTypeSound) | | |
(registeredTypes & UIRemoteNotificationTypeAlert)); | |
} | |
- (void)registerForPushNotifications { | |
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "WMLPushNotificationRegistrationService_iOS8.h" | |
@implementation WMLPushNotificationRegistrationService_iOS8 | |
- (BOOL)isRegisteredForPushNotifications { | |
if (![[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) { | |
return NO; | |
} | |
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]; | |
return ((settings.types & UIUserNotificationTypeAlert) | | |
(settings.types & UIUserNotificationTypeBadge) | | |
(settings.types & UIUserNotificationTypeSound)); | |
} | |
- (void)registerForPushNotifications { | |
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; | |
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; | |
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; | |
[[UIApplication sharedApplication] registerForRemoteNotifications]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment