Created
July 8, 2014 09:27
-
-
Save zhangxigithub/e4dd7d6736ab9f0d1f66 to your computer and use it in GitHub Desktop.
ZXRemoteNotification
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
| // | |
| // ZXRemoteNotification.h | |
| // ZXPush | |
| // | |
| // Created by 张玺 on 12-8-13. | |
| // Copyright (c) 2012年 张玺. All rights reserved. | |
| // | |
| #import <Foundation/Foundation.h> | |
| @interface UIResponder (ZXRemoteNotification) | |
| +(void)registerRemote; | |
| +(void)addDevice:(NSData *)token; | |
| @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
| // | |
| // ZXRemoteNotification.m | |
| // ZXPush | |
| // | |
| // Created by 张玺 on 12-8-13. | |
| // Copyright (c) 2012年 张玺. All rights reserved. | |
| // | |
| #import "ZXRemoteNotification.h" | |
| @implementation UIResponder (ZXRemoteNotification) | |
| +(void)registerRemote | |
| { | |
| [[UIApplication sharedApplication] | |
| registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | | |
| UIRemoteNotificationTypeSound | | |
| UIRemoteNotificationTypeAlert | |
| )]; | |
| } | |
| +(void)addDevice:(NSData *)token | |
| { | |
| NSString* deviceToken = [[[[token description] | |
| stringByReplacingOccurrencesOfString: @"<" withString: @""] | |
| stringByReplacingOccurrencesOfString: @">" withString: @""] | |
| stringByReplacingOccurrencesOfString: @" " withString: @""]; | |
| NSString *oldToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"zxToken"]; | |
| [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"zxToken"]; | |
| [[NSUserDefaults standardUserDefaults] synchronize]; | |
| if(oldToken != nil && [deviceToken isEqualToString:oldToken]) | |
| return; | |
| NSString *address = [NSString stringWithFormat:@"http://xiaobingfm.sinaapp.com/apns/add.php?device=%@&app=%@",deviceToken,[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]]; | |
| NSURL *url = [NSURL URLWithString:address]; | |
| NSLog(@"send"); | |
| NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
| NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:nil]; | |
| [connection start]; | |
| } | |
| - (void)application:(UIApplication *)application | |
| didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { | |
| [UIResponder addDevice:deviceToken]; | |
| } | |
| - (void)application:(UIApplication *)application | |
| didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { | |
| NSLog(@"Error in registration. Error: %@", error); | |
| } | |
| - (void)application:(UIApplication *)application | |
| didReceiveRemoteNotification:(NSDictionary *)userInfo { | |
| /* | |
| 收到消息自定义事件 | |
| */ | |
| NSLog(@"%@",userInfo); | |
| if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"] != nil) { | |
| UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"通知" | |
| message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] | |
| delegate:self | |
| cancelButtonTitle:@"确定" | |
| otherButtonTitles:nil]; | |
| [alert show]; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment