Skip to content

Commit e62fd5c

Browse files
committed
Android Flutter Fragment Activity
1 parent dc4eec8 commit e62fd5c

File tree

5 files changed

+185
-49
lines changed

5 files changed

+185
-49
lines changed

android/src/main/kotlin/com/courier/courier_flutter/CourierFlutterActivity.kt

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,35 @@ package com.courier.courier_flutter
33
import android.content.Intent
44
import android.os.Bundle
55
import com.courier.android.Courier
6-
import com.courier.android.pushNotification
7-
import com.courier.android.trackPushNotificationClick
86
import com.google.firebase.messaging.RemoteMessage
97
import io.flutter.embedding.android.FlutterActivity
108
import io.flutter.embedding.engine.FlutterEngine
119
import io.flutter.plugin.common.MethodChannel
1210

1311

14-
open class CourierFlutterActivity : FlutterActivity() {
12+
open class CourierFlutterActivity : FlutterActivity(), CourierFlutterPushNotificationListener {
1513

1614
private var eventsChannel: MethodChannel? = null
1715

18-
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
19-
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
20-
}
21-
2216
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
2317
super.configureFlutterEngine(flutterEngine)
2418

25-
eventsChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CourierFlutterPlugin.EVENTS_CHANNEL).apply {
26-
setMethodCallHandler { call, result ->
27-
28-
when (call.method) {
29-
30-
"requestNotificationPermission" -> {
31-
32-
// TODO: Not supported yet due to AppCompat issues
33-
result.success("unknown")
34-
35-
}
36-
37-
"getNotificationPermissionStatus" -> {
38-
39-
// TODO: Not supported yet due to AppCompat issues
40-
result.success("unknown")
41-
42-
}
43-
44-
"getClickedNotification" -> {
45-
46-
checkIntentForPushNotificationClick(intent)
47-
result.success(null)
48-
49-
}
50-
51-
else -> {
52-
result.notImplemented()
53-
}
54-
19+
// Setup all the supported channels Courier can use
20+
eventsChannel = flutterEngine.setupCourierMethodChannel(
21+
onGetClickedNotification = {
22+
intent.getAndTrackRemoteMessage()?.let { message ->
23+
postPushNotificationClicked(message)
5524
}
56-
5725
}
58-
}
26+
)
5927

6028
}
6129

6230
override fun detachFromFlutterEngine() {
6331
super.detachFromFlutterEngine()
32+
6433
eventsChannel?.setMethodCallHandler(null)
34+
6535
}
6636

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

8050
// See if there is a pending click event
81-
checkIntentForPushNotificationClick(intent)
51+
intent.getAndTrackRemoteMessage()?.let { message ->
52+
postPushNotificationClicked(message)
53+
}
8254

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

9062
override fun onNewIntent(intent: Intent) {
9163
super.onNewIntent(intent)
92-
checkIntentForPushNotificationClick(intent)
93-
}
9464

95-
private fun checkIntentForPushNotificationClick(intent: Intent?) {
96-
intent?.trackPushNotificationClick { message ->
65+
intent.getAndTrackRemoteMessage()?.let { message ->
9766
postPushNotificationClicked(message)
9867
}
68+
9969
}
10070

101-
private fun postPushNotificationDelivered(message: RemoteMessage) {
102-
eventsChannel?.invokeMethod("pushNotificationDelivered", message.pushNotification)
71+
override fun postPushNotificationDelivered(message: RemoteMessage) {
72+
eventsChannel?.deliverCourierPushNotification(message)
10373
}
10474

105-
private fun postPushNotificationClicked(message: RemoteMessage) {
106-
eventsChannel?.invokeMethod("pushNotificationClicked", message.pushNotification)
75+
override fun postPushNotificationClicked(message: RemoteMessage) {
76+
eventsChannel?.clickCourierPushNotification(message)
10777
}
10878

10979
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.courier.courier_flutter
2+
3+
import android.content.Intent
4+
import com.courier.android.pushNotification
5+
import com.courier.android.trackPushNotificationClick
6+
import com.google.firebase.messaging.RemoteMessage
7+
import io.flutter.embedding.engine.FlutterEngine
8+
import io.flutter.plugin.common.MethodChannel
9+
10+
fun FlutterEngine.setupCourierMethodChannel(onRequestNotificationPermission: ((String) -> Unit)? = null, onGetNotificationPermissionStatus: ((String) -> Unit)? = null, onGetClickedNotification: (() -> Unit)? = null): MethodChannel {
11+
12+
// Create the method channel
13+
val channel = MethodChannel(dartExecutor.binaryMessenger, CourierFlutterPlugin.EVENTS_CHANNEL)
14+
15+
// Handle the calls
16+
channel.setMethodCallHandler { call, result ->
17+
18+
when (call.method) {
19+
20+
"requestNotificationPermission" -> {
21+
22+
// TODO: Not supported yet due to AppCompat issues
23+
val value = "unknown"
24+
onRequestNotificationPermission?.invoke(value)
25+
result.success(value)
26+
27+
}
28+
29+
"getNotificationPermissionStatus" -> {
30+
31+
// TODO: Not supported yet due to AppCompat issues
32+
val value = "unknown"
33+
onGetNotificationPermissionStatus?.invoke(value)
34+
result.success(value)
35+
36+
}
37+
38+
"getClickedNotification" -> {
39+
40+
onGetClickedNotification?.invoke()
41+
result.success(null)
42+
43+
}
44+
45+
else -> {
46+
result.notImplemented()
47+
}
48+
49+
}
50+
51+
}
52+
53+
// Return the channel
54+
return channel
55+
56+
}
57+
58+
fun MethodChannel.deliverCourierPushNotification(message: RemoteMessage) {
59+
invokeMethod("pushNotificationDelivered", message.pushNotification)
60+
}
61+
62+
fun MethodChannel.clickCourierPushNotification(message: RemoteMessage) {
63+
invokeMethod("pushNotificationClicked", message.pushNotification)
64+
}
65+
66+
fun Intent.getAndTrackRemoteMessage(): RemoteMessage? {
67+
68+
var clickedMessage: RemoteMessage? = null
69+
70+
// Try and track the clicked message
71+
// Will return a message if the message was able to be tracked
72+
trackPushNotificationClick { message ->
73+
clickedMessage = message
74+
}
75+
76+
return clickedMessage
77+
78+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.courier.courier_flutter
2+
3+
import android.content.Intent
4+
import android.os.Bundle
5+
import com.courier.android.Courier
6+
import com.google.firebase.messaging.RemoteMessage
7+
import io.flutter.embedding.android.FlutterFragmentActivity
8+
import io.flutter.embedding.engine.FlutterEngine
9+
import io.flutter.plugin.common.MethodChannel
10+
11+
12+
open class CourierFlutterFragmentActivity : FlutterFragmentActivity(), CourierFlutterPushNotificationListener {
13+
14+
private var eventsChannel: MethodChannel? = null
15+
16+
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
17+
super.configureFlutterEngine(flutterEngine)
18+
19+
// Setup all the supported channels Courier can use
20+
eventsChannel = flutterEngine.setupCourierMethodChannel(
21+
onGetClickedNotification = {
22+
intent.getAndTrackRemoteMessage()?.let { message ->
23+
postPushNotificationClicked(message)
24+
}
25+
}
26+
)
27+
28+
}
29+
30+
override fun onDestroy() {
31+
super.onDestroy()
32+
33+
// Remove the callbacks
34+
eventsChannel?.setMethodCallHandler(null)
35+
36+
}
37+
38+
override fun onCreate(savedInstanceState: Bundle?) {
39+
super.onCreate(savedInstanceState)
40+
41+
// Initialize the SDK
42+
Courier.initialize(context = this)
43+
44+
// Set the events listener
45+
Courier.shared.logListener = { log ->
46+
runOnUiThread {
47+
eventsChannel?.invokeMethod("log", log)
48+
}
49+
}
50+
51+
// See if there is a pending click event
52+
intent.getAndTrackRemoteMessage()?.let { message ->
53+
postPushNotificationClicked(message)
54+
}
55+
56+
// Handle delivered messages on the main thread
57+
Courier.getLastDeliveredMessage { message ->
58+
postPushNotificationDelivered(message)
59+
}
60+
61+
}
62+
63+
override fun onNewIntent(intent: Intent) {
64+
super.onNewIntent(intent)
65+
66+
intent.getAndTrackRemoteMessage()?.let { message ->
67+
postPushNotificationClicked(message)
68+
}
69+
70+
}
71+
72+
override fun postPushNotificationDelivered(message: RemoteMessage) {
73+
eventsChannel?.deliverCourierPushNotification(message)
74+
}
75+
76+
override fun postPushNotificationClicked(message: RemoteMessage) {
77+
eventsChannel?.clickCourierPushNotification(message)
78+
}
79+
80+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.courier.courier_flutter
2+
3+
import com.google.firebase.messaging.RemoteMessage
4+
5+
interface CourierFlutterPushNotificationListener {
6+
fun postPushNotificationDelivered(message: RemoteMessage)
7+
fun postPushNotificationClicked(message: RemoteMessage)
8+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: courier_flutter
22
description: The best way to add push notifications to your Flutter app!
3-
version: 1.0.6
3+
version: 1.0.61
44
homepage: https://courier.com
55

66
environment:

0 commit comments

Comments
 (0)