Skip to content

Commit 351cedb

Browse files
skysky
authored andcommitted
Add xiaomi push function
1 parent ebf59a6 commit 351cedb

File tree

8 files changed

+241
-1
lines changed

8 files changed

+241
-1
lines changed

android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ repositories {
4242
dependencies {
4343
compile files('libs/vod_upload_with_bos.jar')
4444
compile files('libs/agent_lss-4.3.0-20171222.071535-1.jar')
45+
compile files('libs/MiPush_SDK_Client_3_6_2.jar')
4546
compile 'com.facebook.react:react-native:+'
4647
compile 'com.squareup.okhttp3:okhttp:3.4.1'
4748
}
667 KB
Binary file not shown.

android/src/main/java/com/reactnative/baibubce/RNBaiduBceModule.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Intent;
66
import android.database.Cursor;
77
import android.net.Uri;
8+
import android.os.Build;
89
import android.os.Environment;
910
import android.provider.DocumentsContract;
1011
import android.provider.MediaStore;
@@ -91,7 +92,6 @@ public void initVodAndBosClient() {
9192
bosClient = new BosClient(bosConfig);
9293
}
9394

94-
9595
@ReactMethod
9696
public void show(String message, final Promise promise) {
9797
Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_SHORT).show();
@@ -343,4 +343,37 @@ public static boolean isDownloadsDocument(Uri uri) {
343343
public static boolean isMediaDocument(Uri uri) {
344344
return "com.android.providers.media.documents".equals(uri.getAuthority());
345345
}
346+
/////////////// 小米推送
347+
@ReactMethod
348+
public void getXiaomiRegId(final Promise promise) {
349+
String regId = Utils.getMiPushRegid(getReactApplicationContext());
350+
if(regId == null){
351+
regId = "";
352+
}
353+
WritableMap map = Arguments.createMap();
354+
map.putString("regId", regId);
355+
map.putString("deviceName", getDeviceName());
356+
promise.resolve(map);
357+
}
358+
359+
public String getDeviceName() {
360+
String manufacturer = Build.MANUFACTURER;
361+
String model = Build.MODEL;
362+
if(model.toLowerCase().startsWith(manufacturer.toLowerCase())) {
363+
return capitalize(model);
364+
}else {
365+
return capitalize(manufacturer) + " " + model;
366+
}
367+
}
368+
public String capitalize(String s){
369+
if (s == null || s.length() == 0){
370+
return "";
371+
}
372+
char first = s.charAt(0);
373+
if(Character.isUpperCase(first)){
374+
return s;
375+
} else {
376+
return Character.toUpperCase(first) + s.substring(1);
377+
}
378+
}
346379
}

android/src/main/java/com/reactnative/baibubce/Utils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.reactnative.baibubce;
22

3+
import android.content.Context;
4+
import android.os.Build;
5+
36
import com.facebook.react.bridge.ReactContext;
47
import com.facebook.react.modules.core.DeviceEventManagerModule;
8+
import com.xiaomi.mipush.sdk.MiPushClient;
59

610
/**
711
* Created by sky on 2018/2/4.
@@ -38,4 +42,12 @@ public static void sendUploadProgress(String strpercent)
3842
Utils.emit("videoUploadStatus", strpercent);
3943
}
4044
}
45+
46+
public static void registerMiPush(Context context, String appId, String appKey){
47+
MiPushClient.registerPush(context, "2882303761517765408", "5661776524408");
48+
}
49+
50+
public static String getMiPushRegid(Context context){
51+
return MiPushClient.getRegId(context);
52+
}
4153
}

ios/RNBaidu.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
271271
"$(SRCROOT)/../../react-native/React/**",
272272
"$(SRCROOT)/BaiduSDK/BaiduPlayer",
273+
"$(SRCROOT)/libMiPush",
273274
);
274275
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
275276
ONLY_ACTIVE_ARCH = YES;
@@ -296,6 +297,7 @@
296297
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
297298
"$(SRCROOT)/../../react-native/React/**",
298299
"$(SRCROOT)/BaiduSDK/BaiduPlayer",
300+
"$(SRCROOT)/libMiPush",
299301
);
300302
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
301303
OTHER_LDFLAGS = "-ObjC";

ios/RNBaidu/main.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#import <RCTEventEmitter.h>
1818
#endif
1919
#import "RNBaiduVod.h"
20+
#import "MiPushSDK.h"
21+
#import <sys/utsname.h>
2022

2123
@interface BaiduBce : RCTEventEmitter <RCTBridgeModule>
2224
@end
@@ -79,4 +81,17 @@ - (void)keyboardWillHiden:(NSNotification *)notification {
7981
reject(@"-1", @"error", nil);
8082
}
8183
}
84+
85+
RCT_EXPORT_METHOD(getXiaomiRegId:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
86+
NSString *regId = [MiPushSDK getRegId];
87+
if(regId == NULL){
88+
regId = @"";
89+
}
90+
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
91+
[dic setObject:regId forKey:@"regId"];
92+
struct utsname systemInfo;
93+
uname(&systemInfo);
94+
[dic setObject:[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding] forKey:@"deviceName"];
95+
resolve(dic);
96+
}
8297
@end

ios/libMiPush/MiPushSDK.h

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
//
2+
// MiPushSDK.h
3+
// MiPushSDK
4+
//
5+
// Created by shen yang on 14-3-6.
6+
// Copyright (c) 2014年 Xiaomi. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <UIKit/UIKit.h>
11+
#import <UserNotifications/UserNotifications.h>
12+
13+
@protocol MiPushSDKDelegate <NSObject>
14+
15+
@optional
16+
17+
/**
18+
* MiPushSDK 请求结果回调
19+
*
20+
* MiPushSDK的所有请求的为异步操作, 用户需监听此方法.
21+
*
22+
* @param
23+
* selector: 请求的方法
24+
* data: 返回结果字典
25+
*/
26+
- (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data;
27+
- (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data;
28+
29+
30+
/**
31+
* 启用长连接后, 当收到消息是就会回调此方法
32+
*
33+
* @param
34+
* type: 消息类型
35+
* data: 返回结果字典, 跟apns的消息格式一样
36+
*/
37+
- (void)miPushReceiveNotification:(NSDictionary*)data;
38+
39+
@end
40+
41+
@interface MiPushSDK : NSObject
42+
43+
/**
44+
* 客户端注册设备
45+
* @param
46+
* delegate: 回调函数
47+
* type: apns推送类型. (Badge, Alert, Sound)
48+
* connect: 是否启动长连接, 它跟APNSs是不同的通道(不管是否启动系统推送, app在前台都可以收到在线或离线消息)
49+
*/
50+
+ (void)registerMiPush:(id<MiPushSDKDelegate, UNUserNotificationCenterDelegate>)delegate;
51+
+ (void)registerMiPush:(id<MiPushSDKDelegate, UNUserNotificationCenterDelegate>)delegate type:(UIRemoteNotificationType)type;
52+
+ (void)registerMiPush:(id<MiPushSDKDelegate, UNUserNotificationCenterDelegate>)delegate type:(UIRemoteNotificationType)type connect:(BOOL)connect;
53+
54+
/**
55+
* 客户端设备注销
56+
*/
57+
+ (void)unregisterMiPush;
58+
59+
60+
/**
61+
* 绑定 PushDeviceToken
62+
*
63+
* NOTE: 有时Apple会重新分配token, 所以为保证消息可达,
64+
* 必须在系统application:didRegisterForRemoteNotificationsWithDeviceToken:回调中,
65+
* 重复调用此方法. SDK内部会处理是否重新上传服务器.
66+
*
67+
* @param
68+
* deviceToken: AppDelegate中,PUSH注册成功后,
69+
* 系统回调didRegisterForRemoteNotificationsWithDeviceToken
70+
*/
71+
+ (void)bindDeviceToken:(NSData *)deviceToken;
72+
73+
/**
74+
* 当同时启动APNs与内部长连接时, 把两处收到的消息合并. 通过miPushReceiveNotification返回
75+
*/
76+
+ (void)handleReceiveRemoteNotification:(NSDictionary*)userInfo;
77+
78+
/**
79+
* 客户端设置别名
80+
*
81+
* @param
82+
* alias: 别名 (length:128)
83+
*/
84+
+ (void)setAlias:(NSString *)alias;
85+
86+
/**
87+
* 客户端取消别名
88+
*
89+
* @param
90+
* alias: 别名 (length:128)
91+
*/
92+
+ (void)unsetAlias:(NSString *)alias;
93+
94+
95+
/**
96+
* 客户端设置帐号
97+
* 多设备设置同一个帐号, 发送消息时多设备可以同时收到
98+
*
99+
* @param
100+
* account: 帐号 (length:128)
101+
*/
102+
+ (void)setAccount:(NSString *)account;
103+
104+
/**
105+
* 客户端取消帐号
106+
*
107+
* @param
108+
* account: 帐号 (length:128)
109+
*/
110+
+ (void)unsetAccount:(NSString *)account;
111+
112+
113+
/**
114+
* 客户端设置主题
115+
* 支持同时设置多个topic, 中间使用","分隔
116+
*
117+
* @param
118+
* subscribe: 主题类型描述
119+
*/
120+
+ (void)subscribe:(NSString *)topics;
121+
122+
/**
123+
* 客户端取消主题
124+
* 支持同时设置多个topic, 中间使用","分隔
125+
*
126+
* @param
127+
* subscribe: 主题类型描述
128+
*/
129+
+ (void)unsubscribe:(NSString *)topics;
130+
131+
132+
/**
133+
* 统计客户端 通过push开启app行为
134+
* 如果, 你想使用服务器帮你统计你app的点击率请自行调用此方法
135+
* 方法放到:application:didReceiveRemoteNotification:回调中.
136+
* @param
137+
* messageId:Payload里面对应的miid参数
138+
*/
139+
+ (void)openAppNotify:(NSString *)messageId;
140+
141+
142+
/**
143+
* NOTE 废弃. 请使用getAllAliasAsync替换
144+
* 获取客户端所有设置的别名
145+
*/
146+
+ (NSArray*)getAllAlias __deprecated;
147+
148+
/**
149+
* 获取客户端所有设置的别名
150+
*/
151+
+ (void)getAllAliasAsync;
152+
153+
/**
154+
* NOTE 废弃. 请使用getAllTopicAsync替换
155+
* 获取客户端所有订阅的主题
156+
*/
157+
+ (NSArray*)getAllTopic __deprecated;
158+
159+
/**
160+
* 获取客户端所有订阅的主题
161+
*/
162+
+ (void)getAllTopicAsync;
163+
164+
+ (void)getAllAccountAsync;
165+
166+
167+
/**
168+
* 获取SDK版本号
169+
*/
170+
+ (NSString*)getSDKVersion;
171+
172+
/**
173+
* 获取RegId
174+
* 如果没有RegId返回nil
175+
*/
176+
+ (NSString*)getRegId;
177+
@end

ios/libMiPush/libMiPushSDK.a

48.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)