客户端:
1、告诉应用程序,接收push来的消息(当然是放在didFinishLaunchingWithOptions方法里面了)
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
2、完成推送比不可缺的东西:deviceToken,苹果推送会根据deviceToken的值进行推送的操作。deviceToken和全球之内的苹果设备一一对应的,也就是说它是唯一的。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"获取设备的deviceToken: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
NSLog(@"Failed to get token, error: %@", error);
3、对推送过来的消息进行处理的方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//以警告框的方式来显示推送消息
if([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"经过推送发送过来的消息"
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
delegate:self
cancelButtonTitle:@"关闭"
otherButtonTitles:@"处理",nil];
[alert show];
[alert release];
}
}
#pragma mark是否接收推送消息
- (BOOL)isAllowedNotification {
if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {
UIUserNotificationSettings*setting = [[UIApplicationsharedApplication]currentUserNotificationSettings];
if(UIUserNotificationTypeNone!= setting.types) {
returnYES;
}
}else{
UIRemoteNotificationTypetype = [[UIApplicationsharedApplication]enabledRemoteNotificationTypes];
if(UIRemoteNotificationTypeNone!= type)
returnYES;
}
returnNO;
}