1、下载Parse的SDK

可以到https://www.parse.com/downloads/android/Parse/latest进行下载,或者下载本文附件中的Parse-1.3.0.jar。

2、将SDK中的jar包导入项目的libs目录

3、Activity中的配置

在Activity中导入下面几个包:

import com.parse.Parse;import com.parse.ParseAnalytics;import com.parse.ParseInstallation;import com.parse.PushService;

在onCreate中调用Parse.initialize:

public void onCreate() {     Parse.initialize(this, "TPPUafHVnUnJ6g91v5E0qxRsVEoRRyrj0QCaUzMA", "XGD13UzadLy6espUxPkqkt9Zntiyk2MXeA7qVAxS"); }

设置处理推送消息的Activity:

PushService.setDefaultPushCallback(this, MainActivity.class);ParseInstallation.getCurrentInstallation().saveInBackground();

4、权限配置

<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" />

5、在MainActivity的onCreate中加入以下代码

ParseAnalytics.trackAppOpened(getIntent());

6、在AndroidManifest.xml的</application>前加入以下代码

<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>

以上就是使用Parse中Push Notification的最基本的配置。

接下来,举两个简单的例子:发送消息和接收消息。

1、发送消息

Button sendBtn = (Button)findViewById(R.id.button1);sendBtn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {try {JSONObject data;EditText et = (EditText)findViewById(R.id.editText1);ParsePush push = new ParsePush();String msg = "{\"action\": \"com.example.demo.UPDATE_STATUS\", \"alert\": " + et.getText().toString() + "}";data = new JSONObject(msg);push.setChannel("Giants");push.setData(data);push.sendInBackground();} catch (JSONException e) {e.printStackTrace();}}});

2、接收消息

1)实现一个BroadcastReceiver类

package com.example.demo;import java.util.Iterator;import org.json.JSONException;import org.json.JSONObject;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences.Editor;import android.util.Log;public class MyCustomReceiver extends BroadcastReceiver {private static final String TAG = "MyCustomReceiver";@Overridepublic 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.i(TAG, "got action " + action + " on channel " + channel+ " with:");Iterator itr = json.keys();while (itr.hasNext()) {String key = (String) itr.next();if (key.equals("alert")) {Log.i(TAG, "..." + key + " => " + json.getString(key));Editor sharedata = context.getSharedPreferences("data", 0).edit();sharedata.putString("msg", json.getString(key));sharedata.commit();}}} catch (JSONException e) {Log.i(TAG, "JSONException: " + e.getMessage());}}}

2)注册MyCustomReceiver

<receiver android:name="com.example.demo.MyCustomReceiver" >    <intent-filter>        <action android:name="com.example.demo.UPDATE_STATUS" />    </intent-filter></receiver>

3)在Activity中订阅Giants通道的消息

PushService.subscribe(this, "Giants", MainActivity.class);

还有很多详细的细节,请参考官网。

更多相关文章

  1. Android代码内存优化建议-OnTrimMemory优化
  2. Android -- 消息处理机制源码分析(Looper,Handler,Message)
  3. Gears Android WIFI/基站定位源代码分析
  4. 安卓学习(初)第三章(2)(《第一行代码》)
  5. 《第一行代码Android》阅读笔记
  6. 白话开发——Android Studio代码调试技巧篇
  7. Windows XP下git通过代理下载android代码

随机推荐

  1. Android(安卓)DDMS ADB启动失败错误解决!
  2. android bluetooth蓝牙移植
  3. Android(安卓)绘制一个Loading动画__向图
  4. Android-- Dialog对话框的使用方法
  5. Android2.3 API变更概要
  6. Android(安卓)反编译APK文件
  7. Android(安卓)O中修改NotificationChanne
  8. Android(安卓)Gatekeeper流程深度解剖
  9. Errors running builder 'Android(安卓)R
  10. android画图-----DensityActivity 添加view