两个代理方法:
//获取diviceToken
1- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString* dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
NSString *token = [dt stringByReplacingOccurrencesOfString:@" " withString:@""];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userid = [defaults objectForKey:user_id];
// NSString *reqKey = [defaults objectForKey:REQ_KEY];
NSString *urlStr;
if (userid!=nil) {
urlStr = [NSString stringWithFormat:@"http://b2c.ezparking.com.cn/rtpi-service/misc/deviceToken.do?deviceId=%@&memberId=%@&deviceToken=%@",[OpenUDID value],userid,token];
}
else
{
urlStr = [NSString stringWithFormat:@"http://b2c.ezparking.com.cn/rtpi-service/misc/deviceToken.do?deviceId=%@&deviceToken=%@",[OpenUDID value],token];
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// manager.requestSerializer.timeoutInterval = 15;
// NSString *urlStr = [NSString stringWithFormat:@"http://b2c.ezparking.com.cn/rtpi-service/misc/deviceToken.do?key=%@&deviceId=%@",reqKey,strPhone];
[manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * operation, id retDict)
{
LogPark(@"%@",retDict);
} failure:^(AFHTTPRequestOperation * operation, NSError * error) {
}];
}
用户允许通知后,会在自己服务器的后台注册,执行这个代理方法。
//远程通知后 接受到的数据
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *dictRemote = [userInfo objectForKey:@"aps"];
NSArray *arrContent = [[dictRemote objectForKey:@"alert"] componentsSeparatedByString:@"\n"];
if (arrContent.count>1) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[arrContent objectAtIndex:0] message:[arrContent objectAtIndex:1] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:[dictRemote objectForKey:@"alert"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
}
这个是接收到通知之后,会在处接受到的通知信息!
//远程推送注册方法:
- (void)registerAPNs
{
//在ios8之后的方法:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)])
{
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
//在ios8之前的方法:
UIRemoteNotificationType types = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
}
}