zoukankan      html  css  js  c++  java
  • 华为PUSH SDK 接入方法

    本文参考了华为推送平台官网及其Demo:http://developer.huawei.com/cn/consumer/wiki/index.php?title=接入说明

    第一步

    下载sdk,导入libs文件夹下,右键add as library。

    第二步

    将res目录中的values、layout、drawable等所有文件夹拷贝到自己的工程中。

    第三步

    将AndroidManifest.xml文件中的所有activity,receiver,service,meta-data拷 贝至自己的AndroidManifest.xml文件中。

    第四步:

    实现com. huawei.pushtest.receiver.MyReceiver,参看官方demo。

    public class MyReceiver extends PushEventReceiver {

    /*
     * 显示Push消息
     */
    public void showPushMessage(int type, String msg) {
        HuaWeiTestActivity mPustTestActivity = AppApplication.instance().getMainActivity();
        if (mPustTestActivity != null) {
            Handler handler = mPustTestActivity.getHandler();
            if (handler != null) {
                Message message = handler.obtainMessage();
                message.what = type;
                message.obj = msg;
                handler.sendMessageDelayed(message, 1L);
            }
        }
    }
    
    @Override
    public void onToken(Context context, String token, Bundle extras){
        String belongId = extras.getString("belongId");
        String content = "获取token和belongId成功,token = " + token + ",belongId = " + belongId;
        Log.d(HuaWeiTestActivity.TAG, content);
        showPushMessage(HuaWeiTestActivity.RECEIVE_TOKEN_MSG, content);
    }
    
    
    @Override
    public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
        try {
            String content = "收到一条Push消息: " + new String(msg, "UTF-8");
            Log.d(HuaWeiTestActivity.TAG, content);
            showPushMessage(HuaWeiTestActivity.RECEIVE_PUSH_MSG, content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public void onEvent(Context context, Event event, Bundle extras) {
        if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
            int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
            if (0 != notifyId) {
                NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                manager.cancel(notifyId);
            }
            String content = "收到通知附加消息: " + extras.getString(BOUND_KEY.pushMsgKey);
            Log.d(HuaWeiTestActivity.TAG, content);
            showPushMessage(HuaWeiTestActivity.RECEIVE_NOTIFY_CLICK_MSG, content);
        } else if (Event.PLUGINRSP.equals(event)) {
            final int TYPE_LBS = 1;
            final int TYPE_TAG = 2;
            int reportType = extras.getInt(BOUND_KEY.PLUGINREPORTTYPE, -1);
            boolean isSuccess = extras.getBoolean(BOUND_KEY.PLUGINREPORTRESULT, false);
            String message = "";
            if (TYPE_LBS == reportType) {
                message = "LBS report result :";
            } else if(TYPE_TAG == reportType) {
                message = "TAG report result :";
            }
            Log.d(HuaWeiTestActivity.TAG, message + isSuccess);
            showPushMessage(HuaWeiTestActivity.RECEIVE_TAG_LBS_MSG, message + isSuccess);
        }
        super.onEvent(context, event, extras);
    }
    

    }

    第五步

    在MyActivity的OnCreate()方法中添加 PushManager.requestToken(MyActivity.this);

    第六步 测试

    登录华为push后台,新建推送消息,如图。

  • 相关阅读:
    ADO.NET FOR MySQL帮助类
    遍历文件夹及其子文件夹下的.pdf文件,并解压文件夹下所有的压缩包
    history.back();谷歌浏览器,iframe后退问题
    Request.url请求属性
    正则表达式 取两字符中间的字符串(双向非贪婪模式)
    MVC 项目中为什么会有两个web.config
    c#动态编译并动态生成dll
    siteserver cms选择栏目搜索无效
    jquery修改Switchery复选框的状态
    ntko office在线编辑控件问题记录
  • 原文地址:https://www.cnblogs.com/shuaiwang/p/5533974.html
Copyright © 2011-2022 走看看