Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.unifiedpush.flutter.connector
import android.app.Activity
import android.content.Context
import android.util.Log
import android.content.Intent
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
Expand All @@ -16,10 +15,11 @@ import org.unifiedpush.android.connector.Registration
class Plugin : ActivityAware, FlutterPlugin, MethodCallHandler {
private var mContext : Context? = null
private var mActivity : Activity? = null
var withReceiverChannel: MethodChannel? = null

companion object {

var channel: MethodChannel? = null
var withCallbackChannel: MethodChannel? = null
private var up = Registration()

/**
Expand Down Expand Up @@ -73,26 +73,36 @@ class Plugin : ActivityAware, FlutterPlugin, MethodCallHandler {
}

@JvmStatic
private fun initializeService(context: Context, args: ArrayList<*>?, result: Result) {
val callbackHandle = args!![0] as Long
private fun initializeCallback(context: Context, args: ArrayList<*>?, result: Result) {
val callbackHandle = args?.get(0) as? Long ?: 0
context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
.edit()
.putLong(CALLBACK_DISPATCHER_HANDLE_KEY, callbackHandle)
.apply()
result.success(true)
}

fun isWithCallback(context: Context): Boolean {
val method = context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE)
.getLong(CALLBACK_DISPATCHER_HANDLE_KEY, 0)
Log.d("Plugin","isWithCallback: ${method > 0}")
return method > 0
}
}

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
Log.d("Plugin", "onAttachedToEngine")
mContext = binding.applicationContext
channel = MethodChannel(binding.binaryMessenger, PLUGIN_CHANNEL)
channel?.setMethodCallHandler(this)

withReceiverChannel = MethodChannel(binding.binaryMessenger, PLUGIN_CHANNEL)
withReceiverChannel?.setMethodCallHandler(this)
withCallbackChannel = MethodChannel(binding.binaryMessenger, PLUGIN_CHANNEL)
withCallbackChannel?.setMethodCallHandler(this)
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
Log.d("Plugin", "onDetachedFromEngine")
withReceiverChannel?.setMethodCallHandler(null)
withCallbackChannel?.setMethodCallHandler(null)
mContext = null
}

Expand All @@ -107,7 +117,7 @@ class Plugin : ActivityAware, FlutterPlugin, MethodCallHandler {
}

override fun onDetachedFromActivityForConfigChanges() {
Log.d("Plugin", "onDetachedFromActivityForConfigChanges")
Log.d("Plugin", "onDetachedFromActivityForConfigChanges")
mActivity = null
}

Expand All @@ -120,7 +130,7 @@ class Plugin : ActivityAware, FlutterPlugin, MethodCallHandler {
Log.d("Plugin","Method: ${call.method}")
val args = call.arguments<ArrayList<*>>()
when(call.method) {
PLUGIN_EVENT_INITIALIZE_CALLBACK -> initializeService(mContext!!, args, result)
PLUGIN_EVENT_INITIALIZE_CALLBACK -> initializeCallback(mContext!!, args, result)
PLUGIN_EVENT_REGISTER_APP_WITH_DIALOG -> registerAppWithDialog(mActivity!!, result)
PLUGIN_EVENT_GET_DISTRIBUTORS -> getDistributors(mActivity!!, result)
PLUGIN_EVENT_SAVE_DISTRIBUTOR -> saveDistributor(mActivity!!, args, result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package org.unifiedpush.flutter.connector

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.util.Log
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.view.FlutterMain
import org.unifiedpush.android.connector.MessagingReceiver
import org.unifiedpush.android.connector.MessagingReceiverHandler

/***
* Handler used when there is a callback
*/

val handler = object : MessagingReceiverHandler {

override fun onMessage(context: Context?, message: String) {
Log.d("Receiver","OnMessage")
FlutterMain.startInitialization(context!!)
FlutterMain.ensureInitializationComplete(context, null)
if (Plugin.channel != null && !CallbackService.sServiceStarted.get()){
if (Plugin.withCallbackChannel != null && !CallbackService.sServiceStarted.get()){
Log.d("Receiver","foregroundChannel")
Plugin.channel?.invokeMethod("onMessage", message)
Plugin.withCallbackChannel?.invokeMethod("onMessage", message)
} else {
Log.d("Receiver","CallbackChannel")
val intent = Intent(context, CallbackService::class.java)
Expand All @@ -29,8 +35,8 @@ val handler = object : MessagingReceiverHandler {
Log.d("Receiver","OnNewEndpoint")
FlutterMain.startInitialization(context!!)
FlutterMain.ensureInitializationComplete(context, null)
if (Plugin.channel != null && !CallbackService.sServiceStarted.get()) {
Plugin.channel?.invokeMethod("onNewEndpoint", endpoint)
if (Plugin.withCallbackChannel != null && !CallbackService.sServiceStarted.get()) {
Plugin.withCallbackChannel?.invokeMethod("onNewEndpoint", endpoint)
} else {
val intent = Intent(context, CallbackService::class.java)
intent.putExtra(EXTRA_CALLBACK_EVENT, CALLBACK_EVENT_NEW_ENDPOINT)
Expand All @@ -41,20 +47,20 @@ val handler = object : MessagingReceiverHandler {

override fun onRegistrationFailed(context: Context?) {
Log.d("Receiver","OnRegistrationFailed")
Plugin.channel?.invokeMethod("onRegistrationFailed", null)
Plugin.withCallbackChannel?.invokeMethod("onRegistrationFailed", null)
}

override fun onRegistrationRefused(context: Context?) {
Log.d("Receiver","OnRegistrationRefused")
Plugin.channel?.invokeMethod("onRegistrationRefused", null)
Plugin.withCallbackChannel?.invokeMethod("onRegistrationRefused", null)
}

override fun onUnregistered(context: Context?) {
Log.d("Receiver","OnUnregistered")
FlutterMain.startInitialization(context!!)
FlutterMain.ensureInitializationComplete(context, null)
if (Plugin.channel != null && !CallbackService.sServiceStarted.get()) {
Plugin.channel?.invokeMethod("onUnregistered", null)
if (Plugin.withCallbackChannel != null && !CallbackService.sServiceStarted.get()) {
Plugin.withCallbackChannel?.invokeMethod("onUnregistered", null)
} else {
val intent = Intent(context, CallbackService::class.java)
intent.putExtra(EXTRA_CALLBACK_EVENT, CALLBACK_EVENT_UNREGISTERED)
Expand All @@ -63,4 +69,64 @@ val handler = object : MessagingReceiverHandler {
}
}

class Receiver : MessagingReceiver(handler)
class Receiver : MessagingReceiver(handler) {
override fun onReceive(context: Context?, intent: Intent?) {
if (Plugin.isWithCallback(context!!)) {
super.onReceive(context, intent)
}
}
}

/***
* Handler used when the Receiver is defined in the app
*/

abstract class UnifiedPushHandler : MessagingReceiverHandler {
abstract fun getEngine(context: Context): FlutterEngine

private val handler = Handler()

private fun getPlugin(context: Context): Plugin {
val registry = getEngine(context).getPlugins()
var plugin = registry.get(Plugin::class.java) as? Plugin
if (plugin == null) {
plugin = Plugin()
registry.add(plugin)
}
return plugin;
}

override fun onMessage(context: Context?, message: String) {
Log.d("Receiver","OnMessage")
handler.post {
getPlugin(context!!).withReceiverChannel?.invokeMethod("onMessage", message)
}
}

override fun onNewEndpoint(context: Context?, endpoint: String) {
Log.d("Receiver","OnNewEndpoint")
handler.post {
getPlugin(context!!).withReceiverChannel?.invokeMethod("onNewEndpoint", endpoint)
}
}

override fun onRegistrationFailed(context: Context?) {
handler.post {
getPlugin(context!!).withReceiverChannel?.invokeMethod("onRegistrationFailed", null)
}
}

override fun onRegistrationRefused(context: Context?) {
Log.d("Receiver","OnRegistrationRefused")
handler.post {
getPlugin(context!!).withReceiverChannel?.invokeMethod("onRegistrationRefused", null)
}
}

override fun onUnregistered(context: Context?) {
Log.d("Receiver","OnUnregistered")
handler.post {
getPlugin(context!!).withReceiverChannel?.invokeMethod("onUnregistered", null)
}
}
}
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class _MyAppState extends State<MyApp> {

@override
void initState() {
UnifiedPush.initialize(
UnifiedPush.initializeWithCallback(
onNewEndpoint,
onRegistrationFailed,
onRegistrationRefused,
Expand Down
2 changes: 1 addition & 1 deletion lib/CallbackDispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unifiedpush/Constants.dart';
import 'main.dart';
import 'unifiedpush.dart';

void callbackDispatcher() {

Expand Down
112 changes: 0 additions & 112 deletions lib/main.dart

This file was deleted.

Loading