react-native-nitro-ali-push 1.0.0 → 1.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.
- package/README.md +65 -40
- package/ios/HybridAliPushPlugin.swift +9 -6
- package/package.json +1 -1
- package/react-native-nitro-ali-push.podspec +15 -3
package/README.md
CHANGED
|
@@ -41,58 +41,83 @@ cd ios && pod install
|
|
|
41
41
|
|
|
42
42
|
## iOS 集成
|
|
43
43
|
|
|
44
|
-
### 1.
|
|
44
|
+
### 1. 引入阿里云推送 SDK(`CloudPushSDK`)
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
推荐方式:放到本库 `ios/` 目录下,并在 `react-native-nitro-ali-push.podspec` 中取消注释 `s.vendored_frameworks`,或改为你自己的引入方式。
|
|
46
|
+
**方式一:CocoaPods(默认,推荐)**
|
|
48
47
|
|
|
49
|
-
|
|
48
|
+
阿里云推送的 pod 不在 CocoaPods trunk,而在官方自己的 spec 仓库,所以要先在 `ios/Podfile` **顶部**声明 source:
|
|
50
49
|
|
|
51
|
-
|
|
50
|
+
```ruby
|
|
51
|
+
source 'https://github.com/aliyun/aliyun-specs.git'
|
|
52
|
+
source 'https://cdn.cocoapods.org/'
|
|
53
|
+
```
|
|
52
54
|
|
|
53
|
-
|
|
54
|
-
#import "AppDelegate.h"
|
|
55
|
-
#import <UserNotifications/UserNotifications.h>
|
|
56
|
-
#import <CloudPushSDK/CloudPushSDK.h>
|
|
57
|
-
#import <NitroAliPush/NitroAliPush-Swift.h>
|
|
55
|
+
若本机还没添加过该仓库,先执行一次:
|
|
58
56
|
|
|
59
|
-
|
|
57
|
+
```bash
|
|
58
|
+
pod repo add AliyunRepo https://github.com/aliyun/aliyun-specs.git
|
|
59
|
+
```
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
// ... 其他初始化(RCTBridge 等)...
|
|
61
|
+
本库 podspec 默认已声明 `s.dependency "AlicloudPush", "~> 3"`,因此无需再做其它配置,直接 `pod install` 即可。
|
|
63
62
|
|
|
64
|
-
|
|
65
|
-
[HybridAliPushPlugin.shared setParams:@"<appKey>"
|
|
66
|
-
appSecret:@"<appSecret>"
|
|
67
|
-
lauchOptions:launchOptions
|
|
68
|
-
createNotificationCategoryHandler:^{ }];
|
|
63
|
+
**方式二:手动集成**
|
|
69
64
|
|
|
70
|
-
|
|
71
|
-
|
|
65
|
+
从 [阿里云移动推送控制台](https://push.console.aliyun.com/) 下载 iOS SDK,把 `CloudPushSDK.framework`(3.x 为 `CloudPushSDK.xcframework`)放到本库 `ios/` 目录下。
|
|
66
|
+
podspec 检测到该文件后会自动改用 `vendored_frameworks`,不再依赖 `AlicloudPush`,无需修改 podspec。
|
|
72
67
|
|
|
73
|
-
|
|
74
|
-
- (void)application:(UIApplication *)application
|
|
75
|
-
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
|
|
76
|
-
[[HybridAliPushPlugin.shared application:application
|
|
77
|
-
didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
|
|
78
|
-
}
|
|
68
|
+
> 若 `pod install` 报 `Unable to find a specification for 'AlicloudPush'`,说明 Podfile 里缺少上面的阿里云 source。
|
|
79
69
|
|
|
80
|
-
|
|
81
|
-
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
|
|
82
|
-
[[HybridAliPushPlugin.shared application:application
|
|
83
|
-
didFailToRegisterForRemoteNotificationsWithError:error];
|
|
84
|
-
}
|
|
70
|
+
### 2. AppDelegate 中初始化
|
|
85
71
|
|
|
86
|
-
|
|
87
|
-
- (void)application:(UIApplication *)application
|
|
88
|
-
didReceiveRemoteNotification:(NSDictionary *)userInfo
|
|
89
|
-
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
|
|
90
|
-
[[HybridAliPushPlugin.shared application:application
|
|
91
|
-
didReceiveRemoteNotification:userInfo
|
|
92
|
-
fetchCompletionHandler:completionHandler];
|
|
93
|
-
}
|
|
72
|
+
iOS 端的推送初始化必须在原生层完成(不能在 JS 中 `initPush`):
|
|
94
73
|
|
|
95
|
-
|
|
74
|
+
```swift
|
|
75
|
+
import UIKit
|
|
76
|
+
import UserNotifications
|
|
77
|
+
// 本库模块名由 podspec 名 react-native-nitro-ali-push 推导为 react_native_nitro_ali_push。
|
|
78
|
+
// HybridAliPushPlugin 是 public,跨模块使用需 import 本库模块(不在 NitroAliPush)。
|
|
79
|
+
import react_native_nitro_ali_push
|
|
80
|
+
|
|
81
|
+
@main
|
|
82
|
+
class AppDelegate: UIResponder, UIApplicationDelegate {
|
|
83
|
+
|
|
84
|
+
func application(_ application: UIApplication,
|
|
85
|
+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
|
86
|
+
// ... 其他初始化(RCTBridge 等)...
|
|
87
|
+
|
|
88
|
+
// 初始化阿里云推送
|
|
89
|
+
HybridAliPushPlugin.shared().setParams(
|
|
90
|
+
"<appKey>",
|
|
91
|
+
appSecret: "<appSecret>",
|
|
92
|
+
lauchOptions: launchOptions as? [AnyHashable: Any],
|
|
93
|
+
createNotificationCategoryHandler: {}
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
return true
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 注册 deviceToken
|
|
100
|
+
func application(_ application: UIApplication,
|
|
101
|
+
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
|
102
|
+
HybridAliPushPlugin.shared().application(application,
|
|
103
|
+
didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
func application(_ application: UIApplication,
|
|
107
|
+
didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
|
108
|
+
HybridAliPushPlugin.shared().application(application,
|
|
109
|
+
didFailToRegisterForRemoteNotificationsWithError: error)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 收到远程通知
|
|
113
|
+
func application(_ application: UIApplication,
|
|
114
|
+
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
|
|
115
|
+
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
|
|
116
|
+
HybridAliPushPlugin.shared().application(application,
|
|
117
|
+
didReceiveRemoteNotification: userInfo,
|
|
118
|
+
fetchCompletionHandler: completionHandler)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
96
121
|
```
|
|
97
122
|
|
|
98
123
|
> 若 CocoaPods 编译时提示找不到 `React` / `CloudPushSDK` 模块,请在 Podfile 中开启 `use_frameworks!` 或 `use_modular_headers!`。
|
|
@@ -25,8 +25,11 @@ private func DLog(_ message: String, file: String = #file, line: UInt = #line) {
|
|
|
25
25
|
* iOS 端的推送初始化必须在原生层完成(AppDelegate 调用 `shared.setParams`),而不是
|
|
26
26
|
* 从 JS 调用 `initPush`(该方法在 iOS 上为无操作)。
|
|
27
27
|
*/
|
|
28
|
+
/// 必须 `public`:本库作为 pod 是独立模块,App 的 Swift/ObjC 需要引用它。
|
|
29
|
+
/// 仅 `internal` 时不会写进 `<react_native_nitro_ali_push-Swift.h>`,App 端编译报
|
|
30
|
+
/// "Use of undeclared identifier 'HybridAliPushPlugin'"。`@objc` 运行时名保持不变。
|
|
28
31
|
@objc(HybridAliPushPlugin)
|
|
29
|
-
class HybridAliPushPlugin: HybridAliPushPluginSpec, UNUserNotificationCenterDelegate {
|
|
32
|
+
public final class HybridAliPushPlugin: HybridAliPushPluginSpec, UNUserNotificationCenterDelegate {
|
|
30
33
|
/// 由 AppDelegate 创建的权威实例(持有 UNUserNotificationCenter 代理与 CloudPushSDK 回调)。
|
|
31
34
|
private static var sharedInstance: HybridAliPushPlugin?
|
|
32
35
|
/// JS 注册的监听器(静态池,AppDelegate 实例与 JS 实例共享)。
|
|
@@ -36,7 +39,7 @@ class HybridAliPushPlugin: HybridAliPushPluginSpec, UNUserNotificationCenterDele
|
|
|
36
39
|
|
|
37
40
|
// MARK: - 单例(供 AppDelegate 使用)
|
|
38
41
|
|
|
39
|
-
@objc static func shared() -> HybridAliPushPlugin {
|
|
42
|
+
@objc public static func shared() -> HybridAliPushPlugin {
|
|
40
43
|
if let instance = sharedInstance { return instance }
|
|
41
44
|
let instance = HybridAliPushPlugin()
|
|
42
45
|
sharedInstance = instance
|
|
@@ -51,7 +54,7 @@ class HybridAliPushPlugin: HybridAliPushPluginSpec, UNUserNotificationCenterDele
|
|
|
51
54
|
|
|
52
55
|
// MARK: - 由原生 AppDelegate 调用完成 iOS 端推送初始化(不应从 JS 调用)
|
|
53
56
|
|
|
54
|
-
@objc func setParams(_ appKey: String,
|
|
57
|
+
@objc public func setParams(_ appKey: String,
|
|
55
58
|
appSecret: String,
|
|
56
59
|
lauchOptions: [AnyHashable: Any]?,
|
|
57
60
|
createNotificationCategoryHandler: @escaping () -> Void) {
|
|
@@ -95,7 +98,7 @@ class HybridAliPushPlugin: HybridAliPushPluginSpec, UNUserNotificationCenterDele
|
|
|
95
98
|
|
|
96
99
|
// MARK: - AppDelegate 转发方法
|
|
97
100
|
|
|
98
|
-
@objc func application(_ application: UIApplication,
|
|
101
|
+
@objc public func application(_ application: UIApplication,
|
|
99
102
|
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
|
|
100
103
|
CloudPushSDK.registerDevice(deviceToken) { res in
|
|
101
104
|
if res?.success ?? false {
|
|
@@ -106,12 +109,12 @@ class HybridAliPushPlugin: HybridAliPushPluginSpec, UNUserNotificationCenterDele
|
|
|
106
109
|
}
|
|
107
110
|
}
|
|
108
111
|
|
|
109
|
-
@objc func application(_ application: UIApplication,
|
|
112
|
+
@objc public func application(_ application: UIApplication,
|
|
110
113
|
didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
|
111
114
|
DLog("Get deviceToken failed, error: \(error.localizedDescription)")
|
|
112
115
|
}
|
|
113
116
|
|
|
114
|
-
@objc func application(_ application: UIApplication,
|
|
117
|
+
@objc public func application(_ application: UIApplication,
|
|
115
118
|
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
|
|
116
119
|
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
|
|
117
120
|
CloudPushSDK.sendNotificationAck(userInfo)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-ali-push",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "React Native wrapper for Alibaba Cloud Push (阿里云移动推送), built with Nitro Modules (swift/kotlin HybridObject).",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -21,9 +21,21 @@ Pod::Spec.new do |s|
|
|
|
21
21
|
load 'nitrogen/generated/ios/NitroAliPush+autolinking.rb'
|
|
22
22
|
add_nitrogen_files(s)
|
|
23
23
|
|
|
24
|
-
# 阿里云推送 SDK(CloudPushSDK
|
|
25
|
-
#
|
|
26
|
-
#
|
|
24
|
+
# 阿里云推送 SDK(CloudPushSDK)。
|
|
25
|
+
#
|
|
26
|
+
# 默认:走官方 CocoaPods 源 `AlicloudPush`。它不在 CocoaPods trunk,而是阿里云自己的
|
|
27
|
+
# spec 仓库,因此使用方必须在 Podfile 中声明该 source(见 README「iOS 集成」):
|
|
28
|
+
# source 'https://github.com/aliyun/aliyun-specs.git'
|
|
29
|
+
# source 'https://cdn.cocoapods.org/'
|
|
30
|
+
#
|
|
31
|
+
# 手动集成:把 CloudPushSDK.framework(或 CloudPushSDK.xcframework)放到本库 ios/ 目录,
|
|
32
|
+
# 这里会自动改为 vendored 方式,不再依赖 AlicloudPush,两者不会同时引入。
|
|
33
|
+
vendored = Dir.glob(File.join(__dir__, "ios", "CloudPushSDK.{framework,xcframework}"))
|
|
34
|
+
if vendored.empty?
|
|
35
|
+
s.dependency "AlicloudPush", "~> 3"
|
|
36
|
+
else
|
|
37
|
+
s.vendored_frameworks = vendored.map { |p| "ios/#{File.basename(p)}" }
|
|
38
|
+
end
|
|
27
39
|
|
|
28
40
|
s.dependency "React-Core"
|
|
29
41
|
end
|