iOS推送服务--客户端获取deivceToken代码

要想获得服务推送,要做很多东西,这里只是写一下获取deviceToken的代码

1.在程序入口Delegate.m的applicationdidFinishLaunchingWithOptions函数写以下代码:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
2.同时在Delegate.m写两个两个函数,一个是获取成功调用的函数,一个是失败的时候调用的函数。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"deviceToken: %@", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error in registration. Error: %@", error);
}

3.消息处理,同样在delegate.m文件

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    
    NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
    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];
    }
}

4.前面的工作非常麻烦,我就不写了,网络上很多资源,这里列出几个。

http://www.cnblogs.com/iphone-crash/archive/2011/08/13/2137430.html

http://www.cnblogs.com/yeagen/archive/2012/11/13/2767678.html

http://blog.csdn.net/f520131480315/article/details/6559577

http://blog.csdn.net/linkai5696/article/details/6316929

http://mmz06.blog.163.com/blog/static/121416962011111710934946/

http://blog.csdn.net/sjzsp/article/details/6323070

4.国外的这个很详细

http://mobiforge.com/developing/story/programming-apple-push-notification-services



你可能感兴趣的:(iOS推送服务--客户端获取deivceToken代码)