ios 消息服务器,关于IOS APNS推送消息(iphone端+服务端)

这篇博客分享了如何实现iOS应用的推送通知功能,包括APPID的申请、设备token的获取以及PHP服务端的接口编写。作者通过Objective-C展示了iOS客户端的代码,用于注册远程通知、接收和处理推送消息。同时,提供了PHP端处理设备注册和清理角标的代码片段。文章还提供了参考资料链接和微博账号以便进一步咨询。
摘要由CSDN通过智能技术生成

这两天,项目要求做个推送功能,研究了一天,在此跟各位分享下。因为之前做了一年的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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值