Skip to content

Commit

Permalink
Killed state notification fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Miller authored and Michael Miller committed Sep 26, 2022
1 parent 52a7f22 commit 80f0fc8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 34 deletions.
12 changes: 8 additions & 4 deletions android/src/main/java/com/courier/android/Courier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.courier.android.managers.UserManager
import com.courier.android.models.CourierAgent
import com.courier.android.models.CourierException
import com.courier.android.models.CourierProvider
import com.courier.android.models.CourierPushEvent
import com.courier.android.repositories.MessagingRepository
import com.courier.android.repositories.TokenRepository
import com.courier.android.utils.NotificationEventBus
import com.google.firebase.messaging.FirebaseMessaging
Expand Down Expand Up @@ -33,9 +35,10 @@ class Courier private constructor() {
* Courier.instance is required for nearly all features of the SDK
*/
fun initialize(context: Context) {
mInstance = Courier().apply {
this.context = context
if (mInstance == null) {
mInstance = Courier()
}
mInstance?.context = context
}

// This will not create a memory leak
Expand Down Expand Up @@ -137,9 +140,10 @@ class Courier private constructor() {
Courier.log("Clearing Courier User Credentials")

// Attempt to delete the current fcm token from the user
// If there is no access token and messaging token, skip this
val fcmToken = this@Courier.fcmToken ?: getCurrentFcmToken()
fcmToken?.let { token ->
tokenRepo.deleteUserToken(token)
if (accessToken != null && fcmToken != null) {
tokenRepo.deleteUserToken(fcmToken)
}

// Remove credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.courier.android.Courier
import com.courier.android.models.CourierAgent
import com.courier.android.trackPushNotificationClick

open class CourierActivity : AppCompatActivity() {
Expand Down
46 changes: 32 additions & 14 deletions android/src/main/java/com/courier/android/service/CourierService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,42 @@ import com.google.firebase.messaging.RemoteMessage

open class CourierService: FirebaseMessagingService() {

override fun onCreate() {
super.onCreate()

// Init the SDK if needed
Courier.initialize(context = this)

}

override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)

// Track the event in Courier
// The payload being sent to the device must contain only data
// If the payload contains title and body, there will be
// issues tracking the event
// More info: https://stackoverflow.com/a/71253912/2415921
Courier.shared.trackNotification(
message = message,
event = CourierPushEvent.DELIVERED,
onSuccess = { Courier.log("Event tracked") },
onFailure = { Courier.log(it.toString()) }
)
// Unlikely to fail, but in case it does
// we can still hit the show notification function
try {

// Track the event in Courier
// The payload being sent to the device must contain only data
// If the payload contains title and body, there will be
// issues tracking the event
// More info: https://stackoverflow.com/a/71253912/2415921
Courier.shared.trackNotification(
message = message,
event = CourierPushEvent.DELIVERED,
onSuccess = { Courier.log("Event tracked") },
onFailure = { Courier.log(it.toString()) }
)

// Broadcast the message to the app
// This will allow us to handle when it's delivered
Courier.shared.broadcastMessage(message)

} catch (e: Exception) {

Courier.log(e.toString())

// Broadcast the message to the app
// This will allow us to handle when it's delivered
Courier.shared.broadcastMessage(message)
}

// Try and show the notification
showNotification(message)
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
dependencies {

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.1'
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/courier/example/ExampleService.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.courier.example

import android.annotation.SuppressLint
import com.courier.android.notifications.presentNotification
import com.courier.android.service.CourierService
import com.google.firebase.messaging.RemoteMessage

// Warning is suppressed
// You do not need to worry about this warning
// The CourierService will handle the function automatically
@SuppressLint("MissingFirebaseInstanceTokenRefresh")
class ExampleService: CourierService() {

override fun showNotification(message: RemoteMessage) {
Expand Down
24 changes: 10 additions & 14 deletions app/src/main/java/com/courier/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.courier.android.Courier
import com.courier.android.models.CourierAgent
import com.courier.android.models.CourierProvider
import com.courier.android.notifications.CourierActivity
import com.courier.android.notifications.CourierPushNotificationCallbacks
Expand Down Expand Up @@ -50,6 +49,9 @@ class MainActivity : CourierActivity(), CourierPushNotificationCallbacks {

try {

val hasNotificationPermissions = requestNotificationPermission()
Toast.makeText(this@MainActivity, "Notification permissions are granted: $hasNotificationPermissions", Toast.LENGTH_LONG).show()

prefs = showSDKConfig(
activity = this@MainActivity,
title = "Configure SDK",
Expand Down Expand Up @@ -95,19 +97,13 @@ class MainActivity : CourierActivity(), CourierPushNotificationCallbacks {

try {

val granted = requestNotificationPermission()

if (granted) {

Courier.shared.sendPush(
authKey = prefs.getString("COURIER_AUTH_KEY", "") ?: "",
userId = prefs.getString("COURIER_USER_ID", "") ?: "",
title = "Hey ${Courier.shared.userId}!",
body = "This is a test message",
providers = listOf(CourierProvider.FCM),
)

}
Courier.shared.sendPush(
authKey = prefs.getString("COURIER_AUTH_KEY", "") ?: "",
userId = prefs.getString("COURIER_USER_ID", "") ?: "",
title = "Hey ${Courier.shared.userId}!",
body = "This is a test message",
providers = listOf(CourierProvider.FCM),
)

} catch (e: Exception) {

Expand Down

0 comments on commit 80f0fc8

Please sign in to comment.