分享代碼:關於IOS APNS推送消息(iphone端+PHP服務端)

tags:    時間:2013-12-03 21:42:55
項目要求做個推送功能,研究了一天,在此跟各位分享下。因為之前做了一年的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
------解決方案--------------------
好東西啊。。。

推薦閱讀文章

Bookmark the permalink ,來源:互聯網