Parse中的Push Notification在Android中的应用

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(){
	@Override
	public 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";

	@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.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);

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

猜你喜欢

转载自eric-gao.iteye.com/blog/1885557