标签:des style blog http io color os ar for
In AppDelegate.m, implement the methods below:
//--------------------------------------------- #pragma mark - App Delegate //--------------------------------------------- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //--------------------------------------------- // set push notification //--------------------------------------------- if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { // iOS 8 Notifications [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [application registerForRemoteNotifications]; } else { // iOS < 8 Notifications [application registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; } //--------------------------------------------- //Accept push notification when app is not open //--------------------------------------------- NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteNotification) { // [self handleRemoteNotification:application userInfo:remoteNotif]; } else{ // do nothing .. } return YES; }
//--------------------------------------------- #pragma mark - Push Notifications Delegate //--------------------------------------------- - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { NSLog(@"This device token is: %@", deviceToken); NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""]; // call the server function to register this device // .. } - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { NSLog(@"Failed to get token, error: %@", error); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"didReceiveRemoteNotification"); UIApplicationState applicationState = application.applicationState; if ( applicationState == UIApplicationStateActive ){ // app was already in the foreground NSLog(@"Content: %@", [[userInfo objectForKey: @"aps"] objectForKey: @"alert"]); UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (enabledTypes & UIRemoteNotificationTypeBadge) { //badge number is enabled } if (enabledTypes & UIRemoteNotificationTypeSound) { //sound is enabled } if (enabledTypes & UIRemoteNotificationTypeAlert) { //alert msg is enabled } } else{ // app was just brought from background to foreground } }
Gem Recomend: https://github.com/nomad/Houston
[1]http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
iOS 8 Apple Push Notification Service
标签:des style blog http io color os ar for
原文地址:http://blog.csdn.net/willyang519/article/details/40559225