react-native-nitro-ali-push 0.0.0-stage → 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +215 -2
- package/android/CMakeLists.txt +31 -0
- package/android/build.gradle +139 -0
- package/android/src/main/AndroidManifest.xml +30 -0
- package/android/src/main/java/com/margelo/nitro/alipush/AliPushMessageReceiver.kt +156 -0
- package/android/src/main/java/com/margelo/nitro/alipush/HybridAliPushPlugin.kt +303 -0
- package/android/src/main/java/com/margelo/nitro/alipush/ThirdPartMessageActivity.kt +26 -0
- package/ios/HybridAliPushPlugin.swift +516 -0
- package/lib/index.d.ts +195 -0
- package/lib/index.js +269 -0
- package/lib/specs/AliPushPlugin.nitro.d.ts +88 -0
- package/lib/specs/AliPushPlugin.nitro.js +2 -0
- package/lib/types.d.ts +111 -0
- package/lib/types.js +11 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NitroAliPush+autolinking.cmake +84 -0
- package/nitrogen/generated/android/NitroAliPush+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NitroAliPushOnLoad.cpp +58 -0
- package/nitrogen/generated/android/NitroAliPushOnLoad.hpp +34 -0
- package/nitrogen/generated/android/c++/JAliPushAppState.hpp +61 -0
- package/nitrogen/generated/android/c++/JAliPushMessage.hpp +98 -0
- package/nitrogen/generated/android/c++/JAliPushMessageType.hpp +58 -0
- package/nitrogen/generated/android/c++/JFunc_void.hpp +75 -0
- package/nitrogen/generated/android/c++/JFunc_void_AliPushMessage.hpp +87 -0
- package/nitrogen/generated/android/c++/JHybridAliPushPluginSpec.cpp +338 -0
- package/nitrogen/generated/android/c++/JHybridAliPushPluginSpec.hpp +78 -0
- package/nitrogen/generated/android/c++/JVariant_Double_String.cpp +26 -0
- package/nitrogen/generated/android/c++/JVariant_Double_String.hpp +68 -0
- package/nitrogen/generated/android/c++/JVariant_NullType_AliPushMessage.cpp +26 -0
- package/nitrogen/generated/android/c++/JVariant_NullType_AliPushMessage.hpp +80 -0
- package/nitrogen/generated/android/c++/JVariant_NullType_String.cpp +26 -0
- package/nitrogen/generated/android/c++/JVariant_NullType_String.hpp +70 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/AliPushAppState.kt +24 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/AliPushMessage.kt +91 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/AliPushMessageType.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/Func_void.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/Func_void_AliPushMessage.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/HybridAliPushPluginSpec.kt +123 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/NitroAliPushOnLoad.kt +35 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/Variant_Double_String.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/Variant_NullType_AliPushMessage.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/alipush/Variant_NullType_String.kt +62 -0
- package/nitrogen/generated/ios/NitroAliPush+autolinking.rb +62 -0
- package/nitrogen/generated/ios/NitroAliPush-Swift-Cxx-Bridge.cpp +89 -0
- package/nitrogen/generated/ios/NitroAliPush-Swift-Cxx-Bridge.hpp +531 -0
- package/nitrogen/generated/ios/NitroAliPush-Swift-Cxx-Umbrella.hpp +60 -0
- package/nitrogen/generated/ios/NitroAliPushAutolinking.mm +33 -0
- package/nitrogen/generated/ios/NitroAliPushAutolinking.swift +26 -0
- package/nitrogen/generated/ios/c++/HybridAliPushPluginSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridAliPushPluginSpecSwift.hpp +217 -0
- package/nitrogen/generated/ios/swift/AliPushAppState.swift +44 -0
- package/nitrogen/generated/ios/swift/AliPushMessage.swift +191 -0
- package/nitrogen/generated/ios/swift/AliPushMessageType.swift +40 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_AliPushMessage.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_double.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__variant_nitro__NullType__AliPushMessage_.swift +58 -0
- package/nitrogen/generated/ios/swift/HybridAliPushPluginSpec.swift +70 -0
- package/nitrogen/generated/ios/swift/HybridAliPushPluginSpec_cxx.swift +476 -0
- package/nitrogen/generated/ios/swift/Variant_Double_String.swift +30 -0
- package/nitrogen/generated/ios/swift/Variant_NullType_AliPushMessage.swift +30 -0
- package/nitrogen/generated/ios/swift/Variant_NullType_String.swift +30 -0
- package/nitrogen/generated/shared/c++/AliPushAppState.hpp +80 -0
- package/nitrogen/generated/shared/c++/AliPushMessage.hpp +123 -0
- package/nitrogen/generated/shared/c++/AliPushMessageType.hpp +76 -0
- package/nitrogen/generated/shared/c++/HybridAliPushPluginSpec.cpp +36 -0
- package/nitrogen/generated/shared/c++/HybridAliPushPluginSpec.hpp +85 -0
- package/package.json +61 -4
- package/react-native-nitro-ali-push.podspec +29 -0
- package/src/index.ts +284 -0
- package/src/specs/AliPushPlugin.nitro.ts +104 -0
- package/src/types.ts +117 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
package com.margelo.nitro.alipush
|
|
2
|
+
|
|
3
|
+
import android.app.Application
|
|
4
|
+
import android.app.NotificationManager
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.os.Build
|
|
7
|
+
import com.alibaba.sdk.android.push.CommonCallback
|
|
8
|
+
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory
|
|
9
|
+
import com.margelo.nitro.core.NullType
|
|
10
|
+
import com.margelo.nitro.core.Promise
|
|
11
|
+
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
12
|
+
import kotlin.coroutines.resume
|
|
13
|
+
import kotlin.coroutines.resumeWithException
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* react-native-nitro-ali-push 的 Android 原生实现(Kotlin,Nitro HybridObject)。
|
|
17
|
+
*
|
|
18
|
+
* 通过 `nitrogen` 生成的 `HybridAliPushPluginSpec` 抽象类暴露给 JS。推送事件通过静态
|
|
19
|
+
* 监听器池(`listeners`)直接派发到 JS 回调,不再依赖 `ReactApplicationContext` /
|
|
20
|
+
* `DeviceEventManagerModule`(这正是 Nitro 与原 TurboModule 实现的主要差异)。
|
|
21
|
+
*/
|
|
22
|
+
class HybridAliPushPlugin : HybridAliPushPluginSpec() {
|
|
23
|
+
/** 记录最近一次设置的应用角标,供 `getApplicationIconBadgeNumber` 返回。 */
|
|
24
|
+
private var badgeNumber: Double = 0.0
|
|
25
|
+
|
|
26
|
+
// MARK: - 静态事件池(供广播接收器调用)
|
|
27
|
+
|
|
28
|
+
companion object {
|
|
29
|
+
private val listeners = mutableMapOf<Int, (AliPushMessage) -> Unit>()
|
|
30
|
+
private var listenerSeq = 0
|
|
31
|
+
private var initialMessage: AliPushMessage? = null
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 由广播接收器调用,把推送载荷派发到 JS(无监听器时缓存首条冷启动消息)。
|
|
35
|
+
*/
|
|
36
|
+
@JvmStatic
|
|
37
|
+
fun dispatch(message: AliPushMessage) {
|
|
38
|
+
if (listeners.isEmpty()) {
|
|
39
|
+
if (initialMessage == null) initialMessage = message
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
listeners.values.forEach { it.invoke(message) }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 读取并清空缓存的首条冷启动消息。 */
|
|
46
|
+
@JvmStatic
|
|
47
|
+
fun consumeInitialMessage(): AliPushMessage? {
|
|
48
|
+
val m = initialMessage
|
|
49
|
+
initialMessage = null
|
|
50
|
+
return m
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// MARK: - 获取 Application Context
|
|
55
|
+
|
|
56
|
+
private fun appContext(): Context {
|
|
57
|
+
return try {
|
|
58
|
+
val clazz = Class.forName("android.app.ActivityThread")
|
|
59
|
+
val currentApp = clazz.getMethod("currentApplication").invoke(null) as? Application
|
|
60
|
+
currentApp?.applicationContext
|
|
61
|
+
?: throw RuntimeException("Application context unavailable")
|
|
62
|
+
} catch (e: Exception) {
|
|
63
|
+
throw RuntimeException("Failed to obtain Application context", e)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// MARK: - 初始化 / 设备号
|
|
68
|
+
|
|
69
|
+
override fun initPush(appKey: String, appSecret: String): Promise<Unit> {
|
|
70
|
+
return Promise.async {
|
|
71
|
+
suspendCancellableCoroutine { cont ->
|
|
72
|
+
try {
|
|
73
|
+
val applicationContext = appContext()
|
|
74
|
+
PushServiceFactory.init(applicationContext)
|
|
75
|
+
PushServiceFactory.getCloudPushService()
|
|
76
|
+
.registerApp(
|
|
77
|
+
appKey,
|
|
78
|
+
appSecret,
|
|
79
|
+
object : CommonCallback {
|
|
80
|
+
override fun onSuccess(response: String?) {
|
|
81
|
+
cont.resume(Unit)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
override fun onFailed(errorCode: String?, errorMessage: String?) {
|
|
85
|
+
cont.resumeWithException(
|
|
86
|
+
RuntimeException("initPush failed: $errorCode $errorMessage")
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
)
|
|
91
|
+
} catch (e: Throwable) {
|
|
92
|
+
cont.resumeWithException(e)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
override fun getDeviceId(): Promise<String> {
|
|
99
|
+
return Promise.parallel {
|
|
100
|
+
PushServiceFactory.getCloudPushService().deviceId
|
|
101
|
+
?: throw RuntimeException("deviceId is null")
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// MARK: - 账号
|
|
106
|
+
|
|
107
|
+
override fun bindAccount(account: String): Promise<Unit> {
|
|
108
|
+
return Promise.async {
|
|
109
|
+
suspendCancellableCoroutine { cont ->
|
|
110
|
+
PushServiceFactory.getCloudPushService()
|
|
111
|
+
.bindAccount(
|
|
112
|
+
account,
|
|
113
|
+
object : CommonCallback {
|
|
114
|
+
override fun onSuccess(response: String?) = cont.resume(Unit)
|
|
115
|
+
override fun onFailed(code: String?, message: String?) =
|
|
116
|
+
cont.resumeWithException(RuntimeException("bindAccount failed: $code $message"))
|
|
117
|
+
},
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
override fun unbindAccount(): Promise<Unit> {
|
|
124
|
+
return Promise.async {
|
|
125
|
+
suspendCancellableCoroutine { cont ->
|
|
126
|
+
PushServiceFactory.getCloudPushService()
|
|
127
|
+
.unbindAccount(
|
|
128
|
+
object : CommonCallback {
|
|
129
|
+
override fun onSuccess(response: String?) = cont.resume(Unit)
|
|
130
|
+
override fun onFailed(code: String?, message: String?) =
|
|
131
|
+
cont.resumeWithException(RuntimeException("unbindAccount failed: $code $message"))
|
|
132
|
+
},
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// MARK: - 标签
|
|
139
|
+
|
|
140
|
+
override fun bindTag(
|
|
141
|
+
target: Double,
|
|
142
|
+
tags: Array<String>,
|
|
143
|
+
alias: Variant_NullType_String?,
|
|
144
|
+
): Promise<Unit> {
|
|
145
|
+
val aliasString = if (alias is Variant_NullType_String.Second) alias.value else null
|
|
146
|
+
return Promise.async {
|
|
147
|
+
suspendCancellableCoroutine { cont ->
|
|
148
|
+
PushServiceFactory.getCloudPushService()
|
|
149
|
+
.bindTag(
|
|
150
|
+
target.toInt(),
|
|
151
|
+
tags,
|
|
152
|
+
aliasString,
|
|
153
|
+
object : CommonCallback {
|
|
154
|
+
override fun onSuccess(response: String?) = cont.resume(Unit)
|
|
155
|
+
override fun onFailed(code: String?, message: String?) =
|
|
156
|
+
cont.resumeWithException(RuntimeException("bindTag failed: $code $message"))
|
|
157
|
+
},
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
override fun unbindTag(
|
|
164
|
+
target: Double,
|
|
165
|
+
tags: Array<String>,
|
|
166
|
+
alias: Variant_NullType_String?,
|
|
167
|
+
): Promise<Unit> {
|
|
168
|
+
val aliasString = if (alias is Variant_NullType_String.Second) alias.value else null
|
|
169
|
+
return Promise.async {
|
|
170
|
+
suspendCancellableCoroutine { cont ->
|
|
171
|
+
PushServiceFactory.getCloudPushService()
|
|
172
|
+
.unbindTag(
|
|
173
|
+
target.toInt(),
|
|
174
|
+
tags,
|
|
175
|
+
aliasString,
|
|
176
|
+
object : CommonCallback {
|
|
177
|
+
override fun onSuccess(response: String?) = cont.resume(Unit)
|
|
178
|
+
override fun onFailed(code: String?, message: String?) =
|
|
179
|
+
cont.resumeWithException(RuntimeException("unbindTag failed: $code $message"))
|
|
180
|
+
},
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
override fun listTags(target: Double): Promise<String> {
|
|
187
|
+
return Promise.async {
|
|
188
|
+
suspendCancellableCoroutine { cont ->
|
|
189
|
+
PushServiceFactory.getCloudPushService()
|
|
190
|
+
.listTags(
|
|
191
|
+
target.toInt(),
|
|
192
|
+
object : CommonCallback {
|
|
193
|
+
override fun onSuccess(response: String?) = cont.resume(response ?: "")
|
|
194
|
+
override fun onFailed(code: String?, message: String?) =
|
|
195
|
+
cont.resumeWithException(RuntimeException("listTags failed: $code $message"))
|
|
196
|
+
},
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// MARK: - 别名
|
|
203
|
+
|
|
204
|
+
override fun addAlias(alias: String): Promise<Unit> {
|
|
205
|
+
return Promise.async {
|
|
206
|
+
suspendCancellableCoroutine { cont ->
|
|
207
|
+
PushServiceFactory.getCloudPushService()
|
|
208
|
+
.addAlias(
|
|
209
|
+
alias,
|
|
210
|
+
object : CommonCallback {
|
|
211
|
+
override fun onSuccess(response: String?) = cont.resume(Unit)
|
|
212
|
+
override fun onFailed(code: String?, message: String?) =
|
|
213
|
+
cont.resumeWithException(RuntimeException("addAlias failed: $code $message"))
|
|
214
|
+
},
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
override fun removeAlias(alias: String): Promise<Unit> {
|
|
221
|
+
return Promise.async {
|
|
222
|
+
suspendCancellableCoroutine { cont ->
|
|
223
|
+
PushServiceFactory.getCloudPushService()
|
|
224
|
+
.removeAlias(
|
|
225
|
+
alias,
|
|
226
|
+
object : CommonCallback {
|
|
227
|
+
override fun onSuccess(response: String?) = cont.resume(Unit)
|
|
228
|
+
override fun onFailed(code: String?, message: String?) =
|
|
229
|
+
cont.resumeWithException(RuntimeException("removeAlias failed: $code $message"))
|
|
230
|
+
},
|
|
231
|
+
)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
override fun listAliases(): Promise<String> {
|
|
237
|
+
return Promise.async {
|
|
238
|
+
suspendCancellableCoroutine { cont ->
|
|
239
|
+
PushServiceFactory.getCloudPushService()
|
|
240
|
+
.listAliases(
|
|
241
|
+
object : CommonCallback {
|
|
242
|
+
override fun onSuccess(response: String?) = cont.resume(response ?: "")
|
|
243
|
+
override fun onFailed(code: String?, message: String?) =
|
|
244
|
+
cont.resumeWithException(RuntimeException("listAliases failed: $code $message"))
|
|
245
|
+
},
|
|
246
|
+
)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// MARK: - 角标
|
|
252
|
+
|
|
253
|
+
override fun setApplicationIconBadgeNumber(badge: Double): Promise<Unit> {
|
|
254
|
+
return Promise.parallel {
|
|
255
|
+
PushServiceFactory.getCloudPushService().setBadgeNum(appContext(), badge.toInt())
|
|
256
|
+
badgeNumber = badge
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
override fun getApplicationIconBadgeNumber(): Promise<Double> {
|
|
261
|
+
return Promise.resolved(badgeNumber)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
override fun syncBadgeNum(badge: Double): Promise<Unit> {
|
|
265
|
+
// Android 端阿里云推送无需单独同步角标,直接成功。
|
|
266
|
+
return Promise.resolved()
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// MARK: - 冷启动首条消息 & 授权状态
|
|
270
|
+
|
|
271
|
+
override fun getInitialMessage(): Promise<Variant_NullType_AliPushMessage> {
|
|
272
|
+
val message = consumeInitialMessage()
|
|
273
|
+
return if (message != null) {
|
|
274
|
+
Promise.resolved(Variant_NullType_AliPushMessage.create(message))
|
|
275
|
+
} else {
|
|
276
|
+
Promise.resolved(Variant_NullType_AliPushMessage.create(NullType))
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
override fun getAuthorizationStatus(): Promise<Boolean> {
|
|
281
|
+
return Promise.parallel {
|
|
282
|
+
val context = appContext()
|
|
283
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
284
|
+
val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
285
|
+
nm.areNotificationsEnabled()
|
|
286
|
+
} else {
|
|
287
|
+
true
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// MARK: - 事件订阅
|
|
293
|
+
|
|
294
|
+
override fun addListener(listener: (message: AliPushMessage) -> Unit): () -> Unit {
|
|
295
|
+
val id = ++listenerSeq
|
|
296
|
+
listeners[id] = listener
|
|
297
|
+
val cached = consumeInitialMessage()
|
|
298
|
+
if (cached != null) listener(cached)
|
|
299
|
+
return {
|
|
300
|
+
listeners.remove(id)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
package com.margelo.nitro.alipush
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.os.Bundle
|
|
5
|
+
import android.util.Log
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 第三方通道(小米 / 华为 / 魅族 / OPPO / vivo 等)通知点击后的中转 Activity。
|
|
9
|
+
*
|
|
10
|
+
* 在 `AndroidManifest.xml` 中以 `android:exported="true"` + 合适的 `intent-filter`
|
|
11
|
+
* 注册后,用户点击系统通知会先拉起本 Activity,由其把通知数据通过
|
|
12
|
+
* [AliPushMessageReceiver.dispatchFromIntent] 派发到 JS。若宿主 App 不需要此中转逻辑,
|
|
13
|
+
* 可忽略本类。
|
|
14
|
+
*/
|
|
15
|
+
class ThirdPartMessageActivity : Activity() {
|
|
16
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
|
17
|
+
super.onCreate(savedInstanceState)
|
|
18
|
+
try {
|
|
19
|
+
// 由广播接收器统一处理通知点击数据。
|
|
20
|
+
AliPushMessageReceiver.dispatchFromIntent(intent?.extras)
|
|
21
|
+
} catch (e: Exception) {
|
|
22
|
+
Log.e("AliPush", "ThirdPartMessageActivity error", e)
|
|
23
|
+
}
|
|
24
|
+
finish()
|
|
25
|
+
}
|
|
26
|
+
}
|