浏览代码

兼容0.6+

zhangzy 5 年之前
父节点
当前提交
9acc3b9e5b

+ 2 - 1
XMPush.podspec

@@ -13,7 +13,8 @@ Pod::Spec.new do |s|
   s.ios.deployment_target = '9.0'
 
   s.source          = { :git => "https://github.com/a289459798/react-native-mipush", :tag => "v#{s.version}" }
-  s.source_files    = 'ios/*.{h,m}'
+  s.source_files    = 'ios/RCTMIPushModule/RCTMIPushModule/*.{h,m}'
+  s.ios.vendored_libraries = 'ios/RCTMIPushModule/RCTMIPushModule/*.a'
   s.preserve_paths  = "**/*.js"
 
   s.dependency 'React'

+ 10 - 6
android/build.gradle

@@ -1,12 +1,16 @@
 apply plugin: 'com.android.library'
 
+def safeExtGet(prop, fallback) {
+    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
+}
+
 android {
-    compileSdkVersion 23
-    buildToolsVersion "23.0.1"
+    compileSdkVersion safeExtGet('compileSdkVersion', 26)
+    buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')
 
     defaultConfig {
-        minSdkVersion 16
-        targetSdkVersion 23
+        minSdkVersion safeExtGet('minSdkVersion', 26)
+        targetSdkVersion safeExtGet('targetSdkVersion', 26)
         versionCode 1
         versionName "1.0"
 
@@ -26,6 +30,6 @@ repositories {
 }
 
 dependencies {
-    compile fileTree(dir: "libs", include: ["*.jar"])
-    compile 'com.facebook.react:react-native:+'
+    implementation fileTree(dir: "libs", include: ["*.jar"])
+    implementation 'com.facebook.react:react-native:+'
 }

+ 17 - 42
android/src/main/AndroidManifest.xml

@@ -2,52 +2,27 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.ichong.zzy.mipush">
 
-    <uses-permission android:name="android.permission.INTERNET" />
-    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
-    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
-    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
-    <uses-permission android:name="android.permission.GET_TASKS" />
-    <uses-permission android:name="android.permission.VIBRATE"/>
-    <permission android:name="${applicationId}.permission.MIPUSH_RECEIVE" android:protectionLevel="signature" />
-    <!--这里com.xiaomi.mipushdemo改成app的包名-->
-    <uses-permission android:name="${applicationId}.permission.MIPUSH_RECEIVE" />
-    <!--这里com.xiaomi.mipushdemo改成app的包名-->
-
-
     <application>
 
-        <service
-            android:enabled="true"
-            android:process=":pushservice"
-            android:name="com.xiaomi.push.service.XMPushService"/>
-        <service
-            android:name="com.xiaomi.push.service.XMJobService"
-            android:enabled="true"
-            android:exported="false"
-            android:permission="android.permission.BIND_JOB_SERVICE"
-            android:process=":pushservice" />
-        <!--注:此service必须在3.0.1版本以后(包括3.0.1版本)加入-->
-        <service
-            android:enabled="true"
-            android:exported="true"
-            android:name="com.xiaomi.mipush.sdk.PushMessageHandler" />
+        <service android:enabled="true" android:process=":pushservice"
+                 android:name="com.xiaomi.push.service.XMPushService"/>
+        <service android:name="com.xiaomi.push.service.XMJobService" android:enabled="true" android:exported="false"
+                 android:permission="android.permission.BIND_JOB_SERVICE"
+                 android:process=":pushservice"/> <!--注:此service必须在3.0.1版本以后(包括3.0.1版本)加入-->
+        <service android:enabled="true" android:exported="true"
+                 android:name="com.xiaomi.mipush.sdk.PushMessageHandler"/>
         <service android:enabled="true"
-                 android:name="com.xiaomi.mipush.sdk.MessageHandleService" />
-        <!--注:此service必须在2.2.5版本以后(包括2.2.5版本)加入-->
-        <receiver
-            android:exported="true"
-            android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver" >
+                 android:name="com.xiaomi.mipush.sdk.MessageHandleService"/> <!--注:此service必须在2.2.5版本以后(包括2.2.5版本)加入-->
+        <receiver android:exported="true" android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver">
             <intent-filter>
-                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
-                <category android:name="android.intent.category.DEFAULT" />
+                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
+                <category android:name="android.intent.category.DEFAULT"/>
             </intent-filter>
         </receiver>
-        <receiver
-            android:exported="false"
-            android:process=":pushservice"
-            android:name="com.xiaomi.push.service.receivers.PingReceiver" >
+        <receiver android:exported="false" android:process=":pushservice"
+                  android:name="com.xiaomi.push.service.receivers.PingReceiver">
             <intent-filter>
-                <action android:name="com.xiaomi.push.PING_TIMER" />
+                <action android:name="com.xiaomi.push.PING_TIMER"/>
             </intent-filter>
         </receiver>
 
@@ -55,13 +30,13 @@
             android:name=".MIPushReceiver"
             android:exported="true">
             <intent-filter>
-                <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" />
+                <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE"/>
             </intent-filter>
             <intent-filter>
-                <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" />
+                <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED"/>
             </intent-filter>
             <intent-filter>
-                <action android:name="com.xiaomi.mipush.ERROR" />
+                <action android:name="com.xiaomi.mipush.ERROR"/>
             </intent-filter>
         </receiver>
 

+ 70 - 8
android/src/main/java/com/ichong/zzy/mipush/MIPushModule.java

@@ -1,15 +1,29 @@
 package com.ichong.zzy.mipush;
 
+import android.app.ActivityManager;
+import android.content.Context;
+import android.os.Process;
+import android.util.Log;
 import com.facebook.react.bridge.*;
+import com.xiaomi.channel.commonutils.logger.LoggerInterface;
+import com.xiaomi.mipush.sdk.Logger;
 import com.xiaomi.mipush.sdk.MiPushClient;
 
+import java.util.List;
+
+import static com.facebook.react.common.ReactConstants.TAG;
+
 /**
  * Created by zzy on 16/8/11.
  * Date : 16/8/11 13:22
  */
 public class MIPushModule extends ReactContextBaseJavaModule {
+
+    private Context mContext;
+
     public MIPushModule(ReactApplicationContext reactContext) {
         super(reactContext);
+        mContext = getReactApplicationContext();
     }
 
     @Override
@@ -18,39 +32,74 @@ public class MIPushModule extends ReactContextBaseJavaModule {
     }
 
     @ReactMethod
+    public void init(String appid, String appkey) {
+
+        try {
+
+            if (shouldInit(mContext)) {
+
+                MiPushClient.registerPush(mContext, appid, appkey);
+            }
+
+            //打开Log
+            LoggerInterface newLogger = new LoggerInterface() {
+
+                @Override
+                public void setTag(String tag) {
+                    // ignore
+                }
+
+                @Override
+                public void log(String content, Throwable t) {
+                    Log.d(TAG, content, t);
+                }
+
+                @Override
+                public void log(String content) {
+                    Log.d(TAG, content);
+                }
+            };
+            Logger.setLogger(mContext, newLogger);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @ReactMethod
     public void setAlias(String text) {
 
-        MiPushClient.setAlias(getReactApplicationContext(), text, null);
+        MiPushClient.setAlias(mContext, text, null);
     }
 
     @ReactMethod
     public void unsetAlias(String text) {
 
-        MiPushClient.unsetAlias(getReactApplicationContext(), text, null);
+        MiPushClient.unsetAlias(mContext, text, null);
     }
 
     @ReactMethod
     public void subscribe(String text) {
 
-        MiPushClient.subscribe(getReactApplicationContext(), text, null);
+        MiPushClient.subscribe(mContext, text, null);
     }
 
     @ReactMethod
     public void unsubscribe(String text) {
 
-        MiPushClient.unsubscribe(getReactApplicationContext(), text, null);
+        MiPushClient.unsubscribe(mContext, text, null);
     }
 
     @ReactMethod
     public void setAccount(String text) {
 
-        MiPushClient.setUserAccount(getReactApplicationContext(), text, null);
+        MiPushClient.setUserAccount(mContext, text, null);
     }
 
     @ReactMethod
     public void unsetAccount(String text) {
 
-        MiPushClient.setAlias(getReactApplicationContext(), text, null);
+        MiPushClient.setAlias(mContext, text, null);
     }
 
 
@@ -71,13 +120,26 @@ public class MIPushModule extends ReactContextBaseJavaModule {
     @ReactMethod
     public void clearNotification(int id) {
 
-        MiPushClient.clearNotification(getReactApplicationContext(), id);
+        MiPushClient.clearNotification(mContext, id);
     }
 
     @ReactMethod
     public void clearAllNotification() {
 
-        MiPushClient.clearNotification(getReactApplicationContext());
+        MiPushClient.clearNotification(mContext);
+    }
+
+    private boolean shouldInit(Context context) {
+        ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
+        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
+        String mainProcessName = context.getPackageName();
+        int myPid = Process.myPid();
+        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
+            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
+                return true;
+            }
+        }
+        return false;
     }
 
 }

+ 0 - 47
android/src/main/java/com/ichong/zzy/mipush/MIPushPackage.java

@@ -34,40 +34,6 @@ public class MIPushPackage implements ReactPackage {
 
         List<NativeModule> modules = new ArrayList<>();
 
-
-        try {
-            String appId = MIPushHelper.getMIPushValue(reactContext.getApplicationContext(), "MIPUSH_APPID");
-            String appKey = MIPushHelper.getMIPushValue(reactContext.getApplicationContext(), "MIPUSH_APPKEY");
-
-            if (shouldInit(reactContext.getApplicationContext())) {
-
-                MiPushClient.registerPush(reactContext.getApplicationContext(), appId, appKey);
-            }
-
-            //打开Log
-            LoggerInterface newLogger = new LoggerInterface() {
-
-                @Override
-                public void setTag(String tag) {
-                    // ignore
-                }
-
-                @Override
-                public void log(String content, Throwable t) {
-                    Log.d(TAG, content, t);
-                }
-
-                @Override
-                public void log(String content) {
-                    Log.d(TAG, content);
-                }
-            };
-            Logger.setLogger(reactContext, newLogger);
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
         sReactContext = reactContext;
 
         modules.add(new MIPushModule(reactContext));
@@ -80,17 +46,4 @@ public class MIPushPackage implements ReactPackage {
         return Collections.emptyList();
     }
 
-    private boolean shouldInit(Context context) {
-        ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
-        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
-        String mainProcessName = context.getPackageName();
-        int myPid = Process.myPid();
-        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
-            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
 }

+ 39 - 25
index.js

@@ -8,7 +8,8 @@ import {
     NativeModules,
     Platform,
     PushNotificationIOS,
-    NativeEventEmitter
+    NativeEventEmitter,
+    PermissionsAndroid,
 } from 'react-native';
 
 const MIPushModule = NativeModules.MIPushModule;
@@ -19,11 +20,28 @@ const MIPushModule = NativeModules.MIPushModule;
 class MIPush extends NativeEventEmitter {
 
     // 构造
-      constructor(props) {
+    constructor(props) {
         super(MIPushModule);
         // 初始状态
         this.state = {};
-      }
+    }
+
+    init(appid, appkey) {
+
+        if (Platform.OS == 'android') {
+            PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE).then((state) => {
+                if (state) {
+                    MIPushModule.init(appid, appkey);
+                } else {
+                    PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE).then((granted) => {
+                        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
+                            MIPushModule.init(appid, appkey);
+                        }
+                    });
+                }
+            });
+        }
+    }
 
     /**
      * 设置别名
@@ -95,12 +113,12 @@ class MIPush extends NativeEventEmitter {
      */
     addEventListener(type, handler) {
 
-        if(Platform.OS == 'ios') {
+        if (Platform.OS == 'ios') {
 
             switch (type) {
-                case "notification":
-                case "localNotification":
-                case "register":
+                case 'notification':
+                case 'localNotification':
+                case 'register':
                     PushNotificationIOS.addEventListener(type, handler);
                     break;
                 default:
@@ -116,12 +134,12 @@ class MIPush extends NativeEventEmitter {
 
     removeEventListener(type) {
 
-        if(Platform.OS == 'ios') {
+        if (Platform.OS == 'ios') {
 
             switch (type) {
-                case "notification":
-                case "localNotification":
-                case "register":
+                case 'notification':
+                case 'localNotification':
+                case 'register':
                     PushNotificationIOS.removeEventListener(type);
                     break;
                 default:
@@ -141,23 +159,19 @@ class MIPush extends NativeEventEmitter {
      */
     presentLocalNotification(notification) {
 
-        if(Platform.OS == 'ios') {
+        if (Platform.OS == 'ios') {
 
             PushNotificationIOS.presentLocalNotification({
 
                 alertBody: notification.alertBody,
-                alertAction: "查看",
-                category: "push",
-                userInfo: notification.userInfo
+                alertAction: '查看',
+                category: 'push',
+                userInfo: notification.userInfo,
 
             });
         } else {
 
-            MIPushModule.presentLocalNotification({
-
-
-
-            });
+            MIPushModule.presentLocalNotification({});
         }
     }
 
@@ -169,7 +183,7 @@ class MIPush extends NativeEventEmitter {
      */
     clearNotification(notifyId) {
 
-        if(Platform.OS == 'ios') {
+        if (Platform.OS == 'ios') {
 
             PushNotificationIOS.cancelLocalNotifications(notifyId);
         } else {
@@ -184,7 +198,7 @@ class MIPush extends NativeEventEmitter {
      */
     clearNotifications() {
 
-        if(Platform.OS == 'ios') {
+        if (Platform.OS == 'ios') {
 
             PushNotificationIOS.cancelAllLocalNotifications();
         } else {
@@ -199,7 +213,7 @@ class MIPush extends NativeEventEmitter {
      */
     setBadgeNumber(num) {
 
-        if(Platform.OS == 'ios') {
+        if (Platform.OS == 'ios') {
 
             PushNotificationIOS.setApplicationIconBadgeNumber(num);
         }
@@ -212,7 +226,7 @@ class MIPush extends NativeEventEmitter {
      */
     getInitialNotification(handler) {
 
-        if(Platform.OS == 'ios') {
+        if (Platform.OS == 'ios') {
 
             PushNotificationIOS.getInitialNotification()
                 .then(handler);
@@ -227,4 +241,4 @@ class MIPush extends NativeEventEmitter {
 
 MIPush = new MIPush();
 
-module.exports = MIPush;
+module.exports = MIPush;

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "react-native-xmpush",
-  "version": "1.0.1",
+  "version": "2.0.0-beta",
   "description": "集成小米推送",
   "main": "index.js",
   "scripts": {