瀏覽代碼

rn集成小米推送-android端

a289459798 8 年之前
當前提交
d6e8ca4740

+ 9 - 0
.gitignore

@@ -0,0 +1,9 @@
+# Created by .ignore support plugin (hsz.mobi)
+*.gradle/
+gradle.properties
+build/
+build-res/
+/*/build/
+*.iml
+*.idea/
+*.DS_Store

+ 35 - 0
.npmignore

@@ -0,0 +1,35 @@
+# Created by .ignore support plugin (hsz.mobi)
+# OSX
+#
+.DS_Store
+
+# Xcode
+#
+build/
+*.pbxuser
+!default.pbxuser
+*.mode1v3
+!default.mode1v3
+*.mode2v3
+!default.mode2v3
+*.perspectivev3
+!default.perspectivev3
+xcuserdata
+*.xccheckout
+*.moved-aside
+DerivedData
+*.hmap
+*.ipa
+*.xcuserstate
+
+# node.js
+#
+node_modules/
+npm-debug.log
+
+# Don't publish example apps
+example/
+# .idea
+
+.idea/
+.gradle/

+ 31 - 0
android/build.gradle

@@ -0,0 +1,31 @@
+apply plugin: 'com.android.library'
+
+android {
+    compileSdkVersion 23
+    buildToolsVersion "23.0.1"
+
+    defaultConfig {
+        minSdkVersion 16
+        targetSdkVersion 23
+        versionCode 1
+        versionName "1.0"
+
+    }
+
+    lintOptions {
+        abortOnError false
+        warning 'InvalidPackage'
+    }
+}
+
+repositories {
+    maven {
+        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
+        url "$rootDir/../node_modules/react-native/android"
+    }
+}
+
+dependencies {
+    compile fileTree(dir: "libs", include: ["*.jar"])
+    compile 'com.facebook.react:react-native:+'
+}

二進制
android/gradle/wrapper/gradle-wrapper.jar


+ 6 - 0
android/gradle/wrapper/gradle-wrapper.properties

@@ -0,0 +1,6 @@
+#Mon Dec 28 10:00:20 PST 2015
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

+ 160 - 0
android/gradlew

@@ -0,0 +1,160 @@
+#!/usr/bin/env bash
+
+##############################################################################
+##
+##  Gradle start up script for UN*X
+##
+##############################################################################
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn ( ) {
+    echo "$*"
+}
+
+die ( ) {
+    echo
+    echo "$*"
+    echo
+    exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+case "`uname`" in
+  CYGWIN* )
+    cygwin=true
+    ;;
+  Darwin* )
+    darwin=true
+    ;;
+  MINGW* )
+    msys=true
+    ;;
+esac
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+    ls=`ls -ld "$PRG"`
+    link=`expr "$ls" : '.*-> \(.*\)$'`
+    if expr "$link" : '/.*' > /dev/null; then
+        PRG="$link"
+    else
+        PRG=`dirname "$PRG"`"/$link"
+    fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >/dev/null
+APP_HOME="`pwd -P`"
+cd "$SAVED" >/dev/null
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+        # IBM's JDK on AIX uses strange locations for the executables
+        JAVACMD="$JAVA_HOME/jre/sh/java"
+    else
+        JAVACMD="$JAVA_HOME/bin/java"
+    fi
+    if [ ! -x "$JAVACMD" ] ; then
+        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+    fi
+else
+    JAVACMD="java"
+    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
+    MAX_FD_LIMIT=`ulimit -H -n`
+    if [ $? -eq 0 ] ; then
+        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+            MAX_FD="$MAX_FD_LIMIT"
+        fi
+        ulimit -n $MAX_FD
+        if [ $? -ne 0 ] ; then
+            warn "Could not set maximum file descriptor limit: $MAX_FD"
+        fi
+    else
+        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+    fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+    JAVACMD=`cygpath --unix "$JAVACMD"`
+
+    # We build the pattern for arguments to be converted via cygpath
+    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+    SEP=""
+    for dir in $ROOTDIRSRAW ; do
+        ROOTDIRS="$ROOTDIRS$SEP$dir"
+        SEP="|"
+    done
+    OURCYGPATTERN="(^($ROOTDIRS))"
+    # Add a user-defined pattern to the cygpath arguments
+    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+    fi
+    # Now convert the arguments - kludge to limit ourselves to /bin/sh
+    i=0
+    for arg in "$@" ; do
+        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option
+
+        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
+            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+        else
+            eval `echo args$i`="\"$arg\""
+        fi
+        i=$((i+1))
+    done
+    case $i in
+        (0) set -- ;;
+        (1) set -- "$args0" ;;
+        (2) set -- "$args0" "$args1" ;;
+        (3) set -- "$args0" "$args1" "$args2" ;;
+        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+    esac
+fi
+
+# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
+function splitJvmOpts() {
+    JVM_OPTS=("$@")
+}
+eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
+JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
+
+exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"

+ 90 - 0
android/gradlew.bat

@@ -0,0 +1,90 @@
+@if "%DEBUG%" == "" @echo off
+@rem ##########################################################################
+@rem
+@rem  Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS=
+
+set DIRNAME=%~dp0
+if "%DIRNAME%" == "" set DIRNAME=.
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if "%ERRORLEVEL%" == "0" goto init
+
+echo.
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto init
+
+echo.
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
+echo.
+echo Please set the JAVA_HOME variable in your environment to match the
+echo location of your Java installation.
+
+goto fail
+
+:init
+@rem Get command-line arguments, handling Windowz variants
+
+if not "%OS%" == "Windows_NT" goto win9xME_args
+if "%@eval[2+2]" == "4" goto 4NT_args
+
+:win9xME_args
+@rem Slurp the command line arguments.
+set CMD_LINE_ARGS=
+set _SKIP=2
+
+:win9xME_args_slurp
+if "x%~1" == "x" goto execute
+
+set CMD_LINE_ARGS=%*
+goto execute
+
+:4NT_args
+@rem Get arguments from the 4NT Shell from JP Software
+set CMD_LINE_ARGS=%$
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
+
+:end
+@rem End local scope for the variables with windows NT shell
+if "%ERRORLEVEL%"=="0" goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+if  not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
+exit /b 1
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega

二進制
android/libs/MiPush_SDK_Client_3_0_3.jar


+ 70 - 0
android/src/main/AndroidManifest.xml

@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="utf-8"?>
+<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: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" />
+            </intent-filter>
+        </receiver>
+        <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" />
+            </intent-filter>
+        </receiver>
+
+        <receiver
+            android:name=".MIPushReceiver"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="com.xiaomi.mipush.ERROR" />
+            </intent-filter>
+        </receiver>
+
+    </application>
+
+</manifest>

+ 59 - 0
android/src/main/java/com/ichong/zzy/mipush/MIPushHelper.java

@@ -0,0 +1,59 @@
+package com.ichong.zzy.mipush;
+
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.text.TextUtils;
+import com.facebook.react.bridge.Arguments;
+import com.facebook.react.bridge.WritableMap;
+import com.xiaomi.mipush.sdk.MiPushMessage;
+
+/**
+ * Created by zzy on 16/8/13.
+ * Date : 16/8/13 16:18
+ */
+public class MIPushHelper {
+
+    public static WritableMap parsePushMessage(MiPushMessage miPushMessage) {
+
+        if (miPushMessage == null) {
+
+            return null;
+        }
+        try {
+
+            WritableMap param = Arguments.createMap();
+            param.putString("title", miPushMessage.getTitle());
+            param.putString("description", miPushMessage.getDescription());
+            param.putString("content", miPushMessage.getContent());
+//        param.putMap("extra", (WritableMap) miPushMessage.getExtra());
+            param.putString("category", miPushMessage.getCategory());
+            param.putInt("notifyId", miPushMessage.getNotifyId());
+            param.putInt("notifyType", miPushMessage.getNotifyType());
+            return param;
+        } catch (Exception e) {
+
+            return null;
+        }
+
+    }
+
+
+    public static String getMIPushValue(Context context, String key) throws Exception {
+        if (context == null) {
+            return "";
+        }
+        if (TextUtils.isEmpty(key)) {
+            return "";
+        }
+
+        ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
+
+        if (appInfo != null && appInfo.metaData != null && appInfo.metaData.containsKey(key)) {
+
+            return appInfo.metaData.getString(key).replace(":", "");
+        } else {
+            return "";
+        }
+    }
+}

+ 83 - 0
android/src/main/java/com/ichong/zzy/mipush/MIPushModule.java

@@ -0,0 +1,83 @@
+package com.ichong.zzy.mipush;
+
+import com.facebook.react.bridge.*;
+import com.xiaomi.mipush.sdk.MiPushClient;
+
+/**
+ * Created by zzy on 16/8/11.
+ * Date : 16/8/11 13:22
+ */
+public class MIPushModule extends ReactContextBaseJavaModule {
+    public MIPushModule(ReactApplicationContext reactContext) {
+        super(reactContext);
+    }
+
+    @Override
+    public String getName() {
+        return "MIPushModule";
+    }
+
+    @ReactMethod
+    public void setAlias(String text) {
+
+        MiPushClient.setAlias(getReactApplicationContext(), text, null);
+    }
+
+    @ReactMethod
+    public void unsetAlias(String text) {
+
+        MiPushClient.unsetAlias(getReactApplicationContext(), text, null);
+    }
+
+    @ReactMethod
+    public void subscribe(String text) {
+
+        MiPushClient.subscribe(getReactApplicationContext(), text, null);
+    }
+
+    @ReactMethod
+    public void unsubscribe(String text) {
+
+        MiPushClient.unsubscribe(getReactApplicationContext(), text, null);
+    }
+
+    @ReactMethod
+    public void setAccount(String text) {
+
+        MiPushClient.setUserAccount(getReactApplicationContext(), text, null);
+    }
+
+    @ReactMethod
+    public void unsetAccount(String text) {
+
+        MiPushClient.setAlias(getReactApplicationContext(), text, null);
+    }
+
+
+    @ReactMethod
+    public void presentLocalNotification(ReadableMap notification) {
+
+    }
+
+    @ReactMethod
+    public void getInitialNotification(
+        Promise promise) {
+
+        promise.resolve(MIPushHelper.parsePushMessage(MIPushPackage.sMiPushMessage));
+
+        MIPushPackage.sMiPushMessage = null;
+    }
+
+    @ReactMethod
+    public void clearNotification(int id) {
+
+        MiPushClient.clearNotification(getReactApplicationContext(), id);
+    }
+
+    @ReactMethod
+    public void clearAllNotification() {
+
+        MiPushClient.clearNotification(getReactApplicationContext());
+    }
+
+}

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

@@ -0,0 +1,104 @@
+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.ReactPackage;
+import com.facebook.react.bridge.JavaScriptModule;
+import com.facebook.react.bridge.NativeModule;
+import com.facebook.react.bridge.ReactApplicationContext;
+import com.facebook.react.uimanager.ViewManager;
+import com.xiaomi.channel.commonutils.logger.LoggerInterface;
+import com.xiaomi.mipush.sdk.Logger;
+import com.xiaomi.mipush.sdk.MiPushClient;
+import com.xiaomi.mipush.sdk.MiPushMessage;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static com.facebook.react.common.ReactConstants.TAG;
+
+/**
+ * Created by zzy on 16/8/11.
+ * Date : 16/8/11 13:36
+ */
+public class MIPushPackage implements ReactPackage {
+
+    public static ReactApplicationContext sReactContext;
+    public static MiPushMessage sMiPushMessage;
+
+
+
+
+    @Override
+    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
+
+        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));
+
+        return modules;
+    }
+
+    @Override
+    public List<Class<? extends JavaScriptModule>> createJSModules() {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
+        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;
+    }
+
+}

+ 72 - 0
android/src/main/java/com/ichong/zzy/mipush/MIPushReceiver.java

@@ -0,0 +1,72 @@
+package com.ichong.zzy.mipush;
+
+import android.content.Context;
+import android.content.Intent;
+import com.facebook.react.modules.core.DeviceEventManagerModule;
+import com.xiaomi.mipush.sdk.MiPushMessage;
+import com.xiaomi.mipush.sdk.PushMessageReceiver;
+
+/**
+ * Created by zzy on 16/8/13.
+ * Date : 16/8/13 12:11
+ */
+public class MIPushReceiver extends PushMessageReceiver {
+
+    /**
+     * 接受服务端发送过来的透传消息
+     * @param context
+     * @param miPushMessage
+     */
+    @Override
+    public void onReceivePassThroughMessage(Context context, MiPushMessage miPushMessage) {
+        super.onReceivePassThroughMessage(context, miPushMessage);
+
+        sendListener("xmpush_message", miPushMessage);
+    }
+
+    /**
+     * 监听用户点击通知栏消息
+     * @param context
+     * @param miPushMessage
+     */
+    @Override
+    public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
+        super.onNotificationMessageClicked(context, miPushMessage);
+
+        Intent intent = new Intent();
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        if(MIPushPackage.sReactContext == null) {
+            // 将数据保存
+            MIPushPackage.sMiPushMessage = miPushMessage;
+
+            String packageName = context.getApplicationContext().getPackageName();
+            Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
+            launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
+            context.startActivity(launchIntent);
+            return;
+        }
+        sendListener("xmpush_click", miPushMessage);
+
+        intent.setClass(context, MIPushPackage.sReactContext.getCurrentActivity().getClass());
+        context.startActivity(intent);
+
+    }
+
+    /**
+     * 消息达到客户端触发
+     * @param context
+     * @param miPushMessage
+     */
+    @Override
+    public void onNotificationMessageArrived(Context context, MiPushMessage miPushMessage) {
+        super.onNotificationMessageArrived(context, miPushMessage);
+
+        sendListener("xmpush_notify", miPushMessage);
+    }
+
+    private void sendListener(String type, MiPushMessage miPushMessage) {
+
+        MIPushPackage.sReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(type, MIPushHelper.parsePushMessage(miPushMessage));
+    }
+
+}

+ 210 - 0
index.js

@@ -0,0 +1,210 @@
+/**
+ * Created by zhangzy on 16/7/27.
+ */
+
+'use strict';
+
+import {
+    NativeModules,
+    Platform,
+    PushNotificationIOS
+} from 'react-native';
+
+const NativeEventEmitter = require('NativeEventEmitter');
+var MIPushModule = NativeModules.MIPushModule;
+
+/**
+ * 获取app的版本名称\版本号和渠道名
+ */
+class MIPush extends NativeEventEmitter {
+
+    // 构造
+      constructor(props) {
+        super(MIPushModule);
+        // 初始状态
+        this.state = {};
+      }
+
+    /**
+     * 设置别名
+     * @param text
+     */
+    setAlias(text) {
+
+        MIPushModule.setAlias(text);
+    }
+
+    /**
+     * 注销别名
+     * @param text
+     */
+    unsetAlias(text) {
+
+        MIPushModule.unsetAlias(text);
+    }
+
+    /**
+     * 设置主题,类似tag
+     * @param text
+     */
+    subscribe(text) {
+
+        MIPushModule.subscribe(text);
+    }
+
+    /**
+     * 注销主题
+     * @param text
+     */
+    unsubscribe(text) {
+
+        MIPushModule.unsubscribe(text);
+    }
+
+    /**
+     * 设置账号,一个账号需要多台设备接收通知
+     * @param text
+     */
+    setAccount(text) {
+
+        MIPushModule.setAccount(text);
+    }
+
+    /**
+     * 注销账号
+     * @param text
+     */
+    unsetAccount(text) {
+
+        MIPushModule.unsetAccount(text);
+    }
+
+    /**
+     *
+     * @param type
+     * ios :
+     * notification => 监听收到apns通知
+     * localNotification => 监听收到本地通知
+     * register => 注册deviceToken 通知
+     *
+     * android :
+     * xmpush_notify => 监听收到推送
+     * xmpush_click => 监听推送被点击
+     * xmpush_message => 监听收到透传消息
+     * @param handler
+     */
+    addEventListener(type, handler) {
+
+        if(Platform.OS == 'ios') {
+
+            PushNotificationIOS.addEventListener(type, handler);
+        } else {
+
+            this.addListener(type, handler);
+        }
+    }
+
+    removeEventListener(type) {
+
+        if(Platform.OS == 'ios') {
+
+            PushNotificationIOS.removeEventListener(type);
+        } else {
+
+            this.removeEventListener(type);
+        }
+    }
+
+    /**
+     * 发送一个本地通知
+     * @param notification
+     */
+    presentLocalNotification(notification) {
+
+        if(Platform.OS == 'ios') {
+
+            PushNotificationIOS.presentLocalNotification({
+
+                alertBody: notification.alertBody,
+                alertAction: "查看",
+                category: "push",
+                userInfo: notification.userInfo
+
+            });
+        } else {
+
+            MIPushModule.presentLocalNotification({
+
+
+
+            });
+        }
+    }
+
+    /**
+     * 清除指定通知
+     * @param notifyId
+     * ios : userInfo
+     * android : id
+     */
+    clearNotification(notifyId) {
+
+        if(Platform.OS == 'ios') {
+
+            PushNotificationIOS.cancelLocalNotifications(notifyId);
+        } else {
+
+            MIPushModule.clearNotification(notifyId);
+        }
+
+    }
+
+    /**
+     * 清除所有通知
+     */
+    clearNotifications() {
+
+        if(Platform.OS == 'ios') {
+
+            PushNotificationIOS.cancelAllLocalNotifications();
+        } else {
+
+            MIPushModule.clearAllNotification();
+        }
+    }
+
+    /**
+     * 设置角标,仅支持ios
+     * @param num
+     */
+    setBadgeNumber(num) {
+
+        if(Platform.OS == 'ios') {
+
+            PushNotificationIOS.setApplicationIconBadgeNumber(num);
+        }
+
+    }
+
+    /**
+     * 通过点击通知启动app
+     * @param handler
+     */
+    getInitialNotification(handler) {
+
+        if(Platform.OS == 'ios') {
+
+            PushNotificationIOS.getInitialNotification()
+                .then(handler);
+        } else {
+
+            MIPushModule.getInitialNotification()
+                .then(handler);
+        }
+    }
+
+}
+
+MIPush = new MIPush();
+
+module.exports = MIPush;

+ 23 - 0
package.json

@@ -0,0 +1,23 @@
+{
+  "name": "react-native-mipush",
+  "version": "0.0.1",
+  "description": "集成小米推送",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/a289459798/react-native-mipush.git"
+  },
+  "keywords": [
+    "react-native",
+    "mipush"
+  ],
+  "author": "zzy",
+  "license": "ISC",
+  "bugs": {
+    "url": "https://github.com/a289459798/react-native-mipush/issues"
+  },
+  "homepage": "https://github.com/a289459798/react-native-mipush#readme"
+}