11package com .sensorsdata .analytics .sensorsanalyticsflutterplugin ;
22
3- import io .flutter .plugin .common .MethodCall ;
4- import io .flutter .plugin .common .MethodChannel ;
5- import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
6- import io .flutter .plugin .common .MethodChannel .Result ;
7- import io .flutter .plugin .common .PluginRegistry .Registrar ;
83import android .text .TextUtils ;
4+
5+ import androidx .annotation .NonNull ;
6+
97import com .sensorsdata .analytics .android .sdk .SALog ;
108import com .sensorsdata .analytics .android .sdk .SensorsDataAPI ;
9+
1110import org .json .JSONObject ;
12- import java .util .List ;
13- import java .util .Map ;
11+
1412import java .util .Collection ;
1513import java .util .HashSet ;
14+ import java .util .List ;
15+ import java .util .Map ;
16+
17+ import io .flutter .embedding .engine .plugins .FlutterPlugin ;
18+ import io .flutter .plugin .common .MethodCall ;
19+ import io .flutter .plugin .common .MethodChannel ;
20+ import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
21+ import io .flutter .plugin .common .MethodChannel .Result ;
22+ import io .flutter .plugin .common .PluginRegistry .Registrar ;
1623
1724/**
1825 * Sensors Analytics Flutter Plugin
19- **/
20-
21- public class SensorsAnalyticsFlutterPlugin implements MethodCallHandler {
22-
26+ */
27+ public class SensorsAnalyticsFlutterPlugin implements FlutterPlugin , MethodCallHandler {
28+ private MethodChannel channel ;
2329 private static final String TAG = "SA.SensorsAnalyticsFlutterPlugin" ;
2430
31+ @ Override
32+ public void onAttachedToEngine (@ NonNull FlutterPluginBinding flutterPluginBinding ) {
33+ channel = new MethodChannel (flutterPluginBinding .getFlutterEngine ().getDartExecutor (), "sensors_analytics_flutter_plugin" );
34+ channel .setMethodCallHandler (this );
35+ }
36+
2537 public static void registerWith (Registrar registrar ) {
2638 final MethodChannel channel = new MethodChannel (registrar .messenger (), "sensors_analytics_flutter_plugin" );
2739 channel .setMethodCallHandler (new SensorsAnalyticsFlutterPlugin ());
2840 }
2941
3042 @ Override
31- public void onMethodCall (MethodCall call , Result result ) {
43+ public void onMethodCall (@ NonNull MethodCall call , @ NonNull Result result ) {
3244 try {
3345 List list = (List ) call .arguments ;
3446 switch (call .method ) {
@@ -86,6 +98,12 @@ public void onMethodCall(MethodCall call, Result result) {
8698 case "getDistinctId" :
8799 getDistinctId (result );
88100 break ;
101+ case "profilePushId" :
102+ profilePushId (list );
103+ break ;
104+ case "profileUnsetPushId" :
105+ profileUnsetPushId (list );
106+ break ;
89107 default :
90108 result .notImplemented ();
91109 break ;
@@ -94,7 +112,11 @@ public void onMethodCall(MethodCall call, Result result) {
94112 e .printStackTrace ();
95113 SALog .d (TAG , e .getMessage ());
96114 }
115+ }
97116
117+ @ Override
118+ public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
119+ channel .setMethodCallHandler (null );
98120 }
99121
100122 /**
@@ -242,6 +264,20 @@ private void clearSuperProperties() {
242264 SensorsDataAPI .sharedInstance ().clearSuperProperties ();
243265 }
244266
267+ /**
268+ * 保存用户推送 ID 到用户表
269+ */
270+ private void profilePushId (List list ){
271+ SensorsDataAPI .sharedInstance ().profilePushId ((String )list .get (0 ), (String )list .get (1 ));
272+ }
273+
274+ /**
275+ * 删除用户设置的 pushId
276+ */
277+ private void profileUnsetPushId (List list ){
278+ SensorsDataAPI .sharedInstance ().profileUnsetPushId ((String )list .get (0 ));
279+ }
280+
245281 private JSONObject assertProperties (Map map ) {
246282 if (map != null ) {
247283 return new JSONObject (map );
0 commit comments