Browse Source

Load apikey from manifest file

Wei Fang 5 years ago
parent
commit
12ea24c52f
2 changed files with 10 additions and 11 deletions
  1. 6 1
      android/src/main/java/com/ichong/zzy/mipush/MIPushModule.java
  2. 4 10
      index.js

+ 6 - 1
android/src/main/java/com/ichong/zzy/mipush/MIPushModule.java

@@ -1,5 +1,7 @@
 package com.ichong.zzy.mipush;
 package com.ichong.zzy.mipush;
 
 
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.app.ActivityManager;
 import android.app.ActivityManager;
 import android.content.Context;
 import android.content.Context;
 import android.os.Process;
 import android.os.Process;
@@ -32,12 +34,15 @@ public class MIPushModule extends ReactContextBaseJavaModule {
     }
     }
 
 
     @ReactMethod
     @ReactMethod
-    public void init(String appid, String appkey) {
+    public void init() {
 
 
         try {
         try {
 
 
             if (shouldInit(mContext)) {
             if (shouldInit(mContext)) {
 
 
+                ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA);
+                String appid = info.metaData.getString("MIPUSH_APPID").replace(":", "");
+                String appkey = info.metaData.getString("MIPUSH_APPKEY").replace(":", "");
                 MiPushClient.registerPush(mContext, appid, appkey);
                 MiPushClient.registerPush(mContext, appid, appkey);
             }
             }
 
 

+ 4 - 10
index.js

@@ -26,24 +26,18 @@ class MIPush extends NativeEventEmitter {
         this.state = {};
         this.state = {};
     }
     }
 
 
-    init(appid, appkey) {
-        this.appid = appid;
-        this.appkey = appkey;
-    }
-
-    registerPush() {
-        const { appid, appkey } = this;
+    init() {
         if (Platform.OS == 'android') {
         if (Platform.OS == 'android') {
             PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE).then((state) => {
             PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE).then((state) => {
                 if (state) {
                 if (state) {
                     PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((state) => {
                     PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((state) => {
                         if (state) {
                         if (state) {
 
 
-                            MIPushModule.init(appid, appkey);
+                            MIPushModule.init();
                         } else {
                         } else {
                             PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((granted) => {
                             PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((granted) => {
                                 if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                                 if (granted === PermissionsAndroid.RESULTS.GRANTED) {
-                                    MIPushModule.init(appid, appkey);
+                                    MIPushModule.init();
                                 }
                                 }
                             });
                             });
                         }
                         }
@@ -54,7 +48,7 @@ class MIPush extends NativeEventEmitter {
                         PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
                         PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
                     ]).then((granted) => {
                     ]).then((granted) => {
                         // if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                         // if (granted === PermissionsAndroid.RESULTS.GRANTED) {
-                        MIPushModule.init(appid, appkey);
+                        MIPushModule.init();
                         // }
                         // }
                     });
                     });
                 }
                 }