-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
185 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
android/src/main/kotlin/com/courier/courier_flutter/CourierFlutterExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
android/src/main/kotlin/com/courier/courier_flutter/CourierFlutterFragmentActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...oid/src/main/kotlin/com/courier/courier_flutter/CourierFlutterPushNotificationListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters