?最近一直在弄推送,趁著還記得,把它整理一下。
1.生成推送證書(shū),現(xiàn)在說(shuō)一下開(kāi)發(fā)證書(shū)。在appStore申請(qǐng)一個(gè)推送證書(shū)。這個(gè)應(yīng)該都知道就不細(xì)說(shuō)了。然后下載。在鑰匙串中找到該證書(shū)。
然后單機(jī)“Apple Development Push Services”,導(dǎo)出p12文件,保存為 apns-dev-cert.p12 文件,
擴(kuò)展“Apple Development Push Services” 對(duì)“Private Key”做同樣操作,保存為 apns-dev-key.p12 文件
在終端寫(xiě)命令openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p1 將這些文件轉(zhuǎn)換為PEM格式。
如果你想要移除密碼,要么在導(dǎo)出/轉(zhuǎn)換時(shí)不要設(shè)定或者執(zhí)行 openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem
最后,你需要將鍵和許可文件合成為apns-dev.pem文件,此文件在連接到APNS時(shí)需要使用
cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem
最后生成的這個(gè)證書(shū)發(fā)給后臺(tái)就可以,然后不出意外你的程序就可以接受推送消息了
推送消息處理:
在appdelegate的這個(gè)方法didReceiveRemoteNotification處理推動(dòng)消息,也可以用這個(gè)方法didReceiveRemoteNotification 因?yàn)槲覀冏龅氖穷?lèi)似微信的聊天
所以我全部用的通知來(lái)完成。例如這個(gè)方法處理中
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
//處理推送消息
NSLog(@"收到推送消息:%@",[[userInfoobjectForKey:@"aps"]objectForKey:@"alert"]);
if(userInfo && [userInfoisKindOfClass:[NSDictionaryclass]]){
[UIApplicationsharedApplication].applicationIconBadgeNumber= 0;
NSDictionary*dicMessage =userInfo[@"aps"][@"alert"];
NSUserDefaults*userDefault = [NSUserDefaultsstandardUserDefaults];
NSIntegercurrentPage = [userDefaultintegerForKey:@"CurrentPage"];
NSString*topic_id = [userDefaultobjectForKey:@"topicid"];
if(currentPage == 1&&[dicMessage[@"topic_id"]isEqualToString:topic_id]) {
//創(chuàng)建通知
NSNotification* notice = [NSNotificationnotificationWithName:@"push_text"object:niluserInfo:dicMessage];
//通過(guò)通知中心發(fā)送通知
[[NSNotificationCenterdefaultCenter]postNotification:notice];
}
elseif(![LDCommonUtilisBlankString:dicMessage[@"title"]]) {
NSNotification*notification = [NSNotificationnotificationWithName:@"go_out"object:niluserInfo:dicMessage];
[[NSNotificationCenterdefaultCenter]postNotification:notification];
[LDCommonUtilalertWithTitle:dicMessage[@"title"]msg:dicMessage[@"body"]];
}
elseif(currentPage == 2&& [userDefaultintegerForKey:@"background"]==1){
NSNotification*notification = [NSNotificationnotificationWithName:@"open_page"object:niluserInfo:dicMessage];
[[NSNotificationCenterdefaultCenter]postNotification:notification];
NSUserDefaults*defaulets = [NSUserDefaultsstandardUserDefaults];
[defauletssetInteger:0forKey:@"background"];
[defauletssynchronize];
}
}
}
創(chuàng)建通知,然后再消息界面處理通知。怎么樣,其實(shí)很簡(jiǎn)單吧!