Skip to content

Commit ab25d84

Browse files
author
Wonday
committed
getDeviceId()接口变更为Promise模式
1 parent a504938 commit ab25d84

File tree

5 files changed

+70
-12
lines changed

5 files changed

+70
-12
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@
66

77
### 修改履历
88

9+
v1.0.12
10+
1. getDeviceId()逻辑处理变更为一次取得失败后延迟三秒再次获取
11+
2. ```重要变更```getDeviceId()接口变更为Promise模式,使用旧版本需要升级代码。
12+
13+
14+
**v1.0.11及以前代码用法:**
15+
```
16+
AliyunPush.getDeviceId((deviceId)=>{
17+
console.log("AliyunPush DeviceId:" + deviceId);
18+
});
19+
```
20+
21+
**v1.0.12及以后代码用法:**
22+
```
23+
AliyunPush.getDeviceId()
24+
.then((deviceId)=>{
25+
//console.log("deviceId:"+deviceId);
26+
})
27+
.catch((error)=>{
28+
console.log("getDeviceId() failed");
29+
});
30+
```
31+
32+
933
v1.0.11
1034

1135
1. 增加角标同步功能syncBadgeNum()(仅iOS支持)
@@ -290,9 +314,13 @@ handleAliyunPushMessage = (e) => {
290314

291315
示例:
292316
```
293-
AliyunPush.getDeviceId((deviceId)=>{
294-
console.log("AliyunPush DeviceId:" + deviceId);
295-
});
317+
AliyunPush.getDeviceId()
318+
.then((deviceId)=>{
319+
//console.log("deviceId:"+deviceId);
320+
})
321+
.catch((error)=>{
322+
console.log("getDeviceId() failed");
323+
});
296324
```
297325
**绑定账号**
298326

android/src/main/java/org/wonday/aliyun/push/AliyunPushModule.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,26 @@ public String getName() {
6565
}
6666

6767
@ReactMethod
68-
public void getDeviceId(Callback callback) {
69-
callback.invoke(PushServiceFactory.getCloudPushService().getDeviceId());
68+
public void getDeviceId(final Promise promise) {
69+
String deviceID = PushServiceFactory.getCloudPushService().getDeviceId();
70+
if (deviceID!=null && deviceID.length()>0) {
71+
promise.resolve(deviceID);
72+
} else {
73+
// 或许还没有初始化完成,等3秒钟再次尝试
74+
try{
75+
Thread.sleep(3000);
76+
deviceID = PushServiceFactory.getCloudPushService().getDeviceId();
77+
78+
if (deviceID!=null && deviceID.length()>0) {
79+
promise.resolve(deviceID);
80+
return;
81+
}
82+
} catch (Exception e) {
83+
84+
}
85+
86+
promise.reject("getDeviceId() failed.");
87+
}
7088
}
7189

7290
@ReactMethod

index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ function getKey(listener,type){
3939

4040
export default class AliyunPush {
4141

42-
static getDeviceId = (callback) => {
43-
AliyunPushNative.getDeviceId(function(args) {
44-
callback(args);
45-
});
42+
static getDeviceId = () => {
43+
return AliyunPushNative.getDeviceId();
4644
}
4745

4846
static getApplicationIconBadgeNumber = (callback) => {

ios/RCTAliyunPush/AliyunPushManager.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,24 @@ + (AliyunPushManager *)sharedInstance
128128
/**
129129
* Get the aliyun push device id
130130
*/
131-
RCT_EXPORT_METHOD(getDeviceId:(RCTResponseSenderBlock)callback)
131+
RCT_EXPORT_METHOD(getDeviceId:(RCTPromiseResolveBlock)resolve
132+
rejecter:(RCTPromiseRejectBlock)reject)
132133
{
133134
NSString *deviceId = [CloudPushSDK getDeviceId];
134-
callback(@[deviceId]);
135+
if (deviceId!=Nil) {
136+
resolve(deviceId);
137+
} else {
138+
// 或许还没有初始化完成,等3秒钟再次尝试
139+
[NSThread sleepForTimeInterval:3.0f];
140+
141+
deviceId = [CloudPushSDK getDeviceId];
142+
if (deviceId!=Nil) {
143+
resolve(deviceId);
144+
} else {
145+
reject([NSString stringWithFormat:@"getDeviceId() failed."], nil, RCTErrorWithMessage(@"getDeviceId() failed."));
146+
}
147+
148+
}
135149
}
136150

137151
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-aliyun-push",
3-
"version": "1.0.11",
3+
"version": "1.0.12",
44
"description": "A react native wrapper for aliyun push SDK",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)