使用Bmob ---擴展服務(Android消息推送)

1.在Bmob官方網站的下載界面中,選擇下載AndroidSDK,將下載的zip壓縮包進行解壓,得到bmobPush_版本號.jar,然後將它放在項目根目錄下的"libs"目錄中。

2.在應用程序AndroidManifest.xml文件中添加相應的權限:

    <permission
        android:name="cn.bmob.permission.push"
        android:protectionLevel="normal" >
    </permission>

    <uses-permission android:name="cn.bmob.permission.push" /> <!-- 添加自定義的權限 -->
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />

3.在應用程序AndroidManifest.xml文件中註冊BmobPush SDK運行所需的推送服務和消息接收器:

        <service
            android:name="cn.bmob.push.lib.service.PushService"
            android:exported="true"
            android:label="PushService"
            android:permission="cn.bmob.permission.push"
            android:process="cn.bmob.push" >
            <intent-filter>
                <action android:name="cn.bmob.push.lib.service.PushService" />
            </intent-filter>
        </service>

        <receiver android:name="cn.bmob.push.PushReceiver" >
            <intent-filter android:priority="2147483647" > <!-- 優先級加最高 -->
                <!-- 系統啓動完成後會調用 -->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <!-- 解鎖完成後會調用 -->
                <action android:name="android.intent.action.USER_PRESENT" />
                <!-- 監聽網絡連通性 -->
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

        <receiver android:name=".PushReceiver" >
            <intent-filter>
                <action android:name="cn.bmob.push.action.MESSAGE" />
            </intent-filter>
        </receiver>

4.在activity_main.xml文件中添加控件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/push"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/query_btn"
        android:layout_alignRight="@+id/button1"
        android:text="Push"
        android:onClick="Push" />

</RelativeLayout>
5.修改ActivitMain.java文件。
public class MainActivity extends Activity {
	private EditText mName;
	private EditText mFeedback;
	private EditText mQuery;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// BmobPush的SDK初始化,並啓動
		BmobInstallation.getCurrentInstallation(this).save();
		BmobPush.startWork(this, "0f83bf800fee187180e4191c4daecd68");
	}

	public void Push(View view) {
		BmobPushManager push = new BmobPushManager(MainActivity.this);
		push.pushMessageAll("Test");
	}

}

6.在應用程序中創建一個消息接收器。

     Push消息通過action=cn.bmob.push.action.MESSAGE的Intent把數據發送給客戶端PushReceiver,消息格式由應用自己決定,PushService只負責把服務器下發的消息以字符串格式透傳給客戶端。PushReceiver代碼示例如下:

public class PushReceiver extends BroadcastReceiver {
	String message = "";

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
			String msg = intent
					.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

			JSONTokener jsonTokener = new JSONTokener(msg);
			try {
				JSONObject object = (JSONObject) jsonTokener.nextValue();
				message = object.getString("alert");//獲取具體的消息內容
			} catch (JSONException e) {
				e.printStackTrace();
			}
			//把Notification顯示在通知中
			NotificationManager manager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification notification = new Notification(
					R.drawable.ic_launcher, "Notification comes",
					System.currentTimeMillis());
			notification
					.setLatestEventInfo(context, "Bmob Test", message, null);
			manager.notify(R.drawable.ic_launcher, notification);
		}
	}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章