–1.进入开发者中心
–2.创建应用
–3.记录应用配置
public class PushService extends Service{
@Override
public void onCreate() {
super.onCreate();
GTServiceManager.getInstance().onCreate(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return GTServiceManager.getInstance().onStartCommand(this, intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return GTServiceManager.getInstance().onBind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
GTServiceManager.getInstance().onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
GTServiceManager.getInstance().onLowMemory();
}
}
/**
* 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息
* onReceiveMessageData 处理透传消息
* onReceiveClientId 接收 cid
* onReceiveOnlineState cid 离线上线通知
* onReceiveCommandResult 各种事件处理回执
*/
public class DemoIntentService extends GTIntentService{
@Override
public void onReceiveServicePid(Context context, int i) {
}
@Override
public void onReceiveClientId(Context context, String clientid) {
Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
}
@Override
public void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage) {
}
@Override
public void onReceiveOnlineState(Context context, boolean b) {
}
@Override
public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {
}
@Override
public void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage) {
}
@Override
public void onNotificationMessageClicked(Context context, GTNotificationMessage gtNotificationMessage) {
}
}
;
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<service
android:name=".service.PushService"
android:exported="true"
android:label="PushService"
android:process=":pushservice">
service>
<service android:name=".service.DemoIntentService"/>
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
// com.getui.demo.DemoPushService 为第三方自定义推送服务
PushManager.getInstance().initialize(this.getApplicationContext(), PushService.class);
// com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
}
}
@Controller
@RequestMapping("/test")
public class TestController {
//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
private static String appId = "";
private static String appKey = "";
private static String masterSecret = "";
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
static String host = "http://sdk.open.api.igexin.com/apiex.htm";
static String CID = "";
//别名推送方式
static String Alias = "";
// @RequestMapping("/hello")
// public String hello(Model model){
// model.addAttribute("msg","hello....");
// return "success";
// }
//推送通知
@RequestMapping("/pushNotice")
public void text() {
IGtPush push = new IGtPush(url, appKey, masterSecret);
// 定义"点击链接打开通知模板",并设置标题、内容、链接
LinkTemplate template = new LinkTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTitle("请填写通知标题");
template.setText("请填写通知内容");
template.setUrl("sh://12");
List appIds = new ArrayList();
appIds.add(appId);
// 定义"AppMessage"类型消息对象,设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
AppMessage message = new AppMessage();
message.setData(template);
message.setAppIdList(appIds);
message.setOffline(true);
message.setOfflineExpireTime(1000 * 600);
IPushResult ret = push.pushMessageToApp(message);
System.out.println(ret.getResponse().toString());
}
@RequestMapping("/pushSignalMessage")
public void pushSignal() {
//
IGtPush push = new IGtPush(host, appKey, masterSecret);
LinkTemplate template = linkTemplateDemo();
SingleMessage message = new SingleMessage();
message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 3600 * 1000);
message.setData(template);
// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
message.setPushNetWorkType(0);
Target target = new Target();
target.setAppId(appId);
target.setClientId(CID);
//target.setAlias(Alias);
IPushResult ret = null;
try {
ret = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
ret = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (ret != null) {
System.out.println(ret.getResponse().toString());
} else {
System.out.println("服务器响应异常");
}
}
public static LinkTemplate linkTemplateDemo() {
LinkTemplate template = new LinkTemplate();
// 设置APPID与APPKEY
template.setAppId(appId);
template.setAppkey(appKey);
Style0 style = new Style0();
// 设置通知栏标题与内容
style.setTitle("请输入通知栏标题");
style.setText("请输入通知栏内容");
// 配置通知栏图标
style.setLogo("icon.png");
// 配置通知栏网络图标
style.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
template.setStyle(style);
// 设置打开的网址地址
//template.setUrl("http://www.baidu.com");
return template;
}
@RequestMapping("/pushMessage")
public void pushMessage() throws Exception{
IIGtPush push = new IGtPush(host, appKey, masterSecret);
IBatch batch = push.getBatch();
try {
//构建客户a的透传消息a
constructClientTransMsg(CID,"msgA",batch);
} catch (Exception e) {
e.printStackTrace();
}
batch.submit();
}
private static void constructClientTransMsg(String cid, String msg ,IBatch batch) throws Exception {
SingleMessage message = new SingleMessage();
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(msg);
template.setTransmissionType(0); // 这个Type为int型,填写1则自动启动app
message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(1 * 1000);
// 设置推送目标,填入appid和clientId
Target target = new Target();
target.setAppId(appId);
target.setClientId(cid);
batch.add(message, target);
}
// private static void constructClientLinkMsg(String cid, String msg ,IBatch batch) throws Exception {
//
// SingleMessage message = new SingleMessage();
// LinkTemplate template = new LinkTemplate();
// template.setAppId(appId);
// template.setAppkey(appKey);
// template.setTitle("title");
// template.setText("msg");
// template.setLogo("push.png");
// template.setLogoUrl("logoUrl");
// template.setUrl("url");
//
// message.setData(template);
// message.setOffline(true);
// message.setOfflineExpireTime(1 * 1000);
//
// // 设置推送目标,填入appid和clientId
// Target target = new Target();
// target.setAppId(appId);
// target.setClientId(cid);
// batch.add(message, target);
// }
发送流程:
包装Message(content,senderId,receiverId)->通过网络请求传给服务器->服务器接受根据receiverId,透传给另一个客户端(调用pushMessage接口)
相关参数需要填写自己申请所获得的key
服务器端更改数据库账号密码,app端修改Network中的URL