zoukankan      html  css  js  c++  java
  • android开发(25)

    什么叫推送?

    中文名称:推送

    英文名称:push

    定义:描述因特网内容提供者和因特网用户之间工作方式的术语。“推送”指因特网内容提供者定期向预订用户“提供”数据。

    项目中有可能会用到推送。如果自己写一个的话,可是个耗时耗力的时,好在很多第三方公司都提供了推送服务,比如百度云。我们可以在自己的程序中使用它。

    百度云推送

    云推送(Push)是百度开放云向开发者提供的消息推送服务;通过利用云端与客户端之间建立稳定、可靠的长连接来为开发者提供向客户端应用推送实时消息服务。

    百度云推送服务支持推送三种类型的消息:通知、透传消息及富媒体;支持向所有用户或根据标签分类向特定用户群体推送消息;支持更多自定义功能(如自定义内容、后续行为、样式模板等);提供用户信息及通知消息统计信息,方便开发者进行后续开发及运营。

    百度hi官方技术讨论群:1405944 QQ群:242190646

    云推送服务具有以下特点:

    1. 增强用户粘性

    通过云和端之间建立的长连接,可以实时的推送消息到达用户端。保持与用户的沟通,大大提升用户活跃度和留存率。

    2. 节约成本

    在省电省流量方面远超行业水平,基础的消息推送服务永久免费,大大节省开发者推送的成本。

    3. 稳定安全的推送

    强大的分布式集群长期为百度各大产品线提供推送服务,保证消息推送服务的稳定、可靠。

    -------------------------------------------------------------

    好吧,让我们看看如何来使用它。

    百度云推送分两部分:web端和手机端。

    我们先看下手机端如何做。

    1.注册百度账户

    2.加入 百度开发者

    3.创建应用

    4.下载sdk

    5.导入sdk包,开发应用

    5.1 在AndroidManifest.xml 中注册响应的receiver

    5.2 在主窗体的oncreate中写

    PushManager.startWork(getApplicationContext(),
                    PushConstants.LOGIN_TYPE_API_KEY, 
                    PushServiceUtils.getMetaValue(this, "api_key"));

    5.3 编写自己的receiver。处理 绑定的相关消息,推送的消息,通知栏点击后的消息

    public class MyPushMessageReceiver extends BroadcastReceiver {
    
        private static final String TAG = "BroadcastReceiver";
    
        @Override
        public void onReceive(final Context context, Intent intent) {
    
            Log.d(TAG, ">>> Receive intent: 
    " + intent);
    
            if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
                // 获取消息内容
                String message = intent.getExtras().getString(
                        PushConstants.EXTRA_PUSH_MESSAGE_STRING);
    
                // 消息的用户自定义内容读取方式
                Log.i(TAG, "onMessage: " + message);
    
                // 自定义内容的json串
                Log.d(TAG,
                        "EXTRA_EXTRA = "
                                + intent.getStringExtra(PushConstants.EXTRA_EXTRA));
    
                // 用户在此自定义处理消息,以下代码为demo界面展示用
                Intent responseIntent = null;
                responseIntent = new Intent(PushServiceUtils.ACTION_MESSAGE);
                responseIntent.putExtra(PushServiceUtils.EXTRA_MESSAGE, message);
                responseIntent.setClass(context, MainActivity.class);
                responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(responseIntent);
    
            } else if (intent.getAction().equals(PushConstants.ACTION_RECEIVE)) {
                // 处理绑定等方法的返回数据
                // PushManager.startWork()的返回值通过PushConstants.METHOD_BIND得到
    
                // 获取方法
                final String method = intent
                        .getStringExtra(PushConstants.EXTRA_METHOD);
                // 方法返回错误码。若绑定返回错误(非0),则应用将不能正常接收消息。
                // 绑定失败的原因有多种,如网络原因,或access token过期。
                // 请不要在出错时进行简单的startWork调用,这有可能导致死循环。
                // 可以通过限制重试次数,或者在其他时机重新调用来解决。
                int errorCode = intent.getIntExtra(PushConstants.EXTRA_ERROR_CODE,
                        PushConstants.ERROR_SUCCESS);
                String content = "";
                if (intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT) != null) {
                    // 返回内容
                    content = new String(
                            intent.getByteArrayExtra(PushConstants.EXTRA_CONTENT));
                }
    
                // 用户在此自定义处理消息,以下代码为demo界面展示用
                Log.d(TAG, "onMessage: method : " + method);
                Log.d(TAG, "onMessage: result : " + errorCode);
                Log.d(TAG, "onMessage: content : " + content);
                Toast.makeText(
                        context,
                        "method : " + method + "
     result: " + errorCode
                                + "
     content = " + content, Toast.LENGTH_SHORT)
                        .show();
    
                Intent responseIntent = null;
                responseIntent = new Intent(PushServiceUtils.ACTION_RESPONSE);
                responseIntent.putExtra(PushServiceUtils.RESPONSE_METHOD, method);
                responseIntent.putExtra(PushServiceUtils.RESPONSE_ERRCODE,
                        errorCode);
                responseIntent.putExtra(PushServiceUtils.RESPONSE_CONTENT, content);
                responseIntent.setClass(context, MainActivity.class);
                responseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(responseIntent);
    
                // 可选。通知用户点击事件处理
            } else if (intent.getAction().equals(
                    PushConstants.ACTION_RECEIVER_NOTIFICATION_CLICK)) {
                Log.d(TAG, "intent=" + intent.toUri(0));
    
                // 自定义内容的json串
                String customData = intent
                        .getStringExtra(PushConstants.EXTRA_EXTRA);
    
                Log.d(TAG,
                        "EXTRA_EXTRA = "
                                + intent.getStringExtra(PushConstants.EXTRA_EXTRA));
    
                if (customData == null || "".equals(customData)) {
                    return;
                }
    
                Intent aIntent = new Intent();
                aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                aIntent.setClass(
                        context,
                        com.pdwy.wulianwang.mobile.main.notification.NotificationDetails_Activity.class);
                String title = intent
                        .getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE);
                aIntent.putExtra(PushConstants.EXTRA_NOTIFICATION_TITLE, title);
                String content = intent
                        .getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT);
                aIntent.putExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT, content);
    
                String detailContent = "";
                try {
                    org.json.JSONObject json = new JSONObject(customData);
                    detailContent = json.getString("detailContent");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // 保存在数据库
                NotifyDao dao = new NotifyDao();
                int notifyId = dao.saveNotify(title, content, detailContent);
                // 向消息详细页发送内容
                aIntent.putExtra("notify_id", notifyId);
    
                context.startActivity(aIntent);
    
            }
        }
    }

    web端需要做什么

    1.建立项目

    2.下载skd,引入包 bccs-api-lib-1.0.jar

    3.编写代码。

    /*
             * @brief 推送单播通知(Android Push SDK拦截并解析) message_type = 1 (默认为0)
             */
    
            // 1. 设置developer平台的ApiKey/SecretKey
            String apiKey = "xxxxxxxxxxxxxxxxxx";
            String secretKey = "xxxxxxxxxxxxx";
            ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);
    
            // 2. 创建BaiduChannelClient对象实例
            BaiduChannelClient channelClient = new BaiduChannelClient(pair);
    
            // 3. 若要了解交互细节,请注册YunLogHandler类
            channelClient.setChannelLogHandler(new YunLogHandler() {
                @Override
                public void onHandle(YunLogEvent event) {
                    System.out.println(event.getMessage());
                }
            });
    
            try {
    
                // 4. 创建请求类对象
                PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
                request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
                                            // 4:ios 5:wp
    
                // request.setMessage("Hello Channel");
                // 若要通知,
                request.setMessageType(1);
                request.setMessage("{"title":"Notify_title_danbo","description":"Notify_description_content"}");
                //request.setMessage(notify.toString());
    
                // 5. 调用pushMessage接口
                PushBroadcastMessageResponse response = channelClient
                        .pushBroadcastMessage(request);
    
                // 6. 认证推送成功
                System.out.println("push amount : " + response.getSuccessAmount());
    
            } catch (ChannelClientException e) {
                // 处理客户端错误异常
                e.printStackTrace();
            } catch (ChannelServerException e) {
                // 处理服务端错误异常
                System.out.println(String.format(
                        "request_id: %d, error_code: %d, error_message: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }

    上面的代码就能发送一条通知到手机。支持自定义消息标题,描述,其他自定义内容。

    --------------

    选用百度是个比较简单实现的方式。截止2013-9-12,我没有找到相关的收费信息。本着学习的精神可以研究研究,不过应该也可以再实际项目中使用。

     
  • 相关阅读:
    重回大一
    20071027我以为我很大度
    凌晨三点
    山洞爱情
    JQuery上传插件Uploadify使用详解
    jquery ui layout
    win2003下direct的问题
    Aptana一些快键用法
    IE、Firefox、Chrome 的JS代码兼容注意事项
    2011学习计划
  • 原文地址:https://www.cnblogs.com/vir56k/p/3317444.html
Copyright © 2011-2022 走看看