客戶端:
1井仰、告訴應(yīng)用程序礼预,接收push來(lái)的消息(當(dāng)然是放在didFinishLaunchingWithOptions方法里面了)
[application?registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
2胆萧、完成推送比不可缺的東西:deviceToken若皱,蘋果推送會(huì)根據(jù)deviceToken的值進(jìn)行推送的操作兔毙。deviceToken和全球之內(nèi)的蘋果設(shè)備一一對(duì)應(yīng)的,也就是說(shuō)它是唯一的。
-?(void)application:(UIApplication?*)application?didRegisterForRemoteNotificationsWithDeviceToken:(NSData?*)deviceToken?{
NSLog(@"獲取設(shè)備的deviceToken:?%@",?deviceToken);
}
-?(void)application:(UIApplication*)application?didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
NSLog(@"Failed?to?get?token,?error:?%@",?error);
3疯趟、對(duì)推送過(guò)來(lái)的消息進(jìn)行處理的方法:
-?(void)application:(UIApplication?*)application?didReceiveRemoteNotification:(NSDictionary?*)userInfo
{
//以警告框的方式來(lái)顯示推送消息
if([[userInfo?objectForKey:@"aps"]?objectForKey:@"alert"]!=NULL)?{
UIAlertView*?alert?=?[[UIAlertView?alloc]?initWithTitle:@"經(jīng)過(guò)推送發(fā)送過(guò)來(lái)的消息"
message:[[userInfo?objectForKey:@"aps"]?objectForKey:@"alert"]
delegate:self
cancelButtonTitle:@"關(guān)閉"
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;
}