项目要求做个推送功能,研究了一天,在此跟各位分享下。因为之前做了一年的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(@"\napns -> didReceiveRemoteNotification,Receive Data:\n%@", 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端代码,下载地址:http://www.cocoachina.com/bbs/job.php?action=download&aid=47929,环境搭配:php+mysql如果不明白的地方,上微博问我。
http://weibo.com/1999711542/ @吴伟滨Double
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(@"\napns -> didReceiveRemoteNotification,Receive Data:\n%@", 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端代码,下载地址:http://www.cocoachina.com/bbs/job.php?action=download&aid=47929,环境搭配:php+mysql如果不明白的地方,上微博问我。
http://weibo.com/1999711542/ @吴伟滨Double
解决方案 »
- iOS中scrollview自动滚动的实现
- 本人开发iPhone客户端,后台是PHP+MYSQL,想通过webService访问。
- iPhone项目的脚本构建问题
- 栈是什么?有多大?栈和栈区是同一概念吗??
- @@ansi转utf-8 Initializing 'char *' with an expression of type 'const void *' dis
- 求教button如何实现按下之后更改按钮背景图片
- 错误: dyld Symbol not found
- 求iphone开发视频教程
- 开发员必备的工具网站,找图标很方便~
- MAC下开发 USB 识别程序(类似Windows下)热插和热拔
- BLE 用什么函数 接收设备发送的 notify
- 【求助】开发IAP(程序内购买)程序时遇到的账号验证问题
// 发布地址:gateway.push.apple.com:2195 // 测试通过
require_once "/home/bae/app/include/db.php";
$db_obj = new DB();
$sql = "select appleID from appleDevice ";
$result = $db_obj->db_array($sql);
$countArray = count($result);
$passphrase = 'zhaojian';
$message = '开发-推送-测试!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
$payload = json_encode($body); for($i = 0; $i < $countArray; $i++)
{
$deviceToken = $result[$i]['appleID'];
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result1 = fwrite($fp, $msg, strlen($msg));
if (!$result1)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
}
fclose($fp); ?>
请大神们指教!