我是靠谱客的博主 机灵茉莉,这篇文章主要介绍极光推送:后台向APP:android,ios极光推送消息,现在分享给大家,希望可以做个参考。

1. 添加依赖

复制代码
1
2
3
//jpush compile group: 'cn.jpush.api', name: 'jpush-client', version: '3.3.4'

2. 注册极光用户

复制代码
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
//APP端注册极光Id,jpushId为每个用户在各个设备的唯一标识 public void registeAuroral(String jpushId) { JPushInterface.setAlias(LaunchActivity.this, jpushId, new TagAliasCallback() { @Override public void gotResult(final int code, String s, Set<String> set) { LKLogUtil.e("极光别名设置==失败errorCode = " + code); switch (code) { case 0: LKLogUtil.e("极光别名设置===成功"); break; case 6002: LKLogUtil.e("极光别名设置===超时"); mHandler.postDelayed(new Runnable() { @Override public void run() { JPushInterface.resumePush(LaunchActivity.this); setAlias(jpushId); } }, 1000 * 60); break; case 6012: LKLogUtil.e("极光别名设置==失败errorCode = " + code); mHandler.postDelayed(new Runnable() { @Override public void run() { JPushInterface.resumePush(LaunchActivity.this); setAlias(jpushId); LKLogUtil.e("极光别名设置==失败errorCode = " + code); } }, 1000 * 60); break; default: LKLogUtil.e("极光别名设置==失败errorCode = " + code); break; } } }); }

3. 后台代码推送极光
//******************************************************JPushUtil.class

复制代码
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import cn.jpush.api.push.model.Message; import cn.jpush.api.push.model.Options; import cn.jpush.api.push.model.Platform; import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.audience.AudienceTarget; import cn.jpush.api.push.model.notification.AndroidNotification; import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification; import com.alibaba.fastjson.JSON; import net.sf.jsqlparser.statement.alter.Alter; import java.util.Map; /** * @author syg * Created by lenovo on 2017/02/24. */ public class JPushUtil { /** * 发送给所有用户 * @param notificationTitle * @param1 avatar * @param1 nick * @param1 imTag jumpType * @return */ public static PushPayload buildPushObject_android_and_ios(String notificationTitle, String title, Map<String,String> map, String[]alias, boolean isApnsProduction) { return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.alias(alias)) .setNotification(Notification.newBuilder() .setAlert(notificationTitle) .addPlatformNotification(AndroidNotification.newBuilder() .setAlert(notificationTitle) //.setTitle(notification_title) //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) //.setTitle(title) .addExtras(map) .build() ) .addPlatformNotification(IosNotification.newBuilder() //传一个IosAlert对象,指定apns title、title、subtitle等 .setAlert(notificationTitle) //直接传alert //此项是指定此推送的badge自动加1 //.incrBadge(1) .disableBadge() //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒, // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音 .setSound("default") //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) // .addExtra("iosNotification extras key",extrasparam) .addExtras(map) //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification // .setContentAvailable(true) .build() ) .build() ) .setOptions(Options.newBuilder() //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 .setApnsProduction(isApnsProduction) //此字段是给开发者自己给推送编号,方便推送者分辨推送记录 .setSendno(1) //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒 .setTimeToLive(600) .build() ) .build(); } /** * 透传消息 * @param msgContent 透传消息内容 * @param map 透传消息附加参数 * @param alias 透传消息发送人 * @return */ public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String msgContent, Map<String,String> map, String[] alias) { return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.alias(alias)) .setMessage(Message.newBuilder() .setMsgContent(msgContent) .addExtras(map) .build()) .build(); } /** * 发送给所有指定设备 * @param notificationTitle * @param * @param * @param * @return */ public static PushPayload buildPushObjectAllTarget(String notificationTitle, Map<String,String> map, String title, boolean isApnsProduction) { return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .setAlert(notificationTitle) .addPlatformNotification(AndroidNotification.newBuilder() .setAlert(notificationTitle) //.setTitle(notification_title) //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) .setTitle(title) .addExtras(map) .build() ) .addPlatformNotification(IosNotification.newBuilder() //传一个IosAlert对象,指定apns title、title、subtitle等 .setAlert(notificationTitle) //直接传alert //此项是指定此推送的badge自动加1 //.incrBadge(1) //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒, // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音 .setSound("default") //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value) // .addExtra("iosNotification extras key",extrasparam) .addExtras(map) //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification // .setContentAvailable(true) .build() ) .build() ) //自定义消息 .setMessage(Message.newBuilder() .setMsgContent(notificationTitle) .setTitle(title) .addExtras(map) .build()) .setOptions(Options.newBuilder() //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 .setApnsProduction(isApnsProduction) //此字段是给开发者自己给推送编号,方便推送者分辨推送记录 .setSendno(1) //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒 .setTimeToLive(600) .build() ) .build(); } /** * 根据别名推送至android * @param notificationTitle * @param alias * @return */ public static PushPayload buildPushObjectAndroidAllAlertWithTitle(String notificationTitle, String title, String[] alias, Map<String,String> map, boolean isApnsProduction) { return PushPayload.newBuilder() //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台 .setPlatform(Platform.android()) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id .setAudience(Audience.alias(alias)) //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发 .setNotification(Notification.newBuilder() //指定当前推送的android通知 .addPlatformNotification(AndroidNotification.newBuilder() .setAlert(notificationTitle) .setTitle(title)//此字段为app名称 .addExtras(map) .build()) .build() ) .setOptions(Options.newBuilder() //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 .setApnsProduction(isApnsProduction) //此字段是给开发者自己给推送编号,方便推送者分辨推送记录 .setSendno(1) //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒 .setTimeToLive(600) .build()) .build(); } /** * 根据别名推送至ios * @param notificationTitle * @param alias * @return */ public static PushPayload buildPushObjectIosAllAlertWithTitle(String notificationTitle, String[] alias, Map<String,String> map, boolean isApnsProduction) { return PushPayload.newBuilder() //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台 .setPlatform(Platform.ios()) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id .setAudience(Audience.alias(alias)) //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发 .setNotification(Notification.newBuilder() //指定当前推送的android通知 .addPlatformNotification(IosNotification.newBuilder() //传一个IosAlert对象,指定apns title、title、subtitle等 .setAlert(notificationTitle) .addExtras(map) //直接传alert //此项是指定此推送的badge自动加1 //.incrBadge(1) //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒, // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音 .setSound("default") //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification // .setContentAvailable(true) .build()) .build() ) .setOptions(Options.newBuilder() //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 .setApnsProduction(isApnsProduction) //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒 .setTimeToLive(600) .build()) .build(); } /** * 根据别名推送至指定用户 * @param registrationId * @param notificationTitle * @param * @param * @param * @return */ public static PushPayload buildPushObjectAllRegistrationIdAlertWithTitle(String registrationId, String notificationTitle, String title, Map<String,String> map, boolean isApnsProduction) { //创建一个IosAlert对象,可指定APNs的alert、title等字段 return PushPayload.newBuilder() //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台 .setPlatform(Platform.all()) //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id .setAudience(Audience.alias(registrationId)) //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发 .setNotification(Notification.newBuilder() //指定当前推送的android通知 .addPlatformNotification(AndroidNotification.newBuilder() .setAlert(notificationTitle) .setTitle(title) .addExtras(map) .build()) //指定当前推送的iOS通知 .addPlatformNotification(IosNotification.newBuilder() //传一个IosAlert对象,指定apns title、title、subtitle等 .setAlert(notificationTitle) .addExtras(map) //.incrBadge(1) .setSound("default") .build()) .build()) .setOptions(Options.newBuilder() //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义 .setApnsProduction(isApnsProduction) //此字段是给开发者自己给推送编号,方便推送者分辨推送记录 .setSendno(1) //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天; .setTimeToLive(600) .build()) .build(); } }

//********************************************* AuroraPusher.class

复制代码
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
@Component public class AuroraPusher { public final static Logger logger = LogManager.getLogger(AuroraPusher.class); @Value("${jpush.masterSecret}") private String masterSecret; @Value("${jpush.appKey}") private String appKey; @Value("${jpush.isApnsProduction}") private boolean isApnsProduction; /** 第三方规定推送接收对象数量 */ private final int aliaSize = 38; /** 推送客户端 */ private JPushClient jPushClient; /** 获取推送客户端 */ public JPushClient getjPushClient() { if(jPushClient == null){ jPushClient = new JPushClient (masterSecret, appKey); } return jPushClient; } /** * 发送推送 * @param payload 推送内容 * @return */ private boolean push(PushPayload payload){ try { PushResult pushResult = this.getjPushClient().sendPush(payload) ; logger.debug("调用推送方法响应结果为:" + pushResult != null ? pushResult.toString() : "推送响应内容为空"); final int code = 200; //存在有未登录用户错误码 final int error_code = 1011; if(pushResult == null || code != pushResult.getResponseCode()){ if(error_code == pushResult.getResponseCode()){ throw new SendMessageFailureException(error_code, "选择用户中包含存未在伯通APP登录激活的用户"); } throw new SendMessageFailureException("推送消息时,第三方响应错误,错误原因为:" + pushResult.error.getMessage()); } } catch (APIConnectionException e) { logger.error("推送消息时,连接第三方异常, 异常原因:" + e.getMessage(), e); throw new SendMessageFailureException("推送消息时,连接第三方异常"); } catch (APIRequestException e) { logger.error("推送消息时,请求第三方异常, 异常原因:" + e.getMessage(), e); throw new SendMessageFailureException("推送消息时,请求第三方异常"); } return true; } /** * 推送至所有设备 * @param notificationTitle * @param title * @param map * @return */ public boolean pushAllTarget(String notificationTitle, String title, Map<String, String> map) { PushPayload payload = JPushUtil.buildPushObjectAllTarget(notificationTitle, map, title, isApnsProduction); return this.push(payload); } /** * 推送至所有设备 * @param notificationTitle * @param map * @param title * @param alias * @return */ public boolean pushAllTargetByUser(String notificationTitle, String title, Map<String, String> map, String[] alias) { PushPayload payload; List<List<String>> aliaList = splitArraySpecifySizeList(alias, aliaSize); for(List<String> itemList : aliaList){ payload = JPushUtil.buildPushObject_android_and_ios(notificationTitle, title, map, itemList.toArray(new String[]{}), isApnsProduction); this.push(payload); } return true; } /** * 透传消息 * @param msgContent 透传消息内容 * @param map 附加信息 * @param alias 接收人 * @return */ public boolean buildPushObjectAudienceMoreMessageWithExtras(String msgContent, Map<String,String> map, String[] alias){ PushPayload payload; List<List<String>> aliaList = splitArraySpecifySizeList(alias, aliaSize); for(List<String> itemList : aliaList){ payload = JPushUtil.buildPushObject_ios_audienceMore_messageWithExtras(msgContent, map, itemList.toArray(new String[]{})); this.push(payload); } return true; } /** * 根据ilias推送至android * @param msg * @param title * @param alias * @param map * @return */ public boolean sendAllAndroid(String msg, String title, String[] alias, Map<String, String> map) { PushPayload payload; List<List<String>> aliaList = splitArraySpecifySizeList(alias, aliaSize); for(List<String> itemList : aliaList){ payload = JPushUtil.buildPushObjectAndroidAllAlertWithTitle(msg, title, itemList.toArray(new String[]{}), map, isApnsProduction);; this.push(payload); } return true; } /** * 根据ilias推送至ios * @param msg * @param alias * @param map * @return */ public boolean sendAllIos(String msg, String[] alias, Map<String, String> map) { PushPayload payload; List<List<String>> aliaList = splitArraySpecifySizeList(alias, aliaSize); for(List<String> itemList : aliaList){ payload = JPushUtil.buildPushObjectIosAllAlertWithTitle(msg, itemList.toArray(new String[]{}), map, isApnsProduction); this.push(payload); } return true; } /** * 根据alias推送至指定设备 * @param registrationId * @param notificationTitle * @param title * @param map * @return */ public boolean sendByAlias(String registrationId, String notificationTitle, String title, Map<String, String> map) { PushPayload payload; String[] alias = registrationId.split(","); if(alias.length < aliaSize){ payload = JPushUtil.buildPushObjectAllRegistrationIdAlertWithTitle(registrationId, title, notificationTitle, map, isApnsProduction); this.push(payload); } else { List<List<String>> list = splitArraySpecifySizeList(alias, aliaSize); for(List<String> temp : list){ payload = JPushUtil.buildPushObjectAllRegistrationIdAlertWithTitle(String.join(",", temp), title, notificationTitle, map, isApnsProduction); this.push(payload); } } return true; } /** * 拆分接收对象为指定数量的数组对象集合 * @param alias 推送接收对象 * @param size 指定数量 * @return */ private List<List<String>> splitArraySpecifySizeList(String[] alias, final int size){ if(ArrayUtils.isEmpty(alias)){ return null; } return ListUtils.getAllowList(Arrays.asList(alias), size); } }

最后

以上就是机灵茉莉最近收集整理的关于极光推送:后台向APP:android,ios极光推送消息的全部内容,更多相关极光推送:后台向APP:android内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部