11package com .onesignal .flutter ;
22
3- import android .app .Activity ;
43import android .content .Context ;
4+ import android .support .annotation .NonNull ;
55
66import com .onesignal .OSEmailSubscriptionObserver ;
77import com .onesignal .OSEmailSubscriptionStateChanges ;
2525import org .json .JSONObject ;
2626
2727import java .util .Collection ;
28- import java .util .HashMap ;
2928import java .util .Map ;
3029
3130import io .flutter .plugin .common .MethodCall ;
3534import io .flutter .plugin .common .PluginRegistry .Registrar ;
3635
3736/** OnesignalPlugin */
38- public class OneSignalPlugin implements MethodCallHandler , NotificationReceivedHandler , NotificationOpenedHandler ,
39- InAppMessageClickHandler , OSSubscriptionObserver , OSEmailSubscriptionObserver , OSPermissionObserver {
37+ public class OneSignalPlugin
38+ extends FlutterRegistrarResponder
39+ implements MethodCallHandler ,
40+ NotificationReceivedHandler ,
41+ NotificationOpenedHandler ,
42+ InAppMessageClickHandler ,
43+ OSSubscriptionObserver ,
44+ OSEmailSubscriptionObserver ,
45+ OSPermissionObserver {
4046
4147 /** Plugin registration. */
42- private Registrar flutterRegistrar ;
43- private MethodChannel channel ;
4448 private OSNotificationOpenResult coldStartNotificationResult ;
4549 private OSInAppMessageAction inAppMessageClickedResult ;
4650 private boolean hasSetNotificationOpenedHandler = false ;
@@ -54,18 +58,15 @@ public static void registerWith(Registrar registrar) {
5458 OneSignalPlugin plugin = new OneSignalPlugin ();
5559
5660 plugin .waitingForUserPrivacyConsent = false ;
57-
5861 plugin .channel = new MethodChannel (registrar .messenger (), "OneSignal" );
59-
6062 plugin .channel .setMethodCallHandler (plugin );
61-
6263 plugin .flutterRegistrar = registrar ;
6364
6465 OneSignalTagsController .registerWith (registrar );
6566 }
6667
6768 @ Override
68- public void onMethodCall (MethodCall call , Result result ) {
69+ public void onMethodCall (@ NonNull MethodCall call , @ NonNull Result result ) {
6970 if (call .method .contentEquals ("OneSignal#init" ))
7071 initOneSignal (call , result );
7172 else if (call .method .contentEquals ("OneSignal#setLogLevel" ))
@@ -105,34 +106,13 @@ else if (call.method.contentEquals("OneSignal#setExternalUserId"))
105106 else if (call .method .contentEquals ("OneSignal#removeExternalUserId" ))
106107 this .removeExternalUserId (result );
107108 else if (call .method .contentEquals ("OneSignal#addTrigger" )) {
108- // call.arguments is being casted to a Map<String, Object> so a try-catch with
109- // a ClassCastException will be thrown
110- try {
111- OneSignal .addTriggers ((Map <String , Object >) call .arguments );
112- } catch (ClassCastException e ) {
113- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , "Add trigger failed with error: " + e .getMessage ());
114- e .printStackTrace ();
115- }
109+ addTriggers (call .arguments );
116110 } else if (call .method .contentEquals ("OneSignal#addTriggers" )) {
117- // call.arguments is being casted to a Map<String, Object> so a try-catch with
118- // a ClassCastException will be thrown
119- try {
120- OneSignal .addTriggers ((Map <String , Object >) call .arguments );
121- } catch (ClassCastException e ) {
122- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , "Add triggers failed with error: " + e .getMessage ());
123- e .printStackTrace ();
124- }
111+ addTriggers (call .arguments );
125112 } else if (call .method .contentEquals ("OneSignal#removeTriggerForKey" ))
126113 OneSignal .removeTriggerForKey ((String ) call .arguments );
127114 else if (call .method .contentEquals ("OneSignal#removeTriggerForKeys" )) {
128- // call.arguments is being casted to a Collection<String> a try-catch with
129- // a ClassCastException will be thrown
130- try {
131- OneSignal .removeTriggersForKeys ((Collection <String >) call .arguments );
132- } catch (ClassCastException e ) {
133- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , "Remove trigger for keys failed with error: " + e .getMessage ());
134- e .printStackTrace ();
135- }
115+ removeTriggersForKeys (call .arguments );
136116 } else if (call .method .contentEquals ("OneSignal#getTriggerValueForKey" ))
137117 getTriggerValueForKey (result , (String ) call .arguments );
138118 else if (call .method .contentEquals ("OneSignal#pauseInAppMessages" ))
@@ -143,6 +123,28 @@ else if (call.method.contentEquals("OneSignal#initInAppMessageClickedHandlerPara
143123 replyNotImplemented (result );
144124 }
145125
126+ private void addTriggers (Object arguments ) {
127+ // call.arguments is being casted to a Map<String, Object> so a try-catch with
128+ // a ClassCastException will be thrown
129+ try {
130+ OneSignal .addTriggers ((Map <String , Object >) arguments );
131+ } catch (ClassCastException e ) {
132+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , "Add triggers failed with error: " + e .getMessage ());
133+ e .printStackTrace ();
134+ }
135+ }
136+
137+ private void removeTriggersForKeys (Object arguments ) {
138+ // call.arguments is being casted to a Collection<String> a try-catch with
139+ // a ClassCastException will be thrown
140+ try {
141+ OneSignal .removeTriggersForKeys ((Collection <String >) arguments );
142+ } catch (ClassCastException e ) {
143+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , "Remove trigger for keys failed with error: " + e .getMessage ());
144+ e .printStackTrace ();
145+ }
146+ }
147+
146148 private void getTriggerValueForKey (Result reply , String key ) {
147149 Object triggerValue = OneSignal .getTriggerValueForKey (key );
148150 replySuccess (reply , triggerValue );
@@ -334,27 +336,27 @@ private void removeExternalUserId(Result result) {
334336
335337 @ Override
336338 public void onOSSubscriptionChanged (OSSubscriptionStateChanges stateChanges ) {
337- this . channel . invokeMethod ("OneSignal#subscriptionChanged" , OneSignalSerializer .convertSubscriptionStateChangesToMap (stateChanges ));
339+ invokeMethodOnUiThread ("OneSignal#subscriptionChanged" , OneSignalSerializer .convertSubscriptionStateChangesToMap (stateChanges ));
338340 }
339341
340342 @ Override
341343 public void onOSEmailSubscriptionChanged (OSEmailSubscriptionStateChanges stateChanges ) {
342- this . channel . invokeMethod ("OneSignal#emailSubscriptionChanged" , OneSignalSerializer .convertEmailSubscriptionStateChangesToMap (stateChanges ));
344+ invokeMethodOnUiThread ("OneSignal#emailSubscriptionChanged" , OneSignalSerializer .convertEmailSubscriptionStateChangesToMap (stateChanges ));
343345 }
344346
345347 @ Override
346348 public void onOSPermissionChanged (OSPermissionStateChanges stateChanges ) {
347- this . channel . invokeMethod ("OneSignal#permissionChanged" , OneSignalSerializer .convertPermissionStateChangesToMap (stateChanges ));
349+ invokeMethodOnUiThread ("OneSignal#permissionChanged" , OneSignalSerializer .convertPermissionStateChangesToMap (stateChanges ));
348350 }
349351
350352 @ Override
351353 public void notificationReceived (OSNotification notification ) {
352354 try {
353- this . channel . invokeMethod ("OneSignal#handleReceivedNotification" , OneSignalSerializer .convertNotificationToMap (notification ));
355+ invokeMethodOnUiThread ("OneSignal#handleReceivedNotification" , OneSignalSerializer .convertNotificationToMap (notification ));
354356 } catch (JSONException e ) {
355357 e .printStackTrace ();
356358 OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR ,
357- "Encountered an error attempting to convert OSNotification object to hash map: " + e .getMessage ());
359+ "Encountered an error attempting to convert OSNotification object to hash map: " + e .getMessage ());
358360 }
359361 }
360362
@@ -366,7 +368,7 @@ public void notificationOpened(OSNotificationOpenResult result) {
366368 }
367369
368370 try {
369- this . channel . invokeMethod ("OneSignal#handleOpenedNotification" , OneSignalSerializer .convertNotificationOpenResultToMap (result ));
371+ invokeMethodOnUiThread ("OneSignal#handleOpenedNotification" , OneSignalSerializer .convertNotificationOpenResultToMap (result ));
370372 } catch (JSONException e ) {
371373 e .getStackTrace ();
372374 OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR ,
@@ -381,49 +383,6 @@ public void inAppMessageClicked(OSInAppMessageAction action) {
381383 return ;
382384 }
383385
384- this . channel . invokeMethod ("OneSignal#handleClickedInAppMessage" , OneSignalSerializer .convertInAppMessageClickedActionToMap (action ));
386+ invokeMethodOnUiThread ("OneSignal#handleClickedInAppMessage" , OneSignalSerializer .convertInAppMessageClickedActionToMap (action ));
385387 }
386-
387- /**
388- * MethodChannel class is home to success() method used by Result class
389- * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown
390- * This will communicate success back to Dart
391- */
392- private void replySuccess (final Result reply , final Object response ) {
393- ((Activity ) flutterRegistrar .activeContext ()).runOnUiThread (new Runnable () {
394- @ Override
395- public void run () {
396- reply .success (response );
397- }
398- });
399- }
400-
401- /**
402- * MethodChannel class is home to error() method used by Result class
403- * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown
404- * This will communicate error back to Dart
405- */
406- private void replyError (final Result reply , final String tag , final String message , final Object response ) {
407- ((Activity ) flutterRegistrar .activeContext ()).runOnUiThread (new Runnable () {
408- @ Override
409- public void run () {
410- reply .error (tag , message , response );
411- }
412- });
413- }
414-
415- /**
416- * MethodChannel class is home to notImplemented() method used by Result class
417- * It has the @UiThread annotation and must be run on UI thread, otherwise a RuntimeException will be thrown
418- * This will communicate not implemented back to Dart
419- */
420- private void replyNotImplemented (final Result reply ) {
421- ((Activity ) flutterRegistrar .activeContext ()).runOnUiThread (new Runnable () {
422- @ Override
423- public void run () {
424- reply .notImplemented ();
425- }
426- });
427- }
428-
429388}
0 commit comments