Skip to content

Instantly share code, notes, and snippets.

@zats
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zats/f75b3e3667bc0d62b119 to your computer and use it in GitHub Desktop.
Save zats/f75b3e3667bc0d62b119 to your computer and use it in GitHub Desktop.
Push notifications registration: iOS7 & iOS8
#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
#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
#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
#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