Skip to content

Instantly share code, notes, and snippets.

@atkit
Last active August 29, 2015 14:07
Show Gist options
  • Save atkit/2e651db9ffeebc635f88 to your computer and use it in GitHub Desktop.
Save atkit/2e651db9ffeebc635f88 to your computer and use it in GitHub Desktop.
iOS8 Push Notifications Tricks (Category)
#import <UIKit/UIKit.h>
@interface UIApplication (Notifications)
- (void) registerForAllNotificationTypes;
@end
#import "UIApplication+Notifications.h"
@implementation UIApplication (Notifications)
- (UIUserNotificationSettings*) createUserNotificationSettings
{
UIUserNotificationType userNotificationTypes = ( UIUserNotificationTypeSound
| UIUserNotificationTypeAlert
| UIUserNotificationTypeBadge);
return [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
}
- (void) registerForAllNotificationTypes
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
if ([self respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[self registerUserNotificationSettings:[self createUserNotificationSettings]];
[self registerForRemoteNotifications];
} else
#endif
{
[self registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge
| UIRemoteNotificationTypeAlert
| UIRemoteNotificationTypeSound];
}
}
@end
#import "UIApplication+Notifications.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application registerForAllNotificationTypes];
// some other logic can be here
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// now you have deviceToken
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment