我是靠谱客的博主 疯狂棒棒糖,这篇文章主要介绍后台app个推,现在分享给大家,希望可以做个参考。

做项目会用到把消息推送给app

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
public class AppMessagePush { private static String appId = MyProperties.getByKey("appId");; private static String appKey = MyProperties.getByKey("appKey");; private static String masterSecret = MyProperties.getByKey("masterSecret");; public static void sendMessage(PushMsg msg) { if (msg.getType().equals("android")) { ITemplate notificationTemplate = notificationTemplateDemo( msg.getTitle(), msg.getMessageInfo(), msg.getBadge()); pushSingleMessage(msg.getCid(), notificationTemplate, false); ITemplate iosTransmissionTemplate = iosTransmissionTemplate( msg.getTitle(), msg.getMessageInfo(), msg.getBadge());// 带APNPayload pushSingleMessage(msg.getCid(), iosTransmissionTemplate, false); } else if (msg.getType().equals("ios")) { // ios透传通知 安卓透传 ITemplate iosTransmissionTemplate = iosTransmissionTemplate( msg.getTitle(), msg.getMessageInfo(), msg.getBadge());// 带APNPayload pushSingleMessage(msg.getCid(), iosTransmissionTemplate, true); } } // 单个推送 private static void pushSingleMessage(String Cid, ITemplate template, boolean offline) { IGtPush push = new IGtPush(appKey, masterSecret, true); SingleMessage message = new SingleMessage(); message.setOffline(offline); // 离线有效时间,单位为毫秒,可选 message.setOfflineExpireTime(24 * 3600 * 1000); message.setData(template); // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发 message.setPushNetWorkType(0); Target target = new Target(); target.setAppId(appId); target.setClientId(Cid); IPushResult ret = null; try { ret = push.pushMessageToSingle(message, target); } catch (RequestException e) { e.printStackTrace(); ret = push.pushMessageToSingle(message, target, e.getRequestId()); } } private static NotificationTemplate notificationTemplateDemo(String title, String messageInfo, String badge) { NotificationTemplate template = new NotificationTemplate(); // 设置APPID与APPKEY template.setAppId(appId); template.setAppkey(appKey); // 设置通知栏标题与内容 template.setTitle(title); template.setText(messageInfo); // 配置通知栏图标 template.setLogo("icon.png"); // 配置通知栏网络图标 template.setLogoUrl(""); // 设置通知是否响铃,震动,或者可清除 template.setIsRing(true); template.setIsVibrate(true); template.setIsClearable(true); template.setAPNInfo(getApnPayload(title, messageInfo, badge)); template.setTransmissionType(2); template.setTransmissionContent("notification." + messageInfo); return template; } // ios透传,设置APNPayload参数 private static TransmissionTemplate iosTransmissionTemplate(String title, String messageInfo, String badge) { TransmissionTemplate template = transmissionTemplate(messageInfo); APNPayload payload = getApnPayload(title, messageInfo, badge); // 字典模式使用下者 // payload.setAlertMsg(getDictionaryAlertMsg()); template.setAPNInfo(payload); return template; } private static APNPayload getApnPayload(String title, String messageInfo, String badge) { APNPayload payload = new APNPayload(); // +1在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字 payload.setAutoBadge(badge); payload.setContentAvailable(1); payload.setSound("123.wav"); payload.setCategory("$由客户端定义"); // //简单模式APNPayload.SimpleMsg // payload.setAlertMsg(new APNPayload.SimpleAlertMsg(messageInfo)); DictionaryAlertMsg dictionaryAlertMsg = new DictionaryAlertMsg(); dictionaryAlertMsg.setTitle(title); dictionaryAlertMsg.setBody(messageInfo); payload.setAlertMsg(dictionaryAlertMsg); return payload; } // 透传消息 private static TransmissionTemplate transmissionTemplate(String messageInfo) { TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(appId); template.setAppkey(appKey); template.setTransmissionContent(messageInfo); template.setTransmissionType(2); return template; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.park.util.push; public class PushMsg { private String cid; private String type; private String title; private String messageInfo; private String badge; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMessageInfo() { return messageInfo; } public void setMessageInfo(String messageInfo) { this.messageInfo = messageInfo; } public String getBadge() { return badge; } public void setBadge(String badge) { this.badge = badge; } public static PushMsg initial(String cid, String type, String title, String messageInfo,String badge) { PushMsg msg = new PushMsg(); msg.setCid(cid); msg.setType(type); msg.setTitle(title); msg.setMessageInfo(messageInfo); msg.setBadge(badge); return msg; } }

在项目上调用就好了

复制代码
1
2
3
PushMsg msg = PushMsg.initial(clientId, "ios", msgTitle, msgContent, badge + ""); AppMessagePush.sendMessage(msg);

最后

以上就是疯狂棒棒糖最近收集整理的关于后台app个推的全部内容,更多相关后台app个推内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部