Android Open Source - BaiduPush My Push Message Receiver






From Project

Back to project page BaiduPush.

License

The source code is released under:

Apache License

If you think the Android project BaiduPush listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.baidu.push.example;
/*from  w w  w. j  a  va2 s  . c o  m*/
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.baidu.frontia.api.FrontiaPushMessageReceiver;

/**
 * Push????????receiver?????????????? ??????? onBind?????????????startWork????
 * onMessage?????????????? onSetTags??onDelTags??onListTags?tag?????????
 * onNotificationClicked?????????? onUnbind?stopWork??????????
 * 
 * ?????errorCode?????? 
 *  0 - Success
 *  10001 - Network Problem
 *  30600 - Internal Server Error
 *  30601 - Method Not Allowed 
 *  30602 - Request Params Not Valid
 *  30603 - Authentication Failed 
 *  30604 - Quota Use Up Payment Required 
 *  30605 - Data Required Not Found 
 *  30606 - Request Time Expires Timeout 
 *  30607 - Channel Token Timeout 
 *  30608 - Bind Relation Not Found 
 *  30609 - Bind Number Too Many
 * 
 * ??????????????????????????????????????requestId?errorCode???????????
 * 
 */
public class MyPushMessageReceiver extends FrontiaPushMessageReceiver {
    /** TAG to Log */
    public static final String TAG = MyPushMessageReceiver.class
            .getSimpleName();

    /**
     * ??PushManager.startWork????sdk??push
     * server???????????????????????????onBind??? ??????????????????????????channel
     * id?user id?????server??????server?????channel id?user id???????????????
     * 
     * @param context
     *            BroadcastReceiver???Context
     * @param errorCode
     *            ??????????0 - ???
     * @param appid
     *            ??id?errorCode???0??null
     * @param userId
     *            ??user id?errorCode???0??null
     * @param channelId
     *            ??channel id?errorCode???0??null
     * @param requestId
     *            ??????????????id??????????
     * @return none
     */
    @Override
    public void onBind(Context context, int errorCode, String appid,
            String userId, String channelId, String requestId) {
        String responseString = "onBind errorCode=" + errorCode + " appid="
                + appid + " userId=" + userId + " channelId=" + channelId
                + " requestId=" + requestId;
        Log.d(TAG, responseString);

        // ???????????flag?????????????????????
        if (errorCode == 0) {
            Utils.setBind(context, true);
        }
        // Demo?????????????????????????????
        updateContent(context, responseString);
    }

    /**
     * ?????????????
     * 
     * @param context
     *            ???
     * @param message
     *            ????????
     * @param customContentString
     *            ?????,????json???
     */
    @Override
    public void onMessage(Context context, String message,
            String customContentString) {
        String messageString = "??????? message=\"" + message
                + "\" customContentString=" + customContentString;
        Log.d(TAG, messageString);

        // ?????????????mykey?myvalue?????????????????????????
        if (!TextUtils.isEmpty(customContentString)) {
            JSONObject customJson = null;
            try {
                customJson = new JSONObject(customContentString);
                String myvalue = null;
                if (!customJson.isNull("mykey")) {
                    myvalue = customJson.getString("mykey");
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        // Demo?????????????????????????????
        updateContent(context, messageString);
    }

    /**
     * ?????????????????????????????????????????????
     * 
     * @param context
     *            ???
     * @param title
     *            ?????????
     * @param description
     *            ???????????
     * @param customContentString
     *            ??????????json???
     */
    @Override
    public void onNotificationClicked(Context context, String title,
            String description, String customContentString) {
        String notifyString = "???? title=\"" + title + "\" description=\""
                + description + "\" customContent=" + customContentString;
        Log.d(TAG, notifyString);

        // ?????????????mykey?myvalue????????????????????
        if (!TextUtils.isEmpty(customContentString)) {
            JSONObject customJson = null;
            try {
                customJson = new JSONObject(customContentString);
                String myvalue = null;
                if (!customJson.isNull("mykey")) {
                    myvalue = customJson.getString("mykey");
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        // Demo?????????????????????????????
        updateContent(context, notifyString);
    }

    /**
     * setTags() ??????
     * 
     * @param context
     *            ???
     * @param errorCode
     *            ?????0?????tag????????????0????tag?????????
     * @param successTags
     *            ??????tag
     * @param failTags
     *            ?????tag
     * @param requestId
     *            ?????????????id
     */
    @Override
    public void onSetTags(Context context, int errorCode,
            List<String> sucessTags, List<String> failTags, String requestId) {
        String responseString = "onSetTags errorCode=" + errorCode
                + " sucessTags=" + sucessTags + " failTags=" + failTags
                + " requestId=" + requestId;
        Log.d(TAG, responseString);

        // Demo?????????????????????????????
        updateContent(context, responseString);
    }

    /**
     * delTags() ??????
     * 
     * @param context
     *            ???
     * @param errorCode
     *            ?????0?????tag????????????0????tag????????
     * @param successTags
     *            ??????tag
     * @param failTags
     *            ?????tag
     * @param requestId
     *            ?????????????id
     */
    @Override
    public void onDelTags(Context context, int errorCode,
            List<String> sucessTags, List<String> failTags, String requestId) {
        String responseString = "onDelTags errorCode=" + errorCode
                + " sucessTags=" + sucessTags + " failTags=" + failTags
                + " requestId=" + requestId;
        Log.d(TAG, responseString);

        // Demo?????????????????????????????
        updateContent(context, responseString);
    }

    /**
     * listTags() ??????
     * 
     * @param context
     *            ???
     * @param errorCode
     *            ?????0????tag???????0?????
     * @param tags
     *            ??????????tag?
     * @param requestId
     *            ?????????????id
     */
    @Override
    public void onListTags(Context context, int errorCode, List<String> tags,
            String requestId) {
        String responseString = "onListTags errorCode=" + errorCode + " tags="
                + tags;
        Log.d(TAG, responseString);

        // Demo?????????????????????????????
        updateContent(context, responseString);
    }

    /**
     * PushManager.stopWork() ??????
     * 
     * @param context
     *            ???
     * @param errorCode
     *            ?????0?????????????????0?????
     * @param requestId
     *            ?????????????id
     */
    @Override
    public void onUnbind(Context context, int errorCode, String requestId) {
        String responseString = "onUnbind errorCode=" + errorCode
                + " requestId = " + requestId;
        Log.d(TAG, responseString);

        // ????????????flag?
        if (errorCode == 0) {
            Utils.setBind(context, false);
        }
        // Demo?????????????????????????????
        updateContent(context, responseString);
    }

    private void updateContent(Context context, String content) {
        Log.d(TAG, "updateContent");
        String logText = "" + Utils.logStringCache;

        if (!logText.equals("")) {
            logText += "\n";
        }

        SimpleDateFormat sDateFormat = new SimpleDateFormat("HH-mm-ss");
        logText += sDateFormat.format(new Date()) + ": ";
        logText += content;

        Utils.logStringCache = logText;

        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), PushDemoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.getApplicationContext().startActivity(intent);
    }

}




Java Source Code List

com.baidu.push.example.CustomActivity.java
com.baidu.push.example.DemoApplication.java
com.baidu.push.example.LoginActivity.java
com.baidu.push.example.MyPushMessageReceiver.java
com.baidu.push.example.PushDemoActivity.java
com.baidu.push.example.Utils.java
info.peoce.BuildConfig.java