您的位置:首页 > 移动开发 > IOS开发

IOS成长之路-推送(通过苹果服务器)

2013-01-04 09:43 120 查看
关于推送的机制和推送用到的证书问题在这里不多说。

关于推送的代码部分:

客户端:

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];
}
}


php服务器端:

点击打开链接

java服务器端:

点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: