Skip to content

Commit

Permalink
getDeviceId()接口变更为Promise模式
Browse files Browse the repository at this point in the history
  • Loading branch information
Wonday committed May 29, 2018
1 parent a504938 commit ab25d84
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@

### 修改履历

v1.0.12
1. getDeviceId()逻辑处理变更为一次取得失败后延迟三秒再次获取
2. ```重要变更```getDeviceId()接口变更为Promise模式,使用旧版本需要升级代码。


**v1.0.11及以前代码用法:**
```
AliyunPush.getDeviceId((deviceId)=>{
console.log("AliyunPush DeviceId:" + deviceId);
});
```

**v1.0.12及以后代码用法:**
```
AliyunPush.getDeviceId()
.then((deviceId)=>{
//console.log("deviceId:"+deviceId);
})
.catch((error)=>{
console.log("getDeviceId() failed");
});
```


v1.0.11

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

示例:
```
AliyunPush.getDeviceId((deviceId)=>{
console.log("AliyunPush DeviceId:" + deviceId);
});
AliyunPush.getDeviceId()
.then((deviceId)=>{
//console.log("deviceId:"+deviceId);
})
.catch((error)=>{
console.log("getDeviceId() failed");
});
```
**绑定账号**

Expand Down
22 changes: 20 additions & 2 deletions android/src/main/java/org/wonday/aliyun/push/AliyunPushModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,26 @@ public String getName() {
}

@ReactMethod
public void getDeviceId(Callback callback) {
callback.invoke(PushServiceFactory.getCloudPushService().getDeviceId());
public void getDeviceId(final Promise promise) {
String deviceID = PushServiceFactory.getCloudPushService().getDeviceId();
if (deviceID!=null && deviceID.length()>0) {
promise.resolve(deviceID);
} else {
// 或许还没有初始化完成,等3秒钟再次尝试
try{
Thread.sleep(3000);
deviceID = PushServiceFactory.getCloudPushService().getDeviceId();

if (deviceID!=null && deviceID.length()>0) {
promise.resolve(deviceID);
return;
}
} catch (Exception e) {

}

promise.reject("getDeviceId() failed.");
}
}

@ReactMethod
Expand Down
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ function getKey(listener,type){

export default class AliyunPush {

static getDeviceId = (callback) => {
AliyunPushNative.getDeviceId(function(args) {
callback(args);
});
static getDeviceId = () => {
return AliyunPushNative.getDeviceId();
}

static getApplicationIconBadgeNumber = (callback) => {
Expand Down
18 changes: 16 additions & 2 deletions ios/RCTAliyunPush/AliyunPushManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,24 @@ + (AliyunPushManager *)sharedInstance
/**
* Get the aliyun push device id
*/
RCT_EXPORT_METHOD(getDeviceId:(RCTResponseSenderBlock)callback)
RCT_EXPORT_METHOD(getDeviceId:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *deviceId = [CloudPushSDK getDeviceId];
callback(@[deviceId]);
if (deviceId!=Nil) {
resolve(deviceId);
} else {
// 或许还没有初始化完成,等3秒钟再次尝试
[NSThread sleepForTimeInterval:3.0f];

deviceId = [CloudPushSDK getDeviceId];
if (deviceId!=Nil) {
resolve(deviceId);
} else {
reject([NSString stringWithFormat:@"getDeviceId() failed."], nil, RCTErrorWithMessage(@"getDeviceId() failed."));
}

}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-aliyun-push",
"version": "1.0.11",
"version": "1.0.12",
"description": "A react native wrapper for aliyun push SDK",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit ab25d84

Please sign in to comment.