Pārlūkot izejas kodu

安卓厂商推送小米测试

安卓厂商推送小米测试
linbing 5 gadi atpakaļ
vecāks
revīzija
27277db64b

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

@@ -31,6 +31,16 @@
 
 
         <!-- 自定义,统一初始化 -->
+        <meta-data
+            android:name="com.flutter.push.channelId"
+            android:value="${pushChannelId}" />
+        <meta-data
+            android:name="com.flutter.push.channelName"
+            android:value="${pushChannelName}" />
+        <meta-data
+            android:name="com.flutter.push.channeDescrition"
+            android:value="${pushChannelDescrition}" />
+
         <meta-data
             android:name="com.mi.push.api_key"
             android:value="${miPushAppKey}" />
@@ -53,6 +63,12 @@
             android:value="${meizhuPushAppId}" />
 
 
+        <activity android:name="com.flutter.flutter_aliyun_push.PopupPushActivity"
+            android:exported="true"
+            android:theme="@style/TranslucentActivity"
+            >
+        </activity>
+
         <receiver
             android:name="com.flutter.flutter_aliyun_push.FlutterMessageReceiver"
             android:exported="false"> <!-- 为保证receiver安全,建议设置不可导出,如需对其他应用开放可通过android:permission进行限制 -->

+ 39 - 24
android/src/main/java/com/flutter/flutter_aliyun_push/FlutterAliyunPushPlugin.java

@@ -8,6 +8,7 @@ import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.graphics.Color;
 import android.os.Build;
+import android.os.Handler;
 
 import androidx.annotation.NonNull;
 
@@ -43,6 +44,7 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
   private static String lastPushRegistSuccessMessage;
   private static String lastPushRegistErrorMessage;
   private static boolean isPluginAttached; //插件是否被加载到flutter
+  private static boolean isFlutterInvokeInitPush; //flutter是否调用了初始化方法,用来判断flutter是否已经添加了method监听
   private static List<FlutterPushNotification> cachedNotifications = new ArrayList<FlutterPushNotification>(); //未传到dart的消息
   private static List<FlutterPushMessage> cachedMessages = new ArrayList<FlutterPushMessage>(); //未传到dart的消息
 
@@ -75,28 +77,15 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
 
 
   public static void sendPushNotification(Context context,FlutterPushNotification message) {
-    if (mContext == null) {
-      //app未启动,厂家通道
-      Log.d(TAG,"showNotification:"+message.title);
-      NotificationUtil.showNotification(context,message.title,message.summary,message.summary);
-      return;
-    }
-
     if(FlutterAliyunPushPlugin.isPluginAttached) {
       EventBus.getDefault().post(new PushMessageEvent(PushMessageEvent.EVENT_onReceiverNotification,message));
     }else {
-      Log.d(FlutterAliyunPushPlugin.TAG, "message recevie not plugin not attach");
+      Log.d(FlutterAliyunPushPlugin.TAG, "notification recevie not plugin not attach");
       cachedNotifications.add(message);
     }
   }
 
   public static void sendPushMessage(Context context,FlutterPushMessage message) {
-    if (mContext == null) {
-      //app未启动,厂家通道
-      Log.d(TAG,"showNotification:"+message.title);
-      NotificationUtil.showNotification(context,message.title,message.content,message.content);
-      return;
-    }
     if(FlutterAliyunPushPlugin.isPluginAttached) {
       EventBus.getDefault().post(new PushMessageEvent(PushMessageEvent.EVENT_onReceiverMessage,message));
     }else {
@@ -105,11 +94,15 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
     }
   }
 
+  /**
+   * 该方法必须在Appcation onCreate中被执行,否则推送会有问题
+   * @param context
+   */
   public static void initPush(Context context) {
     Log.i(TAG, "start initPush");
     mContext = context;
     PushServiceFactory.init(context);
-    initPushVersion(context);
+    initThirdPush(context);
     CloudPushService pushService = PushServiceFactory.getCloudPushService();
     pushService.register(context, new CommonCallback() {
       @Override
@@ -137,7 +130,7 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
         }
       }
     });
-    initThirdPush(context);
+
   }
 
   /**
@@ -157,6 +150,12 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
     if(appInfo == null) {
       return;
     }
+
+    String pushChannelId =  appInfo.metaData.getString("com.flutter.push.channelId");
+    String pushChannelName =  appInfo.metaData.getString("com.flutter.push.channelName");
+    String pushChanneDescription =  appInfo.metaData.getString("com.flutter.push.channeDescrition");
+    initPushVersion(context, pushChannelId,pushChannelName,pushChanneDescription);
+
     config.miPushAppId = appInfo.metaData.getString("com.mi.push.app_id");
     config.miPushAppKey = appInfo.metaData.getString("com.mi.push.api_key");
 
@@ -198,16 +197,16 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
 
   }
 
-  private static void initPushVersion(Context context) {
+  private static void initPushVersion(Context context,String channelId,String channelName,String channelDescription) {
 //    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";
+      String id = channelId;
       // 用户可以看到的通知渠道的名字.
-      CharSequence name = "notification channel";
+      CharSequence name = channelName;
       // 用户可以看到的通知渠道的描述
-      String description = "notification description";
+      String description = channelDescription;
       int importance = NotificationManager.IMPORTANCE_HIGH;
       NotificationChannel mChannel = new NotificationChannel(id, name, importance);
       // 配置通知渠道的属性
@@ -266,6 +265,17 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
   }
 
   private void dealCacheEvent() {
+    if(!FlutterAliyunPushPlugin.isFlutterInvokeInitPush) {
+      //等待,直到dart有监听了执行
+      new Handler().postDelayed(new Runnable() {
+        @Override
+        public void run() {
+          dealCacheEvent();
+        }
+      },1000);
+      return;
+    }
+
     if(lastPushRegistSuccessMessage != null) {
       Log.i(TAG, "invokeMethod:"+ "onPushRegistSuccess");
       aliyunPushPluginChannel.invokeMethod("onPushRegistSuccess",lastPushRegistSuccessMessage);
@@ -276,17 +286,23 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
       lastPushRegistErrorMessage = null;
     }
 
+    dealOfflineMessage();
+  }
+
+  private void dealOfflineMessage() {
     //传递缓存的消息到dart
     if(cachedNotifications.size() > 0) {
+      Log.i(TAG, "invokeMethod cachedNotifications");
       for (FlutterPushNotification message : cachedNotifications) {
-        aliyunPushPluginChannel.invokeMethod(PushMessageEvent.EVENT_onReceiverNotification,message);
+        aliyunPushPluginChannel.invokeMethod(PushMessageEvent.EVENT_onReceiverNotification,message.getParamsJSONString());
       }
       cachedNotifications.clear();
     }
 
     if(cachedMessages.size() > 0) {
+      Log.i(TAG, "invokeMethod cachedMessages");
       for (FlutterPushMessage message : cachedMessages) {
-        aliyunPushPluginChannel.invokeMethod(PushMessageEvent.EVENT_onReceiverNotification,message);
+        aliyunPushPluginChannel.invokeMethod(PushMessageEvent.EVENT_onReceiverNotification,message.getParamsJSONString());
       }
       cachedMessages.clear();
     }
@@ -302,8 +318,7 @@ public class FlutterAliyunPushPlugin implements FlutterPlugin, MethodChannel.Met
         // This message is sent when the Dart side of this plugin is told to initialize.
         result.success("Android " + android.os.Build.VERSION.RELEASE);
       }else if(method.equals("initPush")) {
-//        initPush(this.context);
-
+        isFlutterInvokeInitPush = true;
       }
       else {
         result.notImplemented();

+ 7 - 0
android/src/main/java/com/flutter/flutter_aliyun_push/FlutterPushMessage.java

@@ -1,5 +1,7 @@
 package com.flutter.flutter_aliyun_push;
 
+import com.google.gson.Gson;
+
 import java.util.Map;
 
 public class FlutterPushMessage {
@@ -10,4 +12,9 @@ public class FlutterPushMessage {
     public String content;
     public String traceInfo;
 
+
+    public String getParamsJSONString() {
+        Gson gson = new Gson();
+        return gson.toJson(this);
+    }
 }

+ 9 - 0
android/src/main/java/com/flutter/flutter_aliyun_push/FlutterPushNotification.java

@@ -1,5 +1,7 @@
 package com.flutter.flutter_aliyun_push;
 
+import com.google.gson.Gson;
+
 import java.util.Map;
 
 public class FlutterPushNotification {
@@ -7,4 +9,11 @@ public class FlutterPushNotification {
     public String title;
     public String summary;
     public Map<String, String> extraMap;
+
+
+
+    public String getParamsJSONString() {
+        Gson gson = new Gson();
+        return gson.toJson(this);
+    }
 }

+ 51 - 0
android/src/main/java/com/flutter/flutter_aliyun_push/PopupPushActivity.java

@@ -0,0 +1,51 @@
+package com.flutter.flutter_aliyun_push;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.Gravity;
+import android.view.Window;
+import android.view.WindowManager;
+
+import com.alibaba.sdk.android.push.AndroidPopupActivity;
+
+import java.util.Map;
+
+import io.flutter.Log;
+
+
+public class PopupPushActivity extends AndroidPopupActivity {
+    static final String TAG = "PopupPushActivity";
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        Window window = getWindow();
+        window.setGravity(Gravity.LEFT|Gravity.TOP);
+        WindowManager.LayoutParams params = window.getAttributes();
+        params.x = 0;
+        params.y = 0;
+        params.height = 1;
+        params.width = 1;
+        window.setAttributes(params);
+
+    }
+    /**
+     * 实现通知打开回调方法,获取通知相关信息
+     * @param title     标题
+     * @param summary   内容
+     * @param extMap    额外参数
+     */
+    @Override
+    protected void onSysNoticeOpened(String title, String summary, Map<String, String> extMap) {
+        Log.d(TAG,"OnMiPushSysNoticeOpened, title: " + title + ", content: " + summary + ", extMap: " + extMap);
+
+        FlutterPushNotification notification = new FlutterPushNotification();
+        notification.title = title;
+        notification.summary = summary;
+        notification.extraMap = extMap;
+        FlutterAliyunPushPlugin.sendPushNotification(getApplicationContext(),notification);
+        Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
+        startActivity(intent);
+        finish();
+    }
+}

+ 7 - 0
android/src/main/res/values/styles.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <!--设置背景-->
+    <style name="TranslucentActivity" parent="android:Theme.Holo.Light.NoActionBar">//无标题
+        <item name="android:windowIsTranslucent">true</item>//透明
+    </style>
+</resources>

+ 12 - 9
example/android/app/build.gradle

@@ -43,15 +43,18 @@ android {
         manifestPlaceholders = [
                 aliyunPushAppKey: "30849262", //阿里云推送appkey
                 aliyunPushAppSecret:"a302e75da52a91910b5cbf01854791b1",//阿里云推送appsecret
-                miPushAppId:"\\2882303761517669764", //小米推送Appid
-                miPushAppKey:"\\5691766985764", //小米推送AppKey
-                huaweiPushAppId:"", //华为推送AppId
-                vivoPushAppId:"", //vivo推送AppId
-                vivoPushAppKey:"", //vivo推送AppKey
-                oppoPushAppKey:"", //oppo推送AppKey
-                oppoPushAppSecret:"", //oppo推送AppKey
-                meizhuPushAppId:"",  //魅族推送AppId
-                meizhuPushAppKey:"",//魅族推送AppKey
+                pushChannelId:"\\1", //安卓8.0后推送需指定渠道id,后台发送推送需要指定相同值(配置中纯数字需要加\\反斜线)
+                pushChannelName:"channelname", //安卓8.0后推送需指定用户可以看到的通知渠道的名字.,后台发送推送需要指定相同值(配置中纯数字需要加\\反斜线)
+                pushChannelDescrition:"channeldesc", //安卓8.0后推送需指定用户可以看到的通知渠道的描述.,后台发送推送需要指定相同值(配置中纯数字需要加\\反斜线)
+                miPushAppId:"\\2882303761517669764", //小米推送Appid (配置中纯数字需要加\\反斜线)
+                miPushAppKey:"\\5691766985764", //小米推送AppKey (配置中纯数字需要加\\反斜线)
+                huaweiPushAppId:"", //华为推送AppId (配置中纯数字需要加\\反斜线)
+                vivoPushAppId:"", //vivo推送AppId (配置中纯数字需要加\\反斜线)
+                vivoPushAppKey:"", //vivo推送AppKey (配置中纯数字需要加\\反斜线)
+                oppoPushAppKey:"", //oppo推送AppKey (配置中纯数字需要加\\反斜线)
+                oppoPushAppSecret:"", //oppo推送AppKey (配置中纯数字需要加\\反斜线)
+                meizhuPushAppId:"",  //魅族推送AppId (配置中纯数字需要加\\反斜线)
+                meizhuPushAppKey:"",//魅族推送AppKey (配置中纯数字需要加\\反斜线)
         ]
 
 

+ 1 - 0
example/android/app/src/main/java/com/flutter/flutter_aliyun_push_example/AppApplication.java

@@ -1,6 +1,7 @@
 package com.flutter.flutter_aliyun_push_example;
 
 
+
 import com.flutter.flutter_aliyun_push.FlutterAliyunPushPlugin;
 
 import io.flutter.Log;

+ 1 - 4
lib/flutter_aliyun_push.dart

@@ -21,10 +21,6 @@ class FlutterAliyunPush {
     return version;
   }
 
-  static Future<String> get initPush async {
-    final String version = await _channel.invokeMethod('initPush');
-    return version;
-  }
 
   /**
    * 注册原生调用dart
@@ -67,6 +63,7 @@ class FlutterAliyunPush {
           break;
       }
     });
+    _channel.invokeMethod('initPush');
   }
 
   static void  reigistOnRegistSuccess(Function callback) {