前言
很早就知道Parse服務,專為開發者提供服務端支撐,這又推出了Push服務,正好想用,實踐發現速度快、簡單、好用,這里順便記錄了一下使用心得。
聲明
歡迎轉載,但請保留文章原始出處:)
博客園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com
正文
一、准備
關於Parse的介紹,參考本文文末的鏈接(每月100萬條免費Push)。首先你需要創建一個Parse賬戶,然后創建一個應用,最后下載SDK,解壓並將jar拷貝到libs目錄即可。
官網:Parse Push
你可以嘗試下這個下載鏈接:Parse-1.2.0.zip
二、 在你Application類的onCreate方法中調用Parse.initialize,如下:
import com.parse.Parse;
public class MyApplication extends Application {
public void onCreate() {
Parse.initialize( this, "your application id", "your client key");
}
}
登錄並創建好應用后,點擊網頁頂部的Quickstart就能進入本文的英文版本,並且已經給你填充好了applicationid和key,直接復制到程序里面即可。
三、AndroidMainfest.xml設置
這里設置權限、廣播、Service等。
< uses-permission android:name ="android.permission.ACCESS_NETWORK_STATE" />
< uses-permission android:name ="android.permission.RECEIVE_BOOT_COMPLETED" />
< uses-permission android:name ="android.permission.VIBRATE" />
< receiver android:name ="com.parse.ParseBroadcastReceiver" >
< intent-filter >
< action android:name ="android.intent.action.BOOT_COMPLETED" />
< action android:name ="android.intent.action.USER_PRESENT" />
</ intent-filter >
</ receiver >
注意別加錯地方。
四、訂閱Push通知
PushService.setDefaultPushCallback( this, YourActivity. class);
注意:
a)、最后一個參數YourActivity.class,是指點擊任務欄推送消息時接收處理的Activity,可以從getIntent中取到推送數據,例如 :
com.parse.Channel:null
com.parse.Data:{"alert":"test","push_hash":"098f6bcd4621d373cade4e832627b4f6"}
b)、這段代碼也可以放到Application里面,放在Parse.initialize后面。
c)、以廣播的形式接收JSON數據:
private static final String TAG = "MyCustomReceiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
Iterator itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
五、其他
a). 測試過程中發現,按照快速開發文檔,最后點Send Temp Push沒用,還以為失敗了,直接進入應用后台的Push Notifications,點擊Send a push,然后就可以發送消息了。發送成功一次后,后面都很快了。
b). 注意要在后台Settings的Push notifications中啟用Client push,設置為ON即可。
c). Parse Push支持IOS和Android的通知服務。
d). 1.3后好像要加以下代碼:(2013-06-12更新)
ParseInstallation.getCurrentInstallation().saveInBackground();
六、相關文章
Parse 是一個比較完善的專為您的IOS和Android應用而設計的后端平台
結束
原計划試用一下極光推送,這里先試一下Parse的吧。現在搞APP的很多創業很多,其實專門做這種開發者服務也很好,更加容易實現盈利,同樣最終服務於客戶。創業到底是為了啥了?