Răsfoiți Sursa

添加对fcm的支持

imhao183 4 ani în urmă
părinte
comite
4127c885a9

+ 1 - 0
build.gradle

@@ -18,6 +18,7 @@ buildscript {
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
+        classpath 'com.google.gms:google-services:4.3.5'
     }
 }
 

+ 0 - 15
client/src/main/java/cn/wildfirechat/PushType.java

@@ -1,15 +0,0 @@
-/*
- * Copyright (c) 2020 WildFireChat. All rights reserved.
- */
-
-package cn.wildfirechat;
-
-public interface PushType {
-    int XIAOMI = 1;
-    int HMS = 2;
-    int MEIZU = 3;
-    int VIVO = 4;
-    int OPPO = 5;
-
-    // 如果需要集成其他推送方式,请勿和上面的冲突
-}

+ 3 - 1
push/build.gradle

@@ -1,5 +1,5 @@
 apply plugin: 'com.android.library'
-
+apply plugin: 'com.google.gms.google-services'
 android {
     compileSdkVersion 28
 
@@ -51,6 +51,8 @@ dependencies {
     implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
     implementation project(':client')
     implementation 'com.meizu.flyme.internet:push-internal:3.4.2@aar'
+    implementation platform('com.google.firebase:firebase-bom:26.6.0')
+    implementation 'com.google.firebase:firebase-messaging:20.1.6'
 
     def lifecycle_version = '2.2.0'
     implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

+ 16 - 0
push/src/main/AndroidManifest.xml

@@ -182,6 +182,22 @@
             android:name="OPPO_APP_PUSH_SECRET"
             android:value="${OPPO_APP_SECRET}" />
 
+
+        <!--        Google  -->
+        <service
+            android:name="cn.wildfirechat.push.firebase.MyFirebaseMessagingService"
+            android:exported="false">
+            <intent-filter>
+                <action android:name="com.google.firebase.MESSAGING_EVENT" />
+            </intent-filter>
+        </service>
+        <meta-data
+            android:name="firebase_messaging_auto_init_enabled"
+            android:value="false" />
+        <meta-data
+            android:name="firebase_analytics_collection_enabled"
+            android:value="false" />
+
     </application>
 
 </manifest>

+ 84 - 8
push/src/main/java/cn/wildfirechat/push/PushService.java

@@ -12,14 +12,22 @@ import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.os.Build;
 import android.os.Environment;
+import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
+import android.widget.Toast;
 
+import androidx.annotation.NonNull;
 import androidx.lifecycle.Lifecycle;
 import androidx.lifecycle.LifecycleObserver;
 import androidx.lifecycle.OnLifecycleEvent;
 import androidx.lifecycle.ProcessLifecycleOwner;
 
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GoogleApiAvailability;
+import com.google.android.gms.tasks.OnCompleteListener;
+import com.google.android.gms.tasks.Task;
+import com.google.firebase.messaging.FirebaseMessaging;
 import com.heytap.mcssdk.PushManager;
 import com.heytap.mcssdk.callback.PushCallback;
 import com.heytap.mcssdk.mode.SubscribeResult;
@@ -39,11 +47,14 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.lang.reflect.Method;
 import java.util.List;
+import java.util.Locale;
 import java.util.Properties;
+import java.util.TimeZone;
 
-import cn.wildfirechat.PushType;
 import cn.wildfirechat.remote.ChatManager;
 
+import static com.google.firebase.messaging.Constants.MessageNotificationKeys.TAG;
+
 /**
  * Created by heavyrain.lee on 2018/2/26.
  */
@@ -56,7 +67,7 @@ public class PushService {
     private static String applicationId;
 
     public enum PushServiceType {
-        Unknown, Xiaomi, HMS, MeiZu, VIVO, OPPO
+        Unknown, Xiaomi, HMS, MeiZu, VIVO, OPPO, Google
     }
 
     public static void init(Application gContext, String applicationId) {
@@ -74,8 +85,14 @@ public class PushService {
         } else if (PushManager.isSupportPush(gContext)) {
             INST.pushServiceType = PushServiceType.OPPO;
             INST.initOPPO(gContext);
-        } else /*if (SYS_MIUI.equals(sys) && INST.isXiaomiConfigured(gContext))*/ {
-            //MIUI或其它使用小米推送
+        } else if (SYS_MIUI.equals(sys) && INST.isXiaomiConfigured(gContext)) {
+            INST.pushServiceType = PushServiceType.Xiaomi;
+            INST.initXiaomi(gContext);
+        } else if(useGoogleFCM(gContext)) {
+            INST.pushServiceType = PushServiceType.Google;
+            INST.initFCM(gContext);
+        } else {
+            //其它使用小米推送
             INST.pushServiceType = PushServiceType.Xiaomi;
             INST.initXiaomi(gContext);
         }
@@ -93,6 +110,33 @@ public class PushService {
 
     }
 
+    private static boolean useGoogleFCM(Context context) {
+        if(hasGooglePlayServices(context)) { //判断是否支持google服务框架
+            TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
+            String no = tm.getNetworkOperator();
+            String simNo = tm.getSimOperator();
+
+            if(!TextUtils.isEmpty(no) && TextUtils.isEmpty(simNo)) {
+                if(no.startsWith("460") && simNo.startsWith("460")) {
+                    //当sim卡和网络都是大陆运营商时,不能使用fcm
+                    return false;
+                } else {
+                    //当sim卡和网络有一个不是大陆运营商时,可以使用fcm
+                    return true;
+                }
+            }
+
+            //区域是中国大陆简体中文且是中国标准时区,不用fcm
+            Locale locale = context.getResources().getConfiguration().locale;
+            if("zh".equals(locale.getLanguage()) && "CN".equals(locale.getCountry()) && "Asia/Shanghai".equals(TimeZone.getDefault().getID())) {
+                return false;
+            }
+
+            return true;
+        }
+        return false;
+    }
+
     private static void clearNotification(Context context) {
         if (INST.pushServiceType == PushServiceType.Xiaomi) {
             MiPushClient.clearNotification(context);
@@ -180,7 +224,7 @@ public class PushService {
                 try {
                     String token = HmsInstanceId.getInstance(context).getToken(appId, "HCM");
                     if(!TextUtils.isEmpty(token)){
-                        ChatManager.Instance().setDeviceToken(token, PushType.HMS);
+                        ChatManager.Instance().setDeviceToken(token, PushServiceType.HMS.ordinal());
                     }
                 } catch (ApiException e) {
                     e.printStackTrace();
@@ -215,7 +259,7 @@ public class PushService {
                     String pushId = com.meizu.cloud.pushsdk.PushManager.getPushId(context);
                     com.meizu.cloud.pushsdk.PushManager.register(context, String.valueOf(appId), appKey);
                     com.meizu.cloud.pushsdk.PushManager.switchPush(context, String.valueOf(appId), appKey, pushId, 1, true);
-                    ChatManager.Instance().setDeviceToken(pushId, PushType.MEIZU);
+                    ChatManager.Instance().setDeviceToken(pushId, PushServiceType.MeiZu.ordinal());
                 }
             }
         } catch (Exception e) {
@@ -235,7 +279,7 @@ public class PushService {
                 Log.d("PushService", "vivo turnOnPush " + state);
                 String regId = PushClient.getInstance(context).getRegId();
                 if (!TextUtils.isEmpty(regId)) {
-                    ChatManager.Instance().setDeviceToken(regId, PushType.VIVO);
+                    ChatManager.Instance().setDeviceToken(regId, PushServiceType.VIVO.ordinal());
                 }
             }
         });
@@ -251,7 +295,7 @@ public class PushService {
                 PushManager.getInstance().register(context, appkey, appsecret, new PushCallback() {
                     @Override
                     public void onRegister(int i, String s) {
-                        ChatManager.Instance().setDeviceToken(s, PushType.OPPO);
+                        ChatManager.Instance().setDeviceToken(s, PushServiceType.OPPO.ordinal());
                     }
 
                     @Override
@@ -325,6 +369,38 @@ public class PushService {
         }
     }
 
+    private void initFCM(Context context) {
+        FirebaseMessaging.getInstance().setAutoInitEnabled(true);
+        FirebaseMessaging.getInstance().getToken()
+                .addOnCompleteListener(new OnCompleteListener<String>() {
+                    @Override
+                    public void onComplete(@NonNull Task<String> task) {
+                        if (!task.isSuccessful()) {
+                            Log.w(TAG, "Fetching FCM registration token failed", task.getException());
+                            return;
+                        }
+
+                        // Get new FCM registration token
+                        String token = task.getResult();
+
+                        // Log and toast
+                        String msg = token;
+                        Log.d(TAG, msg);
+//                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
+                        ChatManager.Instance().setDeviceToken(token, PushServiceType.Google.ordinal());
+                    }
+                });
+    }
+
+    /**
+     * 检查 Google Play 服务
+     */
+    private static boolean hasGooglePlayServices(Context context) {
+        // 验证是否已在此设备上安装并启用Google Play服务,以及此设备上安装的旧版本是否为此客户端所需的版本
+        return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS;
+    }
+
+
 
     public static final String SYS_EMUI = "sys_emui";
     public static final String SYS_MIUI = "sys_miui";

+ 29 - 0
push/src/main/java/cn/wildfirechat/push/firebase/MyFirebaseMessagingService.java

@@ -0,0 +1,29 @@
+package cn.wildfirechat.push.firebase;
+
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+
+import com.google.firebase.messaging.FirebaseMessagingService;
+import com.google.firebase.messaging.RemoteMessage;
+
+import org.json.JSONException;
+
+import cn.wildfirechat.push.AndroidPushMessage;
+import cn.wildfirechat.push.PushService;
+import cn.wildfirechat.remote.ChatManager;
+
+public class MyFirebaseMessagingService extends FirebaseMessagingService {
+    @Override
+    public void onNewToken(@NonNull String s) {
+        super.onNewToken(s);
+        ChatManager.Instance().setDeviceToken(s, PushService.PushServiceType.Google.ordinal());
+    }
+
+    @Override
+    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
+        Log.d("FCM", "receiveMessage");
+        // do nothing
+        // 只需将主进程拉起即可,主进程会去拉取消息,并显示通知
+    }
+}

+ 2 - 2
push/src/main/java/cn/wildfirechat/push/huawei/HwPushService.java

@@ -9,7 +9,7 @@ import android.util.Log;
 import com.huawei.hms.push.HmsMessageService;
 import com.huawei.hms.push.RemoteMessage;
 
-import cn.wildfirechat.PushType;
+import cn.wildfirechat.push.PushService;
 import cn.wildfirechat.remote.ChatManager;
 
 public class HwPushService extends HmsMessageService {
@@ -17,7 +17,7 @@ public class HwPushService extends HmsMessageService {
     public void onNewToken(String s) {
         super.onNewToken(s);
         Log.d("HMS", "onNewToken: " + s);
-        ChatManager.Instance().setDeviceToken(s, PushType.HMS);
+        ChatManager.Instance().setDeviceToken(s, PushService.PushServiceType.HMS.ordinal());
     }
 
     @Override

+ 1 - 2
push/src/main/java/cn/wildfirechat/push/meizu/MeizuPushReceiver.java

@@ -27,7 +27,6 @@ import cn.wildfirechat.client.NotInitializedExecption;
 import cn.wildfirechat.push.AndroidPushMessage;
 import cn.wildfirechat.push.PushService;
 import cn.wildfirechat.remote.ChatManager;
-import cn.wildfirechat.PushType;
 
 public class MeizuPushReceiver extends MzPushMessageReceiver {
 
@@ -35,7 +34,7 @@ public class MeizuPushReceiver extends MzPushMessageReceiver {
     public void onRegister(Context context, String pushId) {
         Log.e(TAG, "onReceiveClientId -> " + "pushId = " + pushId);
         try {
-            ChatManager.Instance().setDeviceToken(pushId, PushType.MEIZU);
+            ChatManager.Instance().setDeviceToken(pushId, PushService.PushServiceType.MeiZu.ordinal());
         } catch (NotInitializedExecption notInitializedExecption) {
             notInitializedExecption.printStackTrace();
         }

+ 1 - 2
push/src/main/java/cn/wildfirechat/push/vivo/PushMessageReceiverImpl.java

@@ -12,7 +12,6 @@ import com.vivo.push.sdk.OpenClientPushMessageReceiver;
 
 import cn.wildfirechat.push.PushService;
 import cn.wildfirechat.remote.ChatManager;
-import cn.wildfirechat.PushType;
 
 public class PushMessageReceiverImpl extends OpenClientPushMessageReceiver {
     /**
@@ -29,6 +28,6 @@ public class PushMessageReceiverImpl extends OpenClientPushMessageReceiver {
     public void onReceiveRegId(Context context, String regId) {
         String responseString = "onReceiveRegId regId = " + regId;
         Log.d(TAG, responseString);
-        ChatManager.Instance().setDeviceToken(regId, PushType.VIVO);
+        ChatManager.Instance().setDeviceToken(regId, PushService.PushServiceType.VIVO.ordinal());
     }
 }

+ 1 - 2
push/src/main/java/cn/wildfirechat/push/xiaomi/MiMessageReceiver.java

@@ -21,7 +21,6 @@ import cn.wildfirechat.client.NotInitializedExecption;
 import cn.wildfirechat.push.AndroidPushMessage;
 import cn.wildfirechat.push.PushService;
 import cn.wildfirechat.remote.ChatManager;
-import cn.wildfirechat.PushType;
 
 /**
  * Created by heavyrainlee on 22/02/2018.
@@ -120,7 +119,7 @@ public class MiMessageReceiver extends PushMessageReceiver {
             if (message.getResultCode() == ErrorCode.SUCCESS) {
                 mRegId = cmdArg1;
                 try {
-                    ChatManager.Instance().setDeviceToken(mRegId, PushType.XIAOMI);
+                    ChatManager.Instance().setDeviceToken(mRegId, PushService.PushServiceType.Xiaomi.ordinal());
                 } catch (NotInitializedExecption notInitializedExecption) {
                     notInitializedExecption.printStackTrace();
                 }