一、准备
关于Parse的介绍,参考本文文末的链接(每月100万条免费Push)。首先你需要创建一个Parse账户,然后创建一个应用,最后下载SDK,解压并将jar拷贝到libs目录即可。
官网:Parse Push
你可以尝试下这个下载链接:Parse-1.2.0.zip
二、 在你Application类的onCreate方法中调用Parse.initialize,如下:
import android.app.Application;
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.INTERNET"
/>
<
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"
/>
<
service
android:name
="com.parse.PushService"
/>
<
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.subscribe(
this, "", YourActivity.
class);
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数据:
public
class MyCustomReceiver
extends BroadcastReceiver {
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();
六、相关文章
Android推送实现方案探讨
Parse 是一个比较完善的专为您的IOS和Android应用而设计的后端平台
谈谈移动应用开发的辅助服务
本文转自博客园农民伯伯的博客,原文链接:【Android】Parse Push快速入门指南,如需转载请自行联系原博主。