@@ -2,20 +2,26 @@ package org.unifiedpush.flutter.connector
22
33import android.content.Context
44import android.content.Intent
5+ import android.os.Handler
56import android.util.Log
7+ import io.flutter.embedding.engine.FlutterEngine
68import io.flutter.view.FlutterMain
79import org.unifiedpush.android.connector.MessagingReceiver
810import org.unifiedpush.android.connector.MessagingReceiverHandler
911
12+ /* **
13+ * Handler used when there is a callback
14+ */
15+
1016val handler = object : MessagingReceiverHandler {
1117
1218 override fun onMessage (context : Context ? , message : String ) {
1319 Log .d(" Receiver" ," OnMessage" )
1420 FlutterMain .startInitialization(context!! )
1521 FlutterMain .ensureInitializationComplete(context, null )
16- if (Plugin .channel != null && ! CallbackService .sServiceStarted.get()){
22+ if (Plugin .withCallbackChannel != null && ! CallbackService .sServiceStarted.get()){
1723 Log .d(" Receiver" ," foregroundChannel" )
18- Plugin .channel ?.invokeMethod(" onMessage" , message)
24+ Plugin .withCallbackChannel ?.invokeMethod(" onMessage" , message)
1925 } else {
2026 Log .d(" Receiver" ," CallbackChannel" )
2127 val intent = Intent (context, CallbackService ::class .java)
@@ -29,8 +35,8 @@ val handler = object : MessagingReceiverHandler {
2935 Log .d(" Receiver" ," OnNewEndpoint" )
3036 FlutterMain .startInitialization(context!! )
3137 FlutterMain .ensureInitializationComplete(context, null )
32- if (Plugin .channel != null && ! CallbackService .sServiceStarted.get()) {
33- Plugin .channel ?.invokeMethod(" onNewEndpoint" , endpoint)
38+ if (Plugin .withCallbackChannel != null && ! CallbackService .sServiceStarted.get()) {
39+ Plugin .withCallbackChannel ?.invokeMethod(" onNewEndpoint" , endpoint)
3440 } else {
3541 val intent = Intent (context, CallbackService ::class .java)
3642 intent.putExtra(EXTRA_CALLBACK_EVENT , CALLBACK_EVENT_NEW_ENDPOINT )
@@ -41,20 +47,20 @@ val handler = object : MessagingReceiverHandler {
4147
4248 override fun onRegistrationFailed (context : Context ? ) {
4349 Log .d(" Receiver" ," OnRegistrationFailed" )
44- Plugin .channel ?.invokeMethod(" onRegistrationFailed" , null )
50+ Plugin .withCallbackChannel ?.invokeMethod(" onRegistrationFailed" , null )
4551 }
4652
4753 override fun onRegistrationRefused (context : Context ? ) {
4854 Log .d(" Receiver" ," OnRegistrationRefused" )
49- Plugin .channel ?.invokeMethod(" onRegistrationRefused" , null )
55+ Plugin .withCallbackChannel ?.invokeMethod(" onRegistrationRefused" , null )
5056 }
5157
5258 override fun onUnregistered (context : Context ? ) {
5359 Log .d(" Receiver" ," OnUnregistered" )
5460 FlutterMain .startInitialization(context!! )
5561 FlutterMain .ensureInitializationComplete(context, null )
56- if (Plugin .channel != null && ! CallbackService .sServiceStarted.get()) {
57- Plugin .channel ?.invokeMethod(" onUnregistered" , null )
62+ if (Plugin .withCallbackChannel != null && ! CallbackService .sServiceStarted.get()) {
63+ Plugin .withCallbackChannel ?.invokeMethod(" onUnregistered" , null )
5864 } else {
5965 val intent = Intent (context, CallbackService ::class .java)
6066 intent.putExtra(EXTRA_CALLBACK_EVENT , CALLBACK_EVENT_UNREGISTERED )
@@ -63,4 +69,64 @@ val handler = object : MessagingReceiverHandler {
6369 }
6470}
6571
66- class Receiver : MessagingReceiver (handler)
72+ class Receiver : MessagingReceiver (handler) {
73+ override fun onReceive (context : Context ? , intent : Intent ? ) {
74+ if (Plugin .isWithCallback(context!! )) {
75+ super .onReceive(context, intent)
76+ }
77+ }
78+ }
79+
80+ /* **
81+ * Handler used when the Receiver is defined in the app
82+ */
83+
84+ abstract class UnifiedPushHandler : MessagingReceiverHandler {
85+ abstract fun getEngine (context : Context ): FlutterEngine
86+
87+ private val handler = Handler ()
88+
89+ private fun getPlugin (context : Context ): Plugin {
90+ val registry = getEngine(context).getPlugins()
91+ var plugin = registry.get(Plugin ::class .java) as ? Plugin
92+ if (plugin == null ) {
93+ plugin = Plugin ()
94+ registry.add(plugin)
95+ }
96+ return plugin;
97+ }
98+
99+ override fun onMessage (context : Context ? , message : String ) {
100+ Log .d(" Receiver" ," OnMessage" )
101+ handler.post {
102+ getPlugin(context!! ).withReceiverChannel?.invokeMethod(" onMessage" , message)
103+ }
104+ }
105+
106+ override fun onNewEndpoint (context : Context ? , endpoint : String ) {
107+ Log .d(" Receiver" ," OnNewEndpoint" )
108+ handler.post {
109+ getPlugin(context!! ).withReceiverChannel?.invokeMethod(" onNewEndpoint" , endpoint)
110+ }
111+ }
112+
113+ override fun onRegistrationFailed (context : Context ? ) {
114+ handler.post {
115+ getPlugin(context!! ).withReceiverChannel?.invokeMethod(" onRegistrationFailed" , null )
116+ }
117+ }
118+
119+ override fun onRegistrationRefused (context : Context ? ) {
120+ Log .d(" Receiver" ," OnRegistrationRefused" )
121+ handler.post {
122+ getPlugin(context!! ).withReceiverChannel?.invokeMethod(" onRegistrationRefused" , null )
123+ }
124+ }
125+
126+ override fun onUnregistered (context : Context ? ) {
127+ Log .d(" Receiver" ," OnUnregistered" )
128+ handler.post {
129+ getPlugin(context!! ).withReceiverChannel?.invokeMethod(" onUnregistered" , null )
130+ }
131+ }
132+ }
0 commit comments