|
|
@@ -1,12 +1,24 @@
|
|
|
package com.flutter.flutter_aliyun_push;
|
|
|
|
|
|
+import android.app.Application;
|
|
|
+import android.app.NotificationChannel;
|
|
|
+import android.app.NotificationManager;
|
|
|
import android.content.Context;
|
|
|
+import android.content.pm.ApplicationInfo;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.graphics.Color;
|
|
|
+import android.os.Build;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
import com.alibaba.sdk.android.push.CloudPushService;
|
|
|
import com.alibaba.sdk.android.push.CommonCallback;
|
|
|
+import com.alibaba.sdk.android.push.huawei.HuaWeiRegister;
|
|
|
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
|
|
|
+import com.alibaba.sdk.android.push.register.MeizuRegister;
|
|
|
+import com.alibaba.sdk.android.push.register.MiPushRegister;
|
|
|
+import com.alibaba.sdk.android.push.register.OppoRegister;
|
|
|
+import com.alibaba.sdk.android.push.register.VivoRegister;
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONException;
|
|
|
@@ -23,12 +35,18 @@ import io.flutter.plugin.common.StandardMethodCodec;
|
|
|
|
|
|
public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
|
|
|
|
|
|
- private static FlutterAliyunPushPlugin instance;
|
|
|
+ public static FlutterAliyunPushPlugin instance;
|
|
|
public static final String TAG = "AliyunPushPlugin";
|
|
|
public static final String CHANNEL_NAME="aliyun_push";
|
|
|
private Context context;
|
|
|
- private Object initializationLock = new Object();
|
|
|
+ public static Object initializationLock = new Object();
|
|
|
private MethodChannel aliyunPushPluginChannel;
|
|
|
+ private static String lastPushRegistSuccessMessage;
|
|
|
+ private static String lastPushRegistErrorMessage;
|
|
|
+
|
|
|
+ public MethodChannel getAliyunPushPluginChannel() {
|
|
|
+ return aliyunPushPluginChannel;
|
|
|
+ }
|
|
|
|
|
|
public FlutterAliyunPushPlugin() {}
|
|
|
|
|
|
@@ -41,22 +59,125 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
|
|
|
|
|
|
public static void initPush(Context context) {
|
|
|
Log.i(TAG, "start initPush");
|
|
|
+ if (instance == null) {
|
|
|
+ instance = new FlutterAliyunPushPlugin();
|
|
|
+ }
|
|
|
PushServiceFactory.init(context);
|
|
|
+ initPushVersion(context);
|
|
|
CloudPushService pushService = PushServiceFactory.getCloudPushService();
|
|
|
pushService.register(context, new CommonCallback() {
|
|
|
@Override
|
|
|
public void onSuccess(String response) {
|
|
|
Log.d(FlutterAliyunPushPlugin.TAG, "init cloudchannel success");
|
|
|
+ synchronized (initializationLock) {
|
|
|
+ if(instance.getAliyunPushPluginChannel() != null) {
|
|
|
+ instance.getAliyunPushPluginChannel().invokeMethod("onPushRegistSuccess",response);
|
|
|
+ }else {
|
|
|
+ Log.d(FlutterAliyunPushPlugin.TAG, "instance.aliyunPushPluginChannel null");
|
|
|
+ lastPushRegistSuccessMessage = response;
|
|
|
+ lastPushRegistErrorMessage = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
@Override
|
|
|
public void onFailed(String errorCode, String errorMessage) {
|
|
|
Log.d(FlutterAliyunPushPlugin.TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
|
|
|
+ if(instance.getAliyunPushPluginChannel() != null) {
|
|
|
+ instance.getAliyunPushPluginChannel().invokeMethod("onPushRegistError",errorMessage);
|
|
|
+ }else {
|
|
|
+ lastPushRegistErrorMessage = errorMessage;
|
|
|
+ lastPushRegistSuccessMessage = null;
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化厂商推送
|
|
|
+ */
|
|
|
+ public static void initThirdPush(Context context) {
|
|
|
+ ThirdPushConfig config = new ThirdPushConfig();
|
|
|
+
|
|
|
+ ApplicationInfo appInfo = null;
|
|
|
+ try {
|
|
|
+ appInfo = context.getPackageManager()
|
|
|
+ .getApplicationInfo(context.getPackageName(),
|
|
|
+ PackageManager.GET_META_DATA);
|
|
|
+ } catch (PackageManager.NameNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if(appInfo == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ config.miPushAppId = appInfo.metaData.getString("com.mi.push.app_id");
|
|
|
+ config.miPushAppKey = appInfo.metaData.getString("com.mi.push.api_key");
|
|
|
+
|
|
|
+ config.huaweiPushAppId = appInfo.metaData.getString("com.huawei.hms.client.appid");
|
|
|
+
|
|
|
+ config.vivoPushAppId = appInfo.metaData.getString("com.vivo.push.app_id");
|
|
|
+ config.vivoPushAppKey = appInfo.metaData.getString("com.vivo.push.api_key");
|
|
|
+
|
|
|
+ config.oppoPushAppKey = appInfo.metaData.getString("com.oppo.push.api_key");
|
|
|
+ config.oppoPushAppSecret = appInfo.metaData.getString("com.oppo.push.app_secret");
|
|
|
+
|
|
|
+ config.meizhuPushAppId = appInfo.metaData.getString("com.meizhu.push.app_id");
|
|
|
+ config.meizhuPushAppKey = appInfo.metaData.getString("com.meizhu.push.api_key");
|
|
|
|
|
|
+ // 注册方法会自动判断是否支持小米系统推送,如不支持会跳过注册。
|
|
|
+ if(config.miPushAppId != null && config.miPushAppKey != null) {
|
|
|
+ MiPushRegister.register(context, config.miPushAppId, config.miPushAppKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注册方法会自动判断是否支持华为系统推送,如不支持会跳过注册。
|
|
|
+ if(config.huaweiPushAppId != null) {
|
|
|
+ HuaWeiRegister.register((Application) context);
|
|
|
+ }
|
|
|
+
|
|
|
+ // OPPO通道注册
|
|
|
+ if(config.oppoPushAppKey != null && config.oppoPushAppSecret != null) {
|
|
|
+ OppoRegister.register(context, config.oppoPushAppKey, config.oppoPushAppSecret); // appKey/appSecret在OPPO开发者平台获取
|
|
|
+ }
|
|
|
+
|
|
|
+ // 魅族通道注册
|
|
|
+ if(config.meizhuPushAppId != null && config.meizhuPushAppKey != null) {
|
|
|
+ MeizuRegister.register(context, config.meizhuPushAppId, config.meizhuPushAppKey); // appId/appkey在魅族开发者平台获取
|
|
|
+ }
|
|
|
+
|
|
|
+ // VIVO通道注册
|
|
|
+ if(config.vivoPushAppId != null && config.vivoPushAppKey != null) {
|
|
|
+ VivoRegister.register(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void initPushVersion(Context context) {
|
|
|
+// GcmRegister.register(this, "851061211440", "api-8646462459812937352-2848");
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+ // 通知渠道的id
|
|
|
+ String id = "1";
|
|
|
+ // 用户可以看到的通知渠道的名字.
|
|
|
+ CharSequence name = "notification channel";
|
|
|
+ // 用户可以看到的通知渠道的描述
|
|
|
+ String description = "notification description";
|
|
|
+ int importance = NotificationManager.IMPORTANCE_HIGH;
|
|
|
+ NotificationChannel mChannel = new NotificationChannel(id, name, importance);
|
|
|
+ // 配置通知渠道的属性
|
|
|
+ mChannel.setDescription(description);
|
|
|
+ // 设置通知出现时的闪灯(如果 android 设备支持的话)
|
|
|
+ mChannel.enableLights(true);
|
|
|
+ mChannel.setLightColor(Color.RED);
|
|
|
+ // 设置通知出现时的震动(如果 android 设备支持的话)
|
|
|
+ mChannel.enableVibration(true);
|
|
|
+ mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
|
|
|
+ //最后在notificationmanager中创建该通知渠道
|
|
|
+ mNotificationManager.createNotificationChannel(mChannel);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
|
|
|
onAttachedToEngine(binding.getApplicationContext(), binding.getBinaryMessenger());
|
|
|
@@ -87,6 +208,16 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
|
|
|
// Instantiate a new Plugin and connect the primary method channel for
|
|
|
// Android/Flutter communication.
|
|
|
aliyunPushPluginChannel.setMethodCallHandler(this);
|
|
|
+
|
|
|
+ if(lastPushRegistSuccessMessage != null) {
|
|
|
+ Log.i(TAG, "invokeMethod:"+ "onPushRegistSuccess");
|
|
|
+ aliyunPushPluginChannel.invokeMethod("onPushRegistSuccess",lastPushRegistSuccessMessage);
|
|
|
+ lastPushRegistSuccessMessage = null;
|
|
|
+ }else if(lastPushRegistErrorMessage != null) {
|
|
|
+ Log.i(TAG, "invokeMethod:"+ "onPushRegistError");
|
|
|
+ aliyunPushPluginChannel.invokeMethod("onPushRegistError",lastPushRegistErrorMessage);
|
|
|
+ lastPushRegistErrorMessage = null;
|
|
|
+ }
|
|
|
// aliyunPushPluginChannel.invokeMethod("onPushInit",null);
|
|
|
|
|
|
}
|