我是靠谱客的博主 善良豌豆,这篇文章主要介绍jpush推送通知ios、android,现在分享给大家,希望可以做个参考。

 

引入jpush sdk

复制代码
1
2
3
4
5
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.3.13</version> </dependency>
复制代码
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
public class JpushClientUtils { private final static String masterSecret = "xxxxx"; private final static String appKey = "xxxxx"; private final static Integer SUCCESS = 200; private static JPushClient jPushClient = new JPushClient(masterSecret,appKey); //定时发送[-android public static String sendScheduleAndroid(Map<String, String> parm){ PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .addPlatformNotification(AndroidNotification.newBuilder() .addExtras(parm) .setTitle(parm.get("title")) //标题 .setAlert(parm.get("content")) //内容 .setAlertType(1) //默认提示音 .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(true).build())//false-开发环境,true-生产环境,android不区分环境 .build(); try { try { ScheduleResult singleSchedule = jPushClient.createSingleSchedule("schedule",parm.get("date"), payload);//时间 System.out.println(singleSchedule.isResultOK()); if( singleSchedule.getResponseCode() == SUCCESS){ return singleSchedule.getSchedule_id(); } } catch (APIConnectionException e) { e.printStackTrace(); } } catch (APIRequestException e) { e.printStackTrace(); } return null; } public static String sendScheduleIOS(Map<String, String> parm){ PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.ios()) .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(IosAlert.newBuilder() .setTitleAndBody(parm.get("title"),"",parm.get("content")).build()) .setBadge(+1) .setSound("happy") .addExtras(parm) .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(true).build()) .build(); try { try { ScheduleResult singleSchedule = jPushClient.createSingleSchedule("schedule",parm.get("date"), payload); System.out.println(singleSchedule.isResultOK()); System.out.println(singleSchedule.getSchedule_id()); if( singleSchedule.getResponseCode() == SUCCESS){ return singleSchedule.getSchedule_id(); } } catch (APIConnectionException e) { e.printStackTrace(); } } catch (APIRequestException e) { e.printStackTrace(); } return null; } public static String sendScheduleAll(Map<String, String> parm){ PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(parm.get("content")) .setBadge(+1) .setSound("happy") .addExtras(parm) .build()) .addPlatformNotification(AndroidNotification.newBuilder() .addExtras(parm) .setTitle(parm.get("title")) .setAlert(IosAlert.newBuilder() .setTitleAndBody(parm.get("title"),"",parm.get("content")).build()) .setAlertType(1) .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(true).build()) .build(); try { try { ScheduleResult singleSchedule = jPushClient.createSingleSchedule("schedule",parm.get("date"), payload); System.out.println(singleSchedule.isResultOK()); System.out.println(singleSchedule.getSchedule_id()); if( singleSchedule.getResponseCode() == SUCCESS){ return singleSchedule.getSchedule_id(); } } catch (APIConnectionException e) { e.printStackTrace(); } } catch (APIRequestException e) { e.printStackTrace(); } return null; } //极光推送>>Android public static Long jpushAndroid(Map<String, String> parm) { PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .addPlatformNotification(AndroidNotification.newBuilder() .addExtras(parm) .setTitle(parm.get("title")) .setAlert(parm.get("content")) .setAlertType(1) .build()) .build()) .setMessage(Message.content(parm.get("content"))) .setOptions(Options.newBuilder().setApnsProduction(true).build()) .build(); try { PushResult pu = jPushClient.sendPush(payload); if( pu.getResponseCode() == SUCCESS){ return pu.msg_id; } } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return null; } //极光推送>>ios public static Long jpushIOS(Map<String, String> parm) { PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.ios()) .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(IosAlert.newBuilder() .setTitleAndBody(parm.get("title"),"",parm.get("content")).build()) .setBadge(+1) .setSound("happy") .addExtras(parm) .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(true).build()) .build(); try { PushResult pu = jPushClient.sendPush(payload); if( pu.getResponseCode() == SUCCESS){ return pu.msg_id; } } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return null; } //极光推送>>All所有平台 public static Long jpushAll(Map<String, String> parm) { PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(IosAlert.newBuilder() .setTitleAndBody(parm.get("title"),"",parm.get("content")).build()) .setBadge(+1) .setSound("happy") .addExtras(parm) .build()) .addPlatformNotification(AndroidNotification.newBuilder() .addExtras(parm) .setTitle(parm.get("title")) .setAlert(parm.get("content")) .setAlertType(1) .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(true).build()) .build(); try { PushResult pu = jPushClient.sendPush(payload); if( pu.getResponseCode() == SUCCESS){ return pu.msg_id; } } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return null; } public static Long jpushIOStest(Map<String, String> parm) { JsonObject object = new JsonObject(); object.addProperty("title","ceshi title"); object.addProperty("body","ceshi body"); JSONObject jsonObject = new JSONObject(); IosAlert.Builder builder = new IosAlert.Builder(); builder.setTitleAndBody("title111","subtitle","body111"); //创建JPushClient PushPayload payload = PushPayload.newBuilder() .setPlatform(Platform.ios()) .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(object) // .setAlert(IosAlert.newBuilder() // .setTitleAndBody("title","subtitle","neirong").build()) .setBadge(+1) .setSound("happy") // .addExtras(parm) .build()) .build()) .setOptions(Options.newBuilder().setApnsProduction(true).build()) .build(); try { PushResult pu = jPushClient.sendPush(payload); if( pu.getResponseCode() == SUCCESS){ return pu.msg_id; } } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return null; } }

集成ios推送标题+内容需要注意的地方:

IosNotification.newBuilder() .setAlert(object)   Alert可以是string、object,需要注意一下JsonObject是gson而不是JSONObject;

别忘了build()初始化相应的对象

 

 

最后

以上就是善良豌豆最近收集整理的关于jpush推送通知ios、android的全部内容,更多相关jpush推送通知ios、android内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部