-
Notifications
You must be signed in to change notification settings - Fork 4
Removed Public Dependency on Firebase Messaging #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
24a0a2c
9567d00
9fa0902
01eff1d
528b6b0
c0211f9
7ca40c5
4c913d4
00c2628
e3092cb
eb27460
402859c
9404ef7
5f2f98c
5bba1b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come this is removed? |
||
|
|
||
| <manifest> | ||
| <!-- Empty --> | ||
| </manifest> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,17 +10,26 @@ import com.courier.android.client.CourierClient | |
| import com.courier.android.models.CourierAgent | ||
| import com.courier.android.models.CourierAuthenticationListener | ||
| import com.courier.android.models.CourierException | ||
| import com.courier.android.models.CourierPushNotificationEvent | ||
| import com.courier.android.models.CourierTrackingEvent | ||
| import com.courier.android.modules.InboxModule | ||
| import com.courier.android.modules.linkInbox | ||
| import com.courier.android.modules.refreshFcmToken | ||
| import com.courier.android.modules.setFcmToken | ||
| import com.courier.android.modules.unlinkInbox | ||
| import com.courier.android.utils.NotificationEventBus | ||
| import com.courier.android.utils.broadcastPushNotification | ||
| import com.courier.android.utils.error | ||
| import com.courier.android.utils.log | ||
| import com.courier.android.utils.trackPushNotification | ||
| import com.courier.android.utils.trackingUrl | ||
| import com.courier.android.utils.warn | ||
| import com.google.firebase.FirebaseApp | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.flow.collectLatest | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| /** | ||
|
|
@@ -56,9 +65,6 @@ class Courier private constructor(val context: Context) : Application.ActivityLi | |
| // Inbox | ||
| private const val SOCKET_CLEANUP_DURATION = 60_000L * 10 // 10 minutes | ||
|
|
||
| // Push | ||
| internal const val COURIER_PENDING_NOTIFICATION_KEY = "courier_pending_notification_key" | ||
|
|
||
| // Eventing | ||
| val eventBus by lazy { NotificationEventBus() } | ||
|
|
||
|
|
@@ -125,6 +131,51 @@ class Courier private constructor(val context: Context) : Application.ActivityLi | |
| } | ||
| } | ||
|
|
||
| // Broadcasts and tracks the message in Courier | ||
| fun onMessageReceived(data: Map<String, String>) = coroutineScope.launch(Dispatchers.IO) { | ||
| try { | ||
|
|
||
| val trackingEvent = CourierTrackingEvent.DELIVERED | ||
|
|
||
| // Broadcast the message to the app | ||
| broadcastPushNotification( | ||
| trackingEvent = trackingEvent, | ||
| data = data | ||
| ) | ||
|
|
||
| // Track the push notification delivery | ||
| data.trackingUrl?.let { trackingUrl -> | ||
| trackPushNotification( | ||
| trackingEvent = trackingEvent, | ||
| trackingUrl = trackingUrl | ||
| ) | ||
| } | ||
|
|
||
| } catch (e: Exception) { | ||
| CourierClient.default.error(e.toString()) | ||
| } | ||
| } | ||
|
|
||
| // Saves the notification token to Courier token management | ||
| fun onNewToken(token: String) { | ||
| try { | ||
| shared.setFcmToken( | ||
| token = token, | ||
| onSuccess = { shared.client?.log("Courier FCM token updated") }, | ||
| onFailure = { shared.client?.error(it.toString()) } | ||
| ) | ||
| } catch (e: Exception) { | ||
| CourierClient.default.error(e.toString()) | ||
| } | ||
| } | ||
|
|
||
| // Returns the last message that was delivered via the event bus | ||
| fun onPushNotificationEvent(onEvent: (event: CourierPushNotificationEvent) -> Unit) = coroutineScope.launch(Dispatchers.Main) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to change this to use a listener of some sort in the future. Not a huge deal for now |
||
| eventBus.events.collectLatest { | ||
| onEvent(it) | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // Inbox | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,13 @@ package com.courier.android.activity | |
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.courier.android.Courier | ||
| import com.courier.android.models.CourierTrackingEvent | ||
| import com.courier.android.utils.onPushNotificationEvent | ||
| import com.courier.android.utils.trackPushNotificationClick | ||
| import com.google.firebase.messaging.RemoteMessage | ||
| import com.courier.android.utils.getPushNotificationData | ||
| import com.courier.android.utils.trackPushNotification | ||
| import com.courier.android.utils.trackingUrl | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| open class CourierActivity : AppCompatActivity() { | ||
|
|
||
|
|
@@ -17,13 +19,16 @@ open class CourierActivity : AppCompatActivity() { | |
| // Init Courier if needed | ||
| Courier.initialize(context = this) | ||
|
|
||
| // See if there is a pending click event | ||
| // Handle initial push | ||
| checkIntentForPushNotificationClick(intent) | ||
|
|
||
| // Handle delivered messages on the main thread | ||
| Courier.shared.onPushNotificationEvent { event -> | ||
| if (event.trackingEvent == CourierTrackingEvent.DELIVERED) { | ||
| onPushNotificationDelivered(event.remoteMessage) | ||
| Courier.onPushNotificationEvent { event -> | ||
| when (event.trackingEvent) { | ||
| CourierTrackingEvent.DELIVERED -> { | ||
| onPushNotificationDelivered(event.data) | ||
| } | ||
| else -> Unit | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -34,14 +39,27 @@ open class CourierActivity : AppCompatActivity() { | |
| checkIntentForPushNotificationClick(intent) | ||
| } | ||
|
|
||
| private fun checkIntentForPushNotificationClick(intent: Intent?) { | ||
| intent?.trackPushNotificationClick { message -> | ||
| onPushNotificationClicked(message) | ||
| private fun checkIntentForPushNotificationClick(intent: Intent?) = lifecycleScope.launch { | ||
|
|
||
| // Get the notification and tracking event | ||
| val remoteMessage = intent?.getPushNotificationData() ?: return@launch | ||
| val trackingEvent = CourierTrackingEvent.CLICKED | ||
|
|
||
| // Broadcast the message | ||
| onPushNotificationClicked(remoteMessage) | ||
|
|
||
| // Track the message | ||
| remoteMessage.trackingUrl?.let { trackingUrl -> | ||
| trackPushNotification( | ||
| trackingEvent = trackingEvent, | ||
| trackingUrl = trackingUrl | ||
| ) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| open fun onPushNotificationClicked(message: RemoteMessage) {} | ||
| open fun onPushNotificationClicked(pushNotification: Map<String, String>) {} | ||
|
|
||
| open fun onPushNotificationDelivered(message: RemoteMessage) {} | ||
| open fun onPushNotificationDelivered(pushNotification: Map<String, String>) {} | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can no longer expose the public class at this point. Using a simple map is more ideal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking: since we're now taking an |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
| package com.courier.android.models | ||
|
|
||
| import com.google.firebase.messaging.RemoteMessage | ||
|
|
||
| data class CourierPushNotificationEvent( | ||
| val trackingEvent: CourierTrackingEvent, | ||
| val remoteMessage: RemoteMessage | ||
| val data: Map<String, String> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd consider creating a class for the message data (ex. |
||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: These could use consistent syntax:
The Android docs use double quotes here, too, if we want to stay consistent with those examples: https://developer.android.com/develop/ui/compose/bom#kts