過去幾個月裡做的項目,我的工作內容包括DB, Front-End, Back-End, iOS App的設計,不過就還沒嘗試過自己寫一套推送訊息(Push Notification)管理的程式。我在推送方面的經驗僅僅在於提供後台給使用者填寫推送內容+時間等,寫入DB中,然後交給現成的程式進行去掃描DB並進行分派推送訊息,當然iOS App方面的推送顯示、設定、申請就玩的多了。
目前還沒打算研究學什麼語言來寫一套推送管理程式,所以我想找個現成的可以馬上使用的方案。於是就想到了被Facebook收購的Parse。
Parse在推送方面的收費方式對於用戶不多的項目來說就是免費的,每個月前100萬條不收費,之後以$0.05/1000條的方式來收費,如果只是用來發送官方的公告,每個禮拜發送一條,都能支持25個設備了,何況不是每個人都會同意接受的……
這邊文章是用來記錄Parse是否能夠實現我想要的功能,並不會提供所有過程中涉及的Code.
大家完全可以用自己最熟悉的技術,畢竟我們只是來玩玩看:D
- Web方面使用到PHP+Codeigniter(PHP MVC Framework), Bootstrap(CSS)
- DB使用MySQL
- iOS App 用Objective-C寫
- Push Notification 使用Parse的服務
準備實現的功能
- 一個列表,用於顯示推送過的內容。
- 一個編輯介面,用於填寫發送的內容。
- 我希望發送過的訊息能保存在我自己的Server上,所以我會建立一個DB。
- 一個iOS App,用於接收推送通知。
iOS部分
首先可以先通過教程裝上Parse的SDK,有Parse官方的教學。
這邊先請求用戶允許推送通知,並設定下Parse的AppID以及ClientKey
1 2 3 4 5 6 7 8 9 10 11 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil]; [application registerUserNotificationSettings:settings]; [application registerForRemoteNotifications]; [Parse setApplicationId:@"XXXXXXX" clientKey:@"XXXXXXX"]; return YES; } |
當用戶允許以後,我們就將Device Key發送給Parse。
1 2 3 4 5 6 |
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Store the deviceToken in the current Installation and save it to Parse PFInstallation *currentInstallation = [PFInstallation currentInstallation]; [currentInstallation setDeviceTokenFromData:deviceToken]; [currentInstallation saveInBackground]; } |
因為當App開著的時候,收到推送通知並不會跳出banner而是交給我們自己處理,所以這裡為了方便看到訊息,可以使用Parse自帶的簡單對話框方法。
1 2 3 |
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [PFPush handlePush:userInfo]; } |
接下來,將App Build到手機上並執行,就可以到Parse上看到Token上傳的數據了。
到這邊iOS App的部分就完成了,接下來就是到實現Web調用Parse推送訊息到App上,並且將每次推送的訊息保存一份在DB中。
Web部分
在DB裡面創建一個簡單的表用來保存推送過的數據。
用於呈現歷史推送紀錄的列表:
建立推送訊息的介面:
Parse有非常豐富的文檔可以參考, 這邊我使用PHP調用Parse REST的接口來進行推送訊息。
這邊有Parse在申請證書方面的教學。
PHP 調用Parse REST接口實現推送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
function send_push_notification($message){ $url = 'https://api.parse.com/1/push'; $appId = 'XXXXXXX'; $restKey = 'XXXXXXX'; $target_device = 'ghS68OjM7t'; // using object Id of target Installation. $push_payload = json_encode(array( "where" => array( "objectId" => $target_device, ), "data" => array( "alert" => $message ) )); $rest = curl_init(); curl_setopt($rest, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($rest, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($rest,CURLOPT_URL,$url); curl_setopt($rest,CURLOPT_PORT,443); curl_setopt($rest,CURLOPT_POST,1); curl_setopt($rest,CURLOPT_POSTFIELDS,$push_payload); curl_setopt($rest,CURLOPT_HTTPHEADER, array("X-Parse-Application-Id: " . $appId, "X-Parse-REST-API-Key: " . $restKey, "Content-Type: application/json")); $response = curl_exec($rest); if($response['result']){ return true; }else{ return false; } } |
- AppID以及Rest ID都可以在Parse裡的App介面找到(Setting->Keys)。
- where的部分可以自定義,我這裡設置只推送到我那台手機上。
- 下面這兩行是因為我是在自己電腦上運行,沒有SSL服務,所以要false掉,不然沒有反應……
1 2 |
curl_setopt($rest, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($rest, CURLOPT_SSL_VERIFYHOST, false); |