百度推送代码备份

  1. 首先查看百度推送开发者文档,这里主要是用Java实现。

http://push.biadu.com

  1. Java项目jar 引用,这里使用maven管理jar 包。

HomePage : https://github.com/featherfly/sorm.git

<dependency>
    <groupId>cn.featherfly</groupId>
    <artifactId>bccs-api</artifactId>
    <version>3.0.1</version>
</dependency>
  1. 推送工具类封装,这里直接使用的文档中的demo示例:
package com.sun4j.app.web.plugins;

import com.alibaba.fastjson.JSONObject;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.baidu.yun.push.model.PushMsgToSingleDeviceRequest;
import com.baidu.yun.push.model.PushMsgToSingleDeviceResponse;
import com.baidu.yun.push.model.PushMsgToTagRequest;
import com.baidu.yun.push.model.PushMsgToTagResponse;
import com.sun4j.app.util.ConfigProperty;

public class PushUtil {
    public static void push2Android(String title, String description, String pushKey)
            throws PushClientException, PushServerException {
        String apiKey = ConfigProperty.getKey("API_KEY_Android");
        String secretKey = ConfigProperty.getKey("SECRET_KEY_Android");
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
        BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });
        try {
            JSONObject notification = new JSONObject();
            notification.put("title", title);
            notification.put("description", description);
            notification.put("notification_builder_id", 0);
            notification.put("notification_basic_style", 4);
            notification.put("open_type", 1);
            JSONObject jsonCustormCont = new JSONObject();
            jsonCustormCont.put("key", "value");
            notification.put("custom_content", jsonCustormCont);

            PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest().addChannelId(pushKey)
                    .addMsgExpires(new Integer(3600)).addMessageType(1).addMessage(notification.toString())
                    .addDeviceType(3);
            PushMsgToSingleDeviceResponse response = pushClient.pushMsgToSingleDevice(request);
            System.out.println("msgId: " + response.getMsgId() + ",sendTime: " + response.getSendTime());
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format("requestId: %d, errorCode: %d, errorMessage: %s", e.getRequestId(),
                        e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }

    public static void push2TagIOS(String title, String description,String tag)
            throws PushClientException, PushServerException {
        String apiKey = ConfigProperty.getKey("API_KEY_IOS");
        String secretKey = ConfigProperty.getKey("SECRET_KEY_IOS");
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
        BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            JSONObject notification = new JSONObject();
            JSONObject jsonAPS = new JSONObject();
            jsonAPS.put("alert", title);
            jsonAPS.put("sound", "ttt"); // 设置通知铃声样式,例如"ttt",用户自定义。
            notification.put("aps", jsonAPS);
            notification.put("description", description);
            PushMsgToTagRequest request = new PushMsgToTagRequest()
                    .addTagName(tag) //设置tag组
                    .addMsgExpires(new Integer(3600))
                    .addMessageType(1) // 1:通知,0:透传消息.默认为0  注:IOS只有通知.
                    .addMessage(notification.toString())
                    .addDeployStatus(1)// IOS,DeployStatus => 1: Developer, 2: Production.
                    .addDeviceType(4); // deviceType => 3:android, 4:ios
            PushMsgToTagResponse response = pushClient.pushMsgToTag(request);
            System.out.println("msgId: " + response.getMsgId() + ",sendTime: "
                    + response.getSendTime() + ",timerId: "
                    + response.getTimerId());
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format("requestId: %d, errorCode: %d, errorMessage: %s", e.getRequestId(),
                        e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }

    /**
     * 推送消息到标签组
     *
     * @param title
     * @param description
     * @param pushKey
     * @param tag
     * @throws PushClientException
     * @throws PushServerException
     */
    public static void push2TagAndroid(String message, String tag) throws PushClientException, PushServerException {
        String apiKey = ConfigProperty.getKey("API_KEY_Android");
        String secretKey = ConfigProperty.getKey("SECRET_KEY_Android");
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
        BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            PushMsgToTagRequest request = new PushMsgToTagRequest()
                    .addTagName("member")
                    .addMsgExpires(new Integer(3600))
                    .addMessageType(1)
                    .addMessage(message)
                    .addDeviceType(3);
            PushMsgToTagResponse response = pushClient.pushMsgToTag(request);
            System.out.println("msgId: " + response.getMsgId() + ",sendTime: " + response.getSendTime() + ",timerId: "
                    + response.getTimerId());
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format("requestId: %d, errorCode: %d, errorMessage: %s", e.getRequestId(),
                        e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }

    public static void push2IOS(String title, String description,String pushKey) throws PushClientException, PushServerException {
        String apiKey = ConfigProperty.getKey("API_KEY_IOS");
        String secretKey = ConfigProperty.getKey("SECRET_KEY_IOS");
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
        BaiduPushClient pushClient = new BaiduPushClient(pair, BaiduPushConstants.CHANNEL_REST_URL);
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            }
        });

        try {
            JSONObject notification = new JSONObject();
            JSONObject jsonAPS = new JSONObject();
            jsonAPS.put("alert", "我是单推");
            jsonAPS.put("sound", "ttt"); // 设置通知铃声样式,例如"ttt",用户自定义。
            notification.put("aps", jsonAPS);
            notification.put("description", description);

            PushMsgToSingleDeviceRequest request = new PushMsgToSingleDeviceRequest()
                    .addChannelId(pushKey)
                    .addMsgExpires(new Integer(3600)). // 设置message的有效时间
                    addMessageType(1).// 1:通知,0:透传消息.默认为0 注:IOS只有通知.
                    addMessage(notification.toString()).addDeployStatus(2). // IOS,
                                                                            // DeployStatus
                                                                            // => 1: Developer
                                                                            // 2: Production.
                    addDeviceType(4);// deviceType => 3:android, 4:ios
            PushMsgToSingleDeviceResponse response = pushClient
                    .pushMsgToSingleDevice(request);
            System.out.println("msgId: " + response.getMsgId() + ",sendTime: "
                    + response.getSendTime());
        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                throw e;
            } else {
                System.out.println(String.format("requestId: %d, errorCode: %d, errorMessage: %s", e.getRequestId(),
                        e.getErrorCode(), e.getErrorMsg()));
            }
        }
    }
}
  1. 配置文件内容
#IM Push
API_KEY_Android=cPdUbtfnsplRsYBwR43oPv6Cg
SECRET_KEY_Android=2sqRWCM5H6D81wKbrldjwG0bRCn

API_KEY_IOS=Hwdftdos5wwgIzsdODjA4OcrHI
SECRET_KEY_IOS=8d1503fda5de183ecc35a576c0d311
  1. 获取配置文件工具类
package com.sun4j.app.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ConfigProperty {
    private static final String URL = "config.properties";
    private static final Properties properties = new Properties();
    static {
        loadProperties();
    }

    public static void loadProperties() {
        InputStream is = null;
        Resource rs = new ClassPathResource(URL);
        try {
            is = rs.getInputStream();
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static String getKey(String key) {
        return properties.getProperty(key);
    }
}
  1. 测试结果
public static void main(String[] args) throws PushClientException, PushServerException {
        //Android
        JSONObject jsonMsg = new JSONObject();
        jsonMsg.put("title", "hello ");
        jsonMsg.put("description", "你好");
        //push2TagAndroid(jsonMsg.toJSONString(), "user");
        //单个推送
        //push2Android("项目推送","呵呵哒", "3465930645394868000");

        //IOS
        JSONObject iosMsg = new JSONObject();
        JSONObject aps = new JSONObject();
        aps.put("alert", "百度推送IOS内容");
        aps.put("sound", "ttt");
        aps.put("badge", 1);
        iosMsg.put("description", "IOSTag你好");
        iosMsg.put("aps", aps);
        System.out.println(iosMsg.toJSONString());

        //单个推送
        push2IOS("凡你好", "娃儿", "5734180441672125644");

        //push2TagIOS("你好Tag", "我是备注","user");
    }
时间: 2024-08-01 21:40:46

百度推送代码备份的相关文章

C# 简单的百度推送代码

前段时间搞推送来着,安卓方面用到了百度的推送服务,由于只是简单的用到安卓推送的通知功能,所以没用百度推荐的C# SDK,通过借鉴网上的各种资料和百度的API,费了老大劲终于折腾出来一段能用的代码(早知道这么纠结,直接用别人的了...强迫症伤不起啊) /// <summary> /// 百度推送 /// </summary> public class BaiduPush { private const string method = "push_msg"; pri

百度推送代码

<script>(function(){    var bp = document.createElement('script');    var curProtocol = window.location.protocol.split(':')[0];    if (curProtocol === 'https') {        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';            }    else {   

百度自动推送代码的作用

在百度的搜索资源平台下的站点支持模块中有一个连接提交,下有一段代码叫做自动推送代码,只要在这个平台下认证了你的网站就可以,将自动推送代码加进去.我在https://www.jianzhumuju.com方圆扣这个站点上做了一下尝试,看看这段代码到底有什么作用,能够起到多大的效果,下面是这段代码的截图.在加入代码的时候,百度收录这个站点页面的索引量是1123,今天我们这段自动推送的代码加上,看看在半个月之后的效果.是不是收录增加,自动推送是不是能够让我们的网址真的能够及时推送给百度,百度的收录速度

百度推送证书在终端如何合成?一行代码将.p12证书合成.pem证书

在开发者中心生成推送证书,格式为:证书.cer 在mac电脑的钥匙串导出证书,格式为:证书.p12 打开终端,打开证书.p12所在目录,然后输入: openssl pkcs12 -in 证书.p12 -out 证书.pem -nodes 按下回车键(如果设置密码,请输入密码,继续按一下回车键)---->生成格式为.pem的推送证书 百度推送的开发证书和生产证书合成方法相同

iOS开发三步搞定百度推送

iOS开发三步搞定百度推送 百度推送很简单,准备工作:在百度云推送平台注册应用,上传证书. 步骤一: 百度云推送平台 http://push.baidu.com/sdk/push_client_sdk_for_ios  在这里下载iOS端SDK包,如下图: 把SDK包里面的下图文件夹拖到你的工程中,如下图,第一步就这么简单. 步骤二: 在工程中AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaun

iOS百度推送的基本使用

一.iOS证书指导 在 iOS App 中加入消息推送功能时,必须要在 Apple 的开发者中心网站上申请推送证书,每一个 App 需要申请两个证书,一个在开发测试环境下使用,另一个用于上线到 AppStore 后的生产环境. 7.1 为你的 App 创建 App ID iOS 中每个 App 都需要对应一个 App ID,同一个公司可能会使用类似于 com.example.* 这样通用的 App ID,但是如果要在 App 中加入消息推送功能,那么是不能使用通用 ID 的,需要为之单独创建一个

php 实时推送代码

网站质量不错的网站可以在百度站长平台/数据提交/sitemap栏目下看到实时推送的功能, 目前这个工具是邀请开放, 百度的实时推送的api接口可以实时推送我们新发布的文章, 保证百度在第一时间收录. 百度站长平台 http://zhanzhang.baidu.com/ 打开百度站长平台, 点开实时推送的添加新数据接口获得带token的api推送地址: http://ping.baidu.com/sitemap?site=www.yourdomain.com&resource_name=sitem

整合百度推送碰到的问题

今天在整合百度推送到我的.NET项目中的时候发现,百度官网上下的.NET DEMO是4.5版本的,但是WIN2003安装不了.NET 4.5的, 然后又从网上搜索到4.0版本的整合进项目中了,结果在测试的时候总是出错,但是用.NET 4.5版本的DEMO测试就不会出错, 仔细看了一下,发现是参数问题...NET 4.5里的参数是msg和msg_type,而.NET 4.0里的参数是message和message_type, 我全改为msg和msg_type就好了...然后我上去官网看文档,发现竟

iOS - 百度推送

在很多项目开发中,大家都做过推送!例如:极光推送.信鸽推送.个推等等一系列的推送.我们使用的都是集成过后的SDK,其原理都是相同的. 这里我们来做一下百度推送. 1.百度推送 1.1 百度账号的注册申请和审核 登录百度云推送:http://push.baidu.com/fc.填写相关资料.审核. 审核通过后下载SDK,创建应用(上传相应的推送证书格式看要求到出),默认创建的应用是,开发者测试的,上线后要改成生产的. 1.2 SDK的导入与配置 1)所使用的SDK很简单.(里面有个测试音频text