iPhone應(yīng)用 編寫(xiě)Push Notification服務(wù)器端發(fā)送通知
iPhone應(yīng)用 編寫(xiě)Push Notification服務(wù)器端發(fā)送通知是本文要介紹的內(nèi)容,在編寫(xiě)push notification之獲取device token中拿到device token以后,需要把token字符串發(fā)送給應(yīng)用的服務(wù)器端,即provider。
provider將token號(hào)、通知內(nèi)容、通知形式(比如是否彈出提示窗口、是否發(fā)聲等)發(fā)送給蘋(píng)果的服務(wù)器(apns)。
最簡(jiǎn)單的provider實(shí)現(xiàn),其實(shí)就是通過(guò)證書(shū),和蘋(píng)果服務(wù)器建立安全連接(tsl或ssl),通過(guò)認(rèn)證建立連接后,向蘋(píng)果服務(wù)器發(fā)送符合蘋(píng)果要求的數(shù)據(jù)流。
獲得證書(shū)
蘋(píng)果提供兩種接入方式的證書(shū):
developer,用于測(cè)試
production,用于產(chǎn)品
如果是內(nèi)部測(cè)試,使用developer方式即可。
下載證書(shū),通過(guò)ios provisioning portal:
這要求:
登錄的apple developer program帳號(hào)必須是級(jí)別***的agent(這是針對(duì)企業(yè)帳號(hào)來(lái)說(shuō)的,如果是個(gè)人帳號(hào)就無(wú)所謂了),agent帳號(hào)即創(chuàng)始帳號(hào),否則看不到configure鏈接;
必須經(jīng)過(guò)configure操作,已經(jīng)enable了developer和product。
然后進(jìn)入configure鏈接,點(diǎn)擊download按鈕即可:
處理證書(shū)
如果是編寫(xiě)在mac下跑的objc程序,無(wú)需對(duì)證書(shū)做處理,可跳過(guò)這一步。
如果是在java下使用,需要把打證書(shū)用的私有專(zhuān)用密鑰和上述的支持通知的證書(shū)(注意,不是iphone developer證書(shū))合并導(dǎo)出。
生成證書(shū):
點(diǎn)擊存儲(chǔ)的時(shí)候,會(huì)提示生成一個(gè)文件密碼:
當(dāng)然可以密碼為空。
之后會(huì)提示:
這里需要輸入mac登錄用戶(hù)的密碼。
文件生成。
編寫(xiě)發(fā)送通知的實(shí)例
如果是編寫(xiě)mac代碼,有一個(gè)現(xiàn)成的項(xiàng)目可用:http://stefan.hafeneger.name/download/PushMeBabySource.zip
導(dǎo)入到xcode中,只需將:
deviceToken填寫(xiě)成設(shè)備的token字符串,另外,pathForResource改為上面圖中的:
- aps_developer_identity
另外,要把剛才獲得證書(shū)步驟中下載的證書(shū)復(fù)制到xcode項(xiàng)目Resources目錄下:
可以看到文件名和上面的pathForResource的參數(shù)一致。
之后運(yùn)行程序就可以在設(shè)備上收到推送通知。
如果是用java編寫(xiě),可以用第三方庫(kù),見(jiàn):
http://code.google.com/p/javapns/
編寫(xiě)簡(jiǎn)單的發(fā)送通知代碼:
- import org.json.JSONException;
- import javapns.back.PushNotificationManager;
- import javapns.back.SSLConnectionHelper;
- import javapns.data.Device;
- import javapns.data.PayLoad;
- public class Main {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- PayLoad simplePayLoad = new PayLoad();
- // Get PushNotification Instance
- PushNotificationManager pushManager = PushNotificationManager.getInstance();
- // Link iPhone’s UDID (64-char device token) to a stringName
- pushManager.addDevice("iPhone", "00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ");
- simplePayLoad.addAlert("My alert message測(cè)試");
- simplePayLoad.addBadge(1);
- simplePayLoad.addSound("default");
- Device client = PushNotificationManager.getInstance().getDevice("iPhone");
- PushNotificationManager.getInstance().initializeConnection("gateway.sandbox.push.apple.com",
- 2195, "/home/ubuntu/mypush.p12", "password", SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
- PushNotificationManager.getInstance().sendNotification(client, simplePayLoad);
測(cè)試中文沒(méi)有亂碼問(wèn)題。
編寫(xiě)比較復(fù)雜的使用示例(可以控制通知是否有提示窗口、是否有提醒聲音):
aPayload.addBadge( 2),顯示在手機(jī)應(yīng)用圖標(biāo)上的數(shù)字
aPayload.addAlert("軟件版本有更新"),顯示提示窗口文字
aPayload.addSound("default.wav"),指定提示聲音
另外,也可以使用php的第三方實(shí)現(xiàn),比如:
http://code.google.com/p/php-apns
基本原理是啟動(dòng)一個(gè)php服務(wù),監(jiān)控memcacheq隊(duì)列,如果有消息就發(fā)送給蘋(píng)果服務(wù)器。
小結(jié):iPhone應(yīng)用 編寫(xiě)Push Notification服務(wù)器端發(fā)送通知的內(nèi)容介紹完了,希望本文對(duì)你有所幫助。