Skip to content

Commit

Permalink
Android Flutter Fragment Activity
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemilla committed May 30, 2023
1 parent dc4eec8 commit e62fd5c
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,35 @@ package com.courier.courier_flutter
import android.content.Intent
import android.os.Bundle
import com.courier.android.Courier
import com.courier.android.pushNotification
import com.courier.android.trackPushNotificationClick
import com.google.firebase.messaging.RemoteMessage
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel


open class CourierFlutterActivity : FlutterActivity() {
open class CourierFlutterActivity : FlutterActivity(), CourierFlutterPushNotificationListener {

private var eventsChannel: MethodChannel? = null

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)

eventsChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CourierFlutterPlugin.EVENTS_CHANNEL).apply {
setMethodCallHandler { call, result ->

when (call.method) {

"requestNotificationPermission" -> {

// TODO: Not supported yet due to AppCompat issues
result.success("unknown")

}

"getNotificationPermissionStatus" -> {

// TODO: Not supported yet due to AppCompat issues
result.success("unknown")

}

"getClickedNotification" -> {

checkIntentForPushNotificationClick(intent)
result.success(null)

}

else -> {
result.notImplemented()
}

// Setup all the supported channels Courier can use
eventsChannel = flutterEngine.setupCourierMethodChannel(
onGetClickedNotification = {
intent.getAndTrackRemoteMessage()?.let { message ->
postPushNotificationClicked(message)
}

}
}
)

}

override fun detachFromFlutterEngine() {
super.detachFromFlutterEngine()

eventsChannel?.setMethodCallHandler(null)

}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -78,7 +48,9 @@ open class CourierFlutterActivity : FlutterActivity() {
}

// See if there is a pending click event
checkIntentForPushNotificationClick(intent)
intent.getAndTrackRemoteMessage()?.let { message ->
postPushNotificationClicked(message)
}

// Handle delivered messages on the main thread
Courier.getLastDeliveredMessage { message ->
Expand All @@ -89,21 +61,19 @@ open class CourierFlutterActivity : FlutterActivity() {

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
checkIntentForPushNotificationClick(intent)
}

private fun checkIntentForPushNotificationClick(intent: Intent?) {
intent?.trackPushNotificationClick { message ->
intent.getAndTrackRemoteMessage()?.let { message ->
postPushNotificationClicked(message)
}

}

private fun postPushNotificationDelivered(message: RemoteMessage) {
eventsChannel?.invokeMethod("pushNotificationDelivered", message.pushNotification)
override fun postPushNotificationDelivered(message: RemoteMessage) {
eventsChannel?.deliverCourierPushNotification(message)
}

private fun postPushNotificationClicked(message: RemoteMessage) {
eventsChannel?.invokeMethod("pushNotificationClicked", message.pushNotification)
override fun postPushNotificationClicked(message: RemoteMessage) {
eventsChannel?.clickCourierPushNotification(message)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.courier.courier_flutter

import android.content.Intent
import com.courier.android.pushNotification
import com.courier.android.trackPushNotificationClick
import com.google.firebase.messaging.RemoteMessage
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

fun FlutterEngine.setupCourierMethodChannel(onRequestNotificationPermission: ((String) -> Unit)? = null, onGetNotificationPermissionStatus: ((String) -> Unit)? = null, onGetClickedNotification: (() -> Unit)? = null): MethodChannel {

// Create the method channel
val channel = MethodChannel(dartExecutor.binaryMessenger, CourierFlutterPlugin.EVENTS_CHANNEL)

// Handle the calls
channel.setMethodCallHandler { call, result ->

when (call.method) {

"requestNotificationPermission" -> {

// TODO: Not supported yet due to AppCompat issues
val value = "unknown"
onRequestNotificationPermission?.invoke(value)
result.success(value)

}

"getNotificationPermissionStatus" -> {

// TODO: Not supported yet due to AppCompat issues
val value = "unknown"
onGetNotificationPermissionStatus?.invoke(value)
result.success(value)

}

"getClickedNotification" -> {

onGetClickedNotification?.invoke()
result.success(null)

}

else -> {
result.notImplemented()
}

}

}

// Return the channel
return channel

}

fun MethodChannel.deliverCourierPushNotification(message: RemoteMessage) {
invokeMethod("pushNotificationDelivered", message.pushNotification)
}

fun MethodChannel.clickCourierPushNotification(message: RemoteMessage) {
invokeMethod("pushNotificationClicked", message.pushNotification)
}

fun Intent.getAndTrackRemoteMessage(): RemoteMessage? {

var clickedMessage: RemoteMessage? = null

// Try and track the clicked message
// Will return a message if the message was able to be tracked
trackPushNotificationClick { message ->
clickedMessage = message
}

return clickedMessage

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.courier.courier_flutter

import android.content.Intent
import android.os.Bundle
import com.courier.android.Courier
import com.google.firebase.messaging.RemoteMessage
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel


open class CourierFlutterFragmentActivity : FlutterFragmentActivity(), CourierFlutterPushNotificationListener {

private var eventsChannel: MethodChannel? = null

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)

// Setup all the supported channels Courier can use
eventsChannel = flutterEngine.setupCourierMethodChannel(
onGetClickedNotification = {
intent.getAndTrackRemoteMessage()?.let { message ->
postPushNotificationClicked(message)
}
}
)

}

override fun onDestroy() {
super.onDestroy()

// Remove the callbacks
eventsChannel?.setMethodCallHandler(null)

}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Initialize the SDK
Courier.initialize(context = this)

// Set the events listener
Courier.shared.logListener = { log ->
runOnUiThread {
eventsChannel?.invokeMethod("log", log)
}
}

// See if there is a pending click event
intent.getAndTrackRemoteMessage()?.let { message ->
postPushNotificationClicked(message)
}

// Handle delivered messages on the main thread
Courier.getLastDeliveredMessage { message ->
postPushNotificationDelivered(message)
}

}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)

intent.getAndTrackRemoteMessage()?.let { message ->
postPushNotificationClicked(message)
}

}

override fun postPushNotificationDelivered(message: RemoteMessage) {
eventsChannel?.deliverCourierPushNotification(message)
}

override fun postPushNotificationClicked(message: RemoteMessage) {
eventsChannel?.clickCourierPushNotification(message)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.courier.courier_flutter

import com.google.firebase.messaging.RemoteMessage

interface CourierFlutterPushNotificationListener {
fun postPushNotificationDelivered(message: RemoteMessage)
fun postPushNotificationClicked(message: RemoteMessage)
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: courier_flutter
description: The best way to add push notifications to your Flutter app!
version: 1.0.6
version: 1.0.61
homepage: https://courier.com

environment:
Expand Down

0 comments on commit e62fd5c

Please sign in to comment.