@@ -3,16 +3,14 @@ package com.raven.ravenfcmutil.notification
3
3
import android.app.*
4
4
import android.content.Context
5
5
import android.content.Intent
6
+ import android.content.pm.PackageManager
6
7
import android.graphics.Bitmap
7
8
import android.os.Build
8
9
import android.util.Log
9
10
import androidx.core.app.NotificationCompat
10
11
import com.bumptech.glide.Glide
11
12
import com.bumptech.glide.request.FutureTarget
12
- import com.bumptech.glide.request.target.CustomTarget
13
- import com.bumptech.glide.request.transition.Transition
14
13
import com.google.android.gms.tasks.OnCompleteListener
15
- import com.google.firebase.iid.FirebaseInstanceId
16
14
import com.google.firebase.messaging.FirebaseMessaging
17
15
import com.google.firebase.messaging.FirebaseMessagingService
18
16
import com.google.firebase.messaging.RemoteMessage
@@ -30,19 +28,27 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
30
28
31
29
override fun onMessageReceived (remoteMessage : RemoteMessage ) {
32
30
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 ]
36
33
37
34
// 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
+ )
39
45
}
40
46
}
41
47
42
48
43
49
override fun onDeletedMessages () {
44
50
super .onDeletedMessages()
45
- // notify be to fetch long pending messages
51
+ // notify to fetch long pending messages
46
52
}
47
53
48
54
@@ -54,44 +60,77 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
54
60
}
55
61
56
62
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
+ ) {
60
68
61
69
var style: NotificationCompat .Style = NotificationCompat .BigTextStyle ().bigText(messageBody)
62
70
63
71
// fetch largeicon and bigpicture
64
72
if (bigPicture != null && largeIcon != null ) {
65
- coroutineScope.launch(Dispatchers .Main ) {
73
+ coroutineScope.launch {
74
+
66
75
val largeIconBitmap = loadImageAsBitmap(largeIcon)
67
76
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
+ }
70
91
}
71
92
} else if (bigPicture != null ) {
72
- coroutineScope.launch( Dispatchers . Main ) {
93
+ coroutineScope.launch {
73
94
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
+ }
76
102
}
77
103
} else if (largeIcon != null ) {
78
- coroutineScope.launch( Dispatchers . Main ) {
104
+ coroutineScope.launch {
79
105
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
+ }
81
116
}
82
117
} else {
83
118
notify(ravenNotificationId, title, messageBody, clickAction, null , style)
84
119
}
85
120
}
86
121
87
122
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
+ //
91
130
92
131
val channelId = " Default"
93
132
val notificationBuilder = NotificationCompat .Builder (this , channelId)
94
- .setSmallIcon(R .color.design_default_color_on_primary )
133
+ .setSmallIcon(getSmallIcon() )
95
134
.setContentTitle(title)
96
135
.setContentText(messageBody)
97
136
.setLargeIcon(largeIcon)
@@ -104,27 +143,46 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
104
143
105
144
// Since android Oreo notification channel is needed.
106
145
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
+ )
109
150
notificationManager.createNotificationChannel(channel)
110
151
}
111
152
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
+ )
113
157
}
114
158
115
159
116
160
private fun createOnDismissedIntent (context : Context , notificationId : String? ): PendingIntent ? {
117
161
val intent = Intent (context, NotificationDismissedReceiver ::class .java)
118
162
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
+ )
120
169
}
121
170
122
171
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 ? {
124
177
val intent = Intent (context, NotificationClickReceiver ::class .java)
125
178
intent.putExtra(RavenSdk .RAVEN_NOTIFICATION_ID , notificationId)
126
179
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
+ )
128
186
}
129
187
130
188
@@ -147,6 +205,14 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
147
205
}
148
206
149
207
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
+
150
216
companion object {
151
217
152
218
/*
@@ -163,7 +229,7 @@ class MyFirebaseMessagingService: FirebaseMessagingService() {
163
229
// Get new Instance ID token
164
230
val token: String? = task.result
165
231
token?.let { RavenSdk .setDeviceToken(it) }
166
- })
232
+ })
167
233
}
168
234
169
235
0 commit comments