该示例是使用百度云推送给iOS和安卓Push消息,SDK版本为:Baidu-Push-SDK-Java-1.1.2;
----------------------------------iOS---------------------------------
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
public class IosPushNotificationSample {
public static void main(String[] args) {
/*
* @brief 推送单播通知(IOS APNS) message_type = 1 (默认为0)
*/
// 1. 设置developer平台的ApiKey/SecretKey
// 需自己注册推送账号
String apiKey = "eFG2TqcUzaFeejQ5z4VrgcR";
String secretKey = "yDjYrK3vcslCFsp33o9A8gaABIxhzOHi";
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. 创建请求类对象
// 手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
request.setDeviceType(4); // device_type => 1: web 2: pc 3:android
// 4:ios 5:wp
request.setDeployStatus(2); // DeployStatus => 1: Developer 2:
// Production
request.setChannelId(562599783353482L);
request.setUserId("62990424331551961005");
request.setMessageType(1);
request.setMessage("{\"aps\":{\"alert\":\"555\"}}");
// 5. 调用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(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()));
}
}
}
-------------------------------Android----------------------------------
package com.baidu.yun.channel.sample;
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
public class AndroidPushNotificationSample {
public static void main(String[] args) {
/*
* @brief 推送单播通知(Android Push SDK拦截并解析) message_type = 1 (默认为0)
*/
// 1. 设置developer平台的ApiKey/SecretKey
// 需自己注册推送账号
String apiKey = "eFG2TqcUzaFeejQ5z4VrgcR";
String secretKey = "yDjYrK3vcslCFsp33o9A8gaABIxhzOHi";
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. 创建请求类对象
// 手机端的ChannelId, 手机端的UserId, 先用1111111111111代替,用户需替换为自己的
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
// 4:ios 5:wp
request.setChannelId(562599783353482L);
request.setUserId("62990424331551961005");
request.setMessageType(1);
request.setMessage("{\"title\":\"测试应用\",\"description\":\"555\"}");
// 5. 调用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(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()));
}
}
}
分享到:
相关推荐
Android推送(push)之百度云推送PHP版服务端代码 在用过极光推送之后呢,我又发现了其实百度云推送也还不错,所有把...在这里也分享哈百度云推送远程API的使用方法 大家也可以移步 http://www.jakehu.me/?p=48 下载
bpush-nodejs 是百度云推送 nodejs sdk,使用简单,部署方便,基于百度云推送 rest api 3.0开发。使用npm install bpush-nodejs --savevar bpush = require('bpush-nodejs'); ...说明SDK采用Promise方式进行回调,...
百度云推送android端例子与文档,轻松实现即时通讯
C#源代码共享 百度云推送到Android和IOS ,有IOS 和 安卓 具体推送例子 接口详细描述 http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/api/list#push_msg
1、百度云推送 打开AndroidManifest.xml,将百度开发者中心api_key复制value后的引号中。 <meta-data android:name="api_key" android:value="" /> 打开www下的index.html文件,把api_key替掉为开者中心api_key的值...
WePush消息批量推送工具是一款专注批量推送的小而美的工具。v4.2.5更新日志feature:定时任务开始执行时重新导入支持标签分组 fix:修复苹果M1芯片笔记本上无法运行的问题WePush消息批量推送工具支持消息类型模板...
WePush消息批量推送工具是一款专注批量推送的小而美的工具。 目前支持的消息类型: 模板消息-公众号 模板消息-小程序 微信客服消息 微信企业号/企业微信消息 阿里云短信 阿里大于模板短信 腾讯云短信 华为云短信 ...
cordova-baidupushcordova百度云推送插件
模板消息-公众号模板消息-小程序微信客服消息微信企业号/企业微信消息阿里云短信阿里大于模板短信腾讯云短信华为云短信百度云短信又拍云短信七牛云短信云片网短信E-MailHTTP请求功能:自定义消息并批量推送支持消息...
基于消息百度云推送开发的聊天工具,含源码下载
一直在仿微信界面,今天终于有幸利用百度云推送仿一仿微信聊天了。核心功能也就上面的两张图了~~~我拿着手机和模拟器聊天,同时感谢群里的兄弟姐妹帮忙测试(好友列表中)。下面通过几个问题来说明下实现的原理:...
*创建APP ID,同一个公司可能会使用类似于 com.example.* 这样通用的 App ID,但是如果要在 App 中加入消息推送功能,那么是不能使用通用 ID 的,需要为之单独创建一个。 首先登陆 iOS Dev Center ,然后进入 Member...
基于Baidu云推送技术实现推送服务的一个应用实例,很不错的,可以借鉴学习,如果需要技术讨论,可以联系我。
模板消息-公众号模板消息-小程序微信客服消息微信企业号/企业微信消息阿里云短信阿里大于模板短信腾讯云短信华为云短信百度云短信又拍云短信七牛云短信云片网短信E-MailHTTP请求功能:自定义消息并批量推送支持消息...
1. 支持Android推送,使用百度云推送。(参考代码:https://github.com/xiariqingquan/BaiduPushAspxServer) 2. 支持IOS(Apple/iPhone)推送,使用APNS-Sharp(参考代码:https://github.com/Redth/APNS-Sharp) ...
百度云推送的DEMO,用户在注册百度开发者中心后,在下载下来,进行云推送
为您提供百度云推聊天仿qq源码下载, 百度云推聊天仿qq源码是基于百度云推送的轻量级IM工具,用户与服务器间的通讯都是通过json封装。UI很精致,功能实现的也比较完整,在做IM的朋友可以研究一下。 ...
pushdemo百度云推送
百度云短信 又拍云短信 七牛云短信 云片网短信 E-Mail HTTP请求(单次、批量、压测) 计划中支持的消息类型 网易云信短信 榛子云短信 Luosimao短信 极光短信 极光推送 功能&亮点 支持自定义消息内容并批量推送 支持...