http://www.cocoachina.com/bbs/read.php?tid-123567.html
#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
];
}
}