Skip to content

Commit 4b42f6b

Browse files
committed
bug fixes
1 parent 538946a commit 4b42f6b

File tree

2 files changed

+100
-34
lines changed

2 files changed

+100
-34
lines changed

raven-fcm-util/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ android {
2020
minSdkVersion 16
2121
targetSdkVersion 30
2222
versionCode 1
23-
versionName "1.0.3"
23+
versionName "1.0.5"
2424

2525
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2626
consumerProguardFiles "consumer-rules.pro"
@@ -61,8 +61,8 @@ dependencies {
6161
implementation 'com.google.firebase:firebase-analytics:18.0.2'
6262
implementation 'com.google.firebase:firebase-messaging:21.0.1'
6363

64-
//api project(':raven-android-sdk')
65-
api 'com.github.ravenappdev:raven-android-sdk:1.0.3'
64+
// implementation project(':raven-android-sdk')
65+
implementation 'com.github.ravenappdev:raven-android-sdk:1.0.5'
6666
}
6767

6868
// Because the components are created only during the afterEvaluate phase, you must
@@ -78,7 +78,7 @@ afterEvaluate {
7878
// You can then customize attributes of the publication as shown below.
7979
groupId = 'com.raven'
8080
artifactId = 'raven-fcm-util'
81-
version = '1.0.3'
81+
version = '1.0.5'
8282
}
8383
}
8484
}

raven-fcm-util/src/main/java/com/raven/ravenfcmutil/notification/MyFirebaseMessagingService.kt

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ package com.raven.ravenfcmutil.notification
33
import android.app.*
44
import android.content.Context
55
import android.content.Intent
6+
import android.content.pm.PackageManager
67
import android.graphics.Bitmap
78
import android.os.Build
89
import android.util.Log
910
import androidx.core.app.NotificationCompat
1011
import com.bumptech.glide.Glide
1112
import com.bumptech.glide.request.FutureTarget
12-
import com.bumptech.glide.request.target.CustomTarget
13-
import com.bumptech.glide.request.transition.Transition
1413
import com.google.android.gms.tasks.OnCompleteListener
15-
import com.google.firebase.iid.FirebaseInstanceId
1614
import com.google.firebase.messaging.FirebaseMessaging
1715
import com.google.firebase.messaging.FirebaseMessagingService
1816
import com.google.firebase.messaging.RemoteMessage
@@ -30,19 +28,27 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
3028

3129
override fun onMessageReceived(remoteMessage: RemoteMessage) {
3230
if (remoteMessage.data.isNotEmpty()) {
33-
val id = remoteMessage.data[RavenSdk.RAVEN_NOTIFICATION_ID]
34-
sendNotification(id, remoteMessage.data["title"], remoteMessage.data["body"],
35-
remoteMessage.data["click_action"], remoteMessage.data["large_icon"], remoteMessage.data["big_picture"])
31+
32+
val notificationId = remoteMessage.data[RavenSdk.RAVEN_NOTIFICATION_ID]
3633

3734
//update status to raven that the message is delivered
38-
id?.let { RavenSdk.updateStatus(it, Status.DELIVERED) }
35+
notificationId?.let { RavenSdk.updateStatus(notificationId, Status.DELIVERED) }
36+
37+
sendNotification(
38+
notificationId,
39+
remoteMessage.data["title"],
40+
remoteMessage.data["body"],
41+
remoteMessage.data["click_action"],
42+
remoteMessage.data["large_icon"],
43+
remoteMessage.data["big_picture"]
44+
)
3945
}
4046
}
4147

4248

4349
override fun onDeletedMessages() {
4450
super.onDeletedMessages()
45-
//notify be to fetch long pending messages
51+
//notify to fetch long pending messages
4652
}
4753

4854

@@ -54,44 +60,77 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
5460
}
5561

5662

57-
private fun sendNotification(ravenNotificationId: String?,
58-
title: String?, messageBody: String?, clickAction: String?,
59-
largeIcon: String? = null, bigPicture: String? = null) {
63+
private fun sendNotification(
64+
ravenNotificationId: String?,
65+
title: String?, messageBody: String?, clickAction: String?,
66+
largeIcon: String? = null, bigPicture: String? = null
67+
) {
6068

6169
var style: NotificationCompat.Style = NotificationCompat.BigTextStyle().bigText(messageBody)
6270

6371
//fetch largeicon and bigpicture
6472
if (bigPicture != null && largeIcon != null) {
65-
coroutineScope.launch(Dispatchers.Main) {
73+
coroutineScope.launch {
74+
6675
val largeIconBitmap = loadImageAsBitmap(largeIcon)
6776
val bigPictureBitmap = loadImageAsBitmap(bigPicture)
68-
style = NotificationCompat.BigPictureStyle().bigPicture(bigPictureBitmap).bigLargeIcon(null)
69-
notify(ravenNotificationId, title, messageBody, clickAction, largeIconBitmap, style)
77+
style = NotificationCompat.BigPictureStyle().bigPicture(bigPictureBitmap).bigLargeIcon(
78+
null
79+
)
80+
81+
withContext(Dispatchers.Main) {
82+
notify(
83+
ravenNotificationId,
84+
title,
85+
messageBody,
86+
clickAction,
87+
largeIconBitmap,
88+
style
89+
)
90+
}
7091
}
7192
} else if (bigPicture != null) {
72-
coroutineScope.launch(Dispatchers.Main) {
93+
coroutineScope.launch {
7394
val bigPictureBitmap = loadImageAsBitmap(bigPicture)
74-
style = NotificationCompat.BigPictureStyle().bigPicture(bigPictureBitmap).bigLargeIcon(null)
75-
notify(ravenNotificationId, title, messageBody, clickAction, null, style)
95+
style = NotificationCompat.BigPictureStyle().bigPicture(bigPictureBitmap).bigLargeIcon(
96+
null
97+
)
98+
99+
withContext(Dispatchers.Main) {
100+
notify(ravenNotificationId, title, messageBody, clickAction, null, style)
101+
}
76102
}
77103
} else if (largeIcon != null) {
78-
coroutineScope.launch(Dispatchers.Main) {
104+
coroutineScope.launch {
79105
val largeIconBitmap = loadImageAsBitmap(largeIcon)
80-
notify(ravenNotificationId, title, messageBody, clickAction, largeIconBitmap, style)
106+
withContext(Dispatchers.Main) {
107+
notify(
108+
ravenNotificationId,
109+
title,
110+
messageBody,
111+
clickAction,
112+
largeIconBitmap,
113+
style
114+
)
115+
}
81116
}
82117
} else {
83118
notify(ravenNotificationId, title, messageBody, clickAction, null, style)
84119
}
85120
}
86121

87122

88-
private fun notify(ravenNotificationId: String?,
89-
title: String?, messageBody: String?, clickAction: String?,
90-
largeIcon: Bitmap? = null, style: NotificationCompat.Style? = null) {
123+
private fun notify(
124+
ravenNotificationId: String?,
125+
title: String?, messageBody: String?, clickAction: String?,
126+
largeIcon: Bitmap? = null, style: NotificationCompat.Style? = null
127+
) {
128+
129+
//
91130

92131
val channelId = "Default"
93132
val notificationBuilder = NotificationCompat.Builder(this, channelId)
94-
.setSmallIcon(R.color.design_default_color_on_primary)
133+
.setSmallIcon(getSmallIcon())
95134
.setContentTitle(title)
96135
.setContentText(messageBody)
97136
.setLargeIcon(largeIcon)
@@ -104,27 +143,46 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
104143

105144
// Since android Oreo notification channel is needed.
106145
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
107-
val channel = NotificationChannel(channelId, "Default Channel",
108-
NotificationManager.IMPORTANCE_DEFAULT)
146+
val channel = NotificationChannel(
147+
channelId, "Default Channel",
148+
NotificationManager.IMPORTANCE_DEFAULT
149+
)
109150
notificationManager.createNotificationChannel(channel)
110151
}
111152

112-
notificationManager.notify(Date().time.toInt() /* ID of notification */, notificationBuilder.build())
153+
notificationManager.notify(
154+
Date().time.toInt() /* ID of notification */,
155+
notificationBuilder.build()
156+
)
113157
}
114158

115159

116160
private fun createOnDismissedIntent(context: Context, notificationId: String?): PendingIntent? {
117161
val intent = Intent(context, NotificationDismissedReceiver::class.java)
118162
intent.putExtra(RavenSdk.RAVEN_NOTIFICATION_ID, notificationId)
119-
return PendingIntent.getBroadcast(context.applicationContext, 100, intent, 0)
163+
return PendingIntent.getBroadcast(
164+
context.applicationContext,
165+
Date().time.toInt(),
166+
intent,
167+
0
168+
)
120169
}
121170

122171

123-
private fun createOnClickedIntent(context: Context, notificationId: String?, clickAction: String?): PendingIntent? {
172+
private fun createOnClickedIntent(
173+
context: Context,
174+
notificationId: String?,
175+
clickAction: String?
176+
): PendingIntent? {
124177
val intent = Intent(context, NotificationClickReceiver::class.java)
125178
intent.putExtra(RavenSdk.RAVEN_NOTIFICATION_ID, notificationId)
126179
intent.putExtra("click_action", clickAction)
127-
return PendingIntent.getBroadcast(context.applicationContext, 101, intent, 0)
180+
return PendingIntent.getBroadcast(
181+
context.applicationContext,
182+
Date().time.toInt(),
183+
intent,
184+
0
185+
)
128186
}
129187

130188

@@ -147,6 +205,14 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
147205
}
148206

149207

208+
private fun getSmallIcon(): Int {
209+
val ai = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
210+
val bundle = ai.metaData
211+
val smallIcon: Int = bundle.getInt("notification_small_icon")
212+
return smallIcon
213+
}
214+
215+
150216
companion object {
151217

152218
/*
@@ -163,7 +229,7 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
163229
// Get new Instance ID token
164230
val token: String? = task.result
165231
token?.let { RavenSdk.setDeviceToken(it) }
166-
})
232+
})
167233
}
168234

169235

0 commit comments

Comments
 (0)