代码库> 其他> 关于IOS APNS推送消息(iphone端+服务端)
关于IOS APNS推送消息(iphone端+服务端)
关注
上传者:wuweibin 分类:其他(Others)
查看次数:18058 下载次数:1561
上传时间:2012-10-24 大小:7 B
这两天,项目要求做个推送功能,研究了一天,在此跟各位分享下。因为之前做了一年的php,所以服务端我自己写的,php语言。
1:请求证书,APPID各种繁琐的操作,我就不多话了,我也是看网上例子的。 http://luoyl.info/blog/2012/02/apple_push_notification_guide/
iphone端的代码:


#define push_server @"http://192.168.0.123/push/apns.php"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    /** 注册推送通知功能, */
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    application.applicationIconBadgeNumber = 0;
    //判断程序是不是由推送服务完成的
    if (launchOptions) {
        NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (pushNotificationKey) {
            application.applicationIconBadgeNumber = 0;
        }
    }

}

/** 接收从苹果服务器返回的唯一的设备token,然后发送给自己的服务端*/
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString* devices_token = [NSString stringWithFormat:@"%@",deviceToken];
    NSString* devices_name = [[UIDevice currentDevice] name];
    NSString* devices_version = [[UIDevice currentDevice] systemVersion];
    NSString* devices_type = [[UIDevice currentDevice] model];
    NSString* mode = @"Development";
    NSString *strUrl = [NSString stringWithFormat:@"%@?action=registerDevices&devices_token=%@&devices_name=%@&devices_version=%@&devices_type=%@&mode=%@",
                        push_server,devices_token,devices_name,devices_version,devices_type,mode];
    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strUrl];
    
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    //发送URL请求
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

//程序处于启动状态,或者在后台运行时,会接收到推送消息,解析处理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"
apns -> didReceiveRemoteNotification,Receive Data:
%@", userInfo);
    //把icon上的标记数字设置为0,
    application.applicationIconBadgeNumber = 0;
    if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
        if(application.applicationState ==UIApplicationStateActive){
            [self alertNotice:@"推送通知" withMSG:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] cancleButtonTitle:@"OK" otherButtonTitle:nil];
        }
        NSString *strUrl = [NSString stringWithFormat:@"%@?action=cleanBadgeNumber&id=%@&badge=%d",
                            push_server,[[userInfo objectForKey:@"aps"] objectForKey:@"id"],0];
        strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:strUrl];
        
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        //发送URL请求
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
}




php端代码,下载附件,环境搭配:php+mysql

如果不明白的地方,上微博问我。http://weibo.com/1999711542/
收藏
我来说两句
发表评论
您还没有登录!请登录注册
所有评论(0


本周热门下载
综合评论
提示
sina weixin mail 回到顶部