Created
April 19, 2021 12:10
-
-
Save kariot/90b0e6e1399dd58f52b252c33f29ffa9 to your computer and use it in GitHub Desktop.
Creates Notification from FCM Service With Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun showNotification(remoteMessage: RemoteMessage) { | |
val intent = Intent(this, SplashActivity::class.java) | |
val pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_ONE_SHOT) | |
val channelId = getString(R.string.default_notification_channel_id) | |
val defaultSoundUri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | |
val notificationBuilder: NotificationCompat.Builder = | |
NotificationCompat.Builder(this, channelId) | |
.setSmallIcon(R.drawable.notification_icon) | |
.setContentTitle(remoteMessage.notification!!.title) | |
.setContentText(remoteMessage.notification!!.body) | |
.setAutoCancel(true) | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent) | |
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
// Since android Oreo notification channel is needed. | |
// Since android Oreo notification channel is needed. | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
val channel = NotificationChannel(channelId, "Default Channel", NotificationManager.IMPORTANCE_HIGH) | |
notificationManager.createNotificationChannel(channel) | |
} | |
if (remoteMessage.notification?.imageUrl != null) { | |
val url = remoteMessage.notification?.imageUrl | |
Glide.with(this).asBitmap().load(url).into(object : CustomTarget<Bitmap>() { | |
override fun onResourceReady(resource: Bitmap, transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?) { | |
val bitmap: Bitmap = resource | |
notificationBuilder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(bitmap).bigLargeIcon(null)).setLargeIcon(bitmap) | |
notificationManager.notify(123, notificationBuilder.build()) | |
} | |
override fun onLoadCleared(placeholder: Drawable?) { | |
} | |
}) | |
return | |
} | |
notificationManager.notify(123, notificationBuilder.build()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment