ios 消息服务器,关于IOS APNS推送消息(iphone端+服务端)
這兩天,項目要求做個推送功能,研究了一天,在此跟各位分享下。因為之前做了一年的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/
總結
以上是生活随笔為你收集整理的ios 消息服务器,关于IOS APNS推送消息(iphone端+服务端)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python播放音乐同步歌词_使用Pyt
- 下一篇: convert.todatetime指定