Parse中的Push Notification在Android中的應用
阿新 • • 發佈:2019-01-30
1、下載Parse的SDK
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);
還有很多詳細的細節,請參考官網。