capacitor-jpush-core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,245 @@
1
+ package com.capacitor.jpush.core
2
+
3
+ import android.content.Context
4
+ import android.content.Intent
5
+ import android.util.Log
6
+ import cn.jpush.android.api.CustomMessage
7
+ import cn.jpush.android.api.JPushMessage
8
+ import cn.jpush.android.api.NotificationMessage
9
+ import cn.jpush.android.service.JPushMessageReceiver
10
+ import com.getcapacitor.JSObject
11
+ import org.json.JSONException
12
+ import org.json.JSONObject
13
+
14
+
15
+ /**
16
+ * 自定义JPush消息接收器,接收推送消息并转发给JS层
17
+ */
18
+ class JPushReceiver : JPushMessageReceiver() {
19
+
20
+ private val TAG = "JPushReceiver"
21
+
22
+ companion object {
23
+ // 推送事件广播的action
24
+ const val ACTION_REGISTRATION_COMPLETED = "jpush:registrationCompleted"
25
+ const val ACTION_REGISTRATION_FAILED = "jpush:registrationFailed"
26
+ const val ACTION_NOTIFICATION_RECEIVED = "jpush:notificationReceived"
27
+ const val ACTION_NOTIFICATION_OPENED = "jpush:notificationOpened"
28
+ const val ACTION_CUSTOM_MESSAGE_RECEIVED = "jpush:customMessageReceived"
29
+
30
+ // 消息数据的键
31
+ const val EXTRA_DATA = "extra_data"
32
+ }
33
+
34
+ /**
35
+ * 接收注册成功事件, 只有首次安装的首次初始化才会进入,发送通知改用 registerToken 方法的回调去返回结果。
36
+ */
37
+ override fun onRegister(context: Context, registrationId: String) {
38
+ Log.d(TAG, "JPush onRegister: $registrationId")
39
+
40
+ // 发送广播给插件
41
+ if (registrationId.isNotEmpty()) {
42
+ // 注册成功
43
+ val data = JSObject().apply {
44
+ put("registrationID", registrationId)
45
+ }
46
+ Log.i(TAG, data.toString())
47
+ // sendEventBroadcast(context, ACTION_REGISTRATION_COMPLETED, data)
48
+ } else {
49
+ // 注册失败
50
+ val data = JSObject().apply {
51
+ put("errorCode", -1)
52
+ put("errorMessage", "注册ID为空")
53
+ }
54
+ Log.i(TAG, data.toString())
55
+ // sendEventBroadcast(context, ACTION_REGISTRATION_FAILED, data)
56
+ }
57
+ }
58
+
59
+ /**
60
+ * 接收自定义消息
61
+ */
62
+ override fun onMessage(context: Context, customMessage: CustomMessage) {
63
+ Log.d(TAG, "JPush onMessage: ${customMessage.message}")
64
+
65
+ // 构建消息对象
66
+ val message = JSObject().apply {
67
+ put("title", customMessage.title)
68
+ put("content", customMessage.message)
69
+ put("type", "custom")
70
+
71
+ // 处理额外数据
72
+ if (!customMessage.extra.isNullOrEmpty()) {
73
+ put("extras", parseExtras(customMessage.extra))
74
+ }
75
+ }
76
+
77
+ // 发送广播给插件
78
+ sendEventBroadcast(context, ACTION_CUSTOM_MESSAGE_RECEIVED, message)
79
+ }
80
+
81
+ /**
82
+ * 接收通知
83
+ */
84
+ override fun onNotifyMessageArrived(context: Context, notificationMessage: NotificationMessage) {
85
+ Log.d(
86
+ TAG,
87
+ "JPush onNotifyMessageArrived: ${notificationMessage.notificationTitle} - ${notificationMessage.notificationContent}"
88
+ )
89
+
90
+ // 构建通知对象
91
+ val notification = JSObject().apply {
92
+ put("title", notificationMessage.notificationTitle)
93
+ put("content", notificationMessage.notificationContent)
94
+ put("messageId", notificationMessage.notificationId)
95
+ put("type", "notification")
96
+
97
+ // 处理额外数据
98
+ if (!notificationMessage.notificationExtras.isNullOrEmpty()) {
99
+ put("extras", parseExtras(notificationMessage.notificationExtras))
100
+ }
101
+ }
102
+
103
+ // 发送广播给插件
104
+ sendEventBroadcast(context, ACTION_NOTIFICATION_RECEIVED, notification)
105
+ }
106
+
107
+ /**
108
+ * 用户点击通知 - JPush SDK直接调用
109
+ */
110
+ override fun onNotifyMessageOpened(context: Context, notificationMessage: NotificationMessage) {
111
+ Log.d(
112
+ TAG,
113
+ "JPush onNotifyMessageOpened: ${notificationMessage.notificationTitle} - ${notificationMessage.notificationContent}"
114
+ )
115
+
116
+ // 构建通知对象
117
+ val notification = JSObject().apply {
118
+ put("title", notificationMessage.notificationTitle)
119
+ put("content", notificationMessage.notificationContent)
120
+ put("messageId", notificationMessage.notificationId)
121
+ put("type", "notification")
122
+
123
+ // 处理额外数据
124
+ if (!notificationMessage.notificationExtras.isNullOrEmpty()) {
125
+ put("extras", parseExtras(notificationMessage.notificationExtras))
126
+ }
127
+ }
128
+
129
+ // 发送广播给插件
130
+ sendEventBroadcast(context, ACTION_NOTIFICATION_OPENED, notification)
131
+ }
132
+
133
+ /**
134
+ * 别名操作结果回调
135
+ */
136
+ override fun onAliasOperatorResult(context: Context, jPushMessage: JPushMessage) {
137
+ super.onAliasOperatorResult(context, jPushMessage)
138
+
139
+ val sequence = jPushMessage.sequence
140
+ val errorCode = jPushMessage.errorCode
141
+ val alias = jPushMessage.alias
142
+
143
+ Log.d(TAG, "别名操作结果: sequence=$sequence, alias=$alias, errorCode=$errorCode")
144
+
145
+ // 处理别名设置结果
146
+ try {
147
+ val data = JSObject().apply {
148
+ put("sequence", sequence)
149
+ put("alias", alias)
150
+ put("errorCode", errorCode)
151
+ put("success", errorCode == 0)
152
+ }
153
+
154
+ if (errorCode == 0) {
155
+ Log.d(TAG, "别名设置成功: ${data.toString()}")
156
+ } else {
157
+ data.put("errorMessage", getErrorMessage(errorCode))
158
+ Log.e(TAG, "别名设置失败: ${data.toString()}")
159
+ }
160
+
161
+ // 不再发送广播,因为TS层没有对应的监听
162
+ } catch (e: Exception) {
163
+ Log.e(TAG, "处理别名操作结果失败: ${e.message}")
164
+ }
165
+ }
166
+
167
+ /**
168
+ * 标签操作结果回调
169
+ */
170
+ override fun onTagOperatorResult(context: Context, jPushMessage: JPushMessage) {
171
+ super.onTagOperatorResult(context, jPushMessage)
172
+
173
+ val sequence = jPushMessage.sequence
174
+ val errorCode = jPushMessage.errorCode
175
+ val tags = jPushMessage.tags
176
+
177
+ Log.d(TAG, "标签操作结果: sequence=$sequence, tags=$tags, errorCode=$errorCode")
178
+
179
+ // 处理标签操作结果
180
+ try {
181
+ val data = JSObject().apply {
182
+ put("sequence", sequence)
183
+ put("tags", tags)
184
+ put("errorCode", errorCode)
185
+ put("success", errorCode == 0)
186
+ }
187
+
188
+ if (errorCode == 0) {
189
+ Log.d(TAG, "标签操作成功: ${data.toString()}")
190
+ } else {
191
+ data.put("errorMessage", getErrorMessage(errorCode))
192
+ Log.e(TAG, "标签操作失败: ${data.toString()}")
193
+ }
194
+
195
+ // 不再发送广播,因为TS层没有对应的监听
196
+ } catch (e: Exception) {
197
+ Log.e(TAG, "处理标签操作结果失败: ${e.message}")
198
+ }
199
+ }
200
+
201
+ /**
202
+ * 根据错误码获取错误信息
203
+ */
204
+ private fun getErrorMessage(errorCode: Int): String {
205
+ return when (errorCode) {
206
+ 0 -> "操作成功"
207
+ 6002 -> "服务器繁忙"
208
+ 6003 -> "无效参数"
209
+ 6004 -> "别名长度超过限制"
210
+ 6005 -> "标签数量超过限制"
211
+ 6006 -> "标签长度超过限制"
212
+ 6008 -> "标签名无效"
213
+ else -> "未知错误: $errorCode"
214
+ }
215
+ }
216
+
217
+ /**
218
+ * 解析额外数据
219
+ */
220
+ private fun parseExtras(extrasJson: String): JSObject {
221
+ val extras = JSObject()
222
+ try {
223
+ val json = JSONObject(extrasJson)
224
+ val keys = json.keys()
225
+ while (keys.hasNext()) {
226
+ val key = keys.next() as String
227
+ val value = json.get(key)
228
+ extras.put(key, value)
229
+ }
230
+ } catch (e: JSONException) {
231
+ Log.e(TAG, "解析额外数据失败: ${e.message}")
232
+ }
233
+ return extras
234
+ }
235
+
236
+ /**
237
+ * 发送事件广播
238
+ */
239
+ private fun sendEventBroadcast(context: Context, action: String, data: JSObject) {
240
+ val intent = Intent(action).apply {
241
+ putExtra(EXTRA_DATA, data.toString())
242
+ }
243
+ context.sendBroadcast(intent)
244
+ }
245
+ }
@@ -0,0 +1,10 @@
1
+ package com.capacitor.jpush.core;
2
+
3
+ import cn.jpush.android.service.JCommonService;
4
+
5
+ /**
6
+ * 自定义JPush Service,用于提升推送通道稳定性(JCore 2.0.0+版本推荐)
7
+ */
8
+ class JPushService : JCommonService() {
9
+ // 无需重写方法,使用默认实现即可
10
+ }
File without changes