IOS成长之路-推送(通过苹果服务器)
关于推送的机制和推送用到的证书问题在这里不多说。
关于推送的代码部分:
客户端:
1、告诉应用程序,接收push来的消息(当然是放在 didFinishLaunchingWithOptions 方法里面了)
[cpp]
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
2、完成推送比不可缺的东西:deviceToken ,苹果推送会根据 deviceToken的值进行推送的操作。deviceToken和全球之内的苹果设备一一对应的,也就是说它是唯一的。
[cpp]
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"获取设备的deviceToken: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
NSLog(@"Failed to get token, error: %@", error);
- (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、对推送过来的消息进行处理的方法:
[cpp]
- (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];
}
}
补充:移动开发 , IOS ,