Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
243 lines (203 sloc) 7.5 KB
package com.hellohasan.android_firebase_notification.notification
import android.app.*
import android.content.ComponentName
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.Ringtone
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.text.Html
import android.text.TextUtils
import android.util.Patterns
import androidx.core.app.NotificationCompat
import com.hellohasan.android_firebase_notification.R
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date
import com.hellohasan.android_firebase_notification.notification.Configuration.Companion.NOTIFICATION_ID
class NotificationUtils(private val mContext: Context) {
private val channelId = mContext.getString(R.string.default_notification_channel_id)
@JvmOverloads
fun showNotificationMessage(
title: String,
message: String,
timeStamp: String,
intent: Intent,
imageUrl: String? = null) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return
// notification icon
val icon = R.mipmap.ic_launcher
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
val resultPendingIntent = PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
)
val mBuilder = NotificationCompat.Builder(
mContext,
channelId
)
val alarmSound = Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.packageName + "/raw/notification"
)
if (!TextUtils.isEmpty(imageUrl)) {
if (imageUrl != null && imageUrl.length > 4 && Patterns.WEB_URL.matcher(imageUrl).matches()) {
val bitmap = getBitmapFromURL(imageUrl)
if (bitmap != null) {
showBigNotification(
bitmap,
mBuilder,
icon,
title,
message,
timeStamp,
resultPendingIntent,
alarmSound
)
} else {
showSmallNotification(
mBuilder,
icon,
title,
message,
timeStamp,
resultPendingIntent,
alarmSound
)
}
}
} else {
showSmallNotification(
mBuilder,
icon,
title,
message,
timeStamp,
resultPendingIntent,
alarmSound
)
playNotificationSound()
}
}
private fun showSmallNotification(
mBuilder: NotificationCompat.Builder,
icon: Int,
title: String,
message: String,
timeStamp: String,
resultPendingIntent: PendingIntent,
alarmSound: Uri
) {
val inboxStyle = NotificationCompat.InboxStyle()
inboxStyle.addLine(message)
val notification: Notification
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.resources, icon))
.setContentText(message)
.build()
val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
"Firebase Notification channel for sample app",
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(Configuration.NOTIFICATION_ID, notification)
}
private fun showBigNotification(
bitmap: Bitmap,
mBuilder: NotificationCompat.Builder,
icon: Int,
title: String,
message: String,
timeStamp: String,
resultPendingIntent: PendingIntent,
alarmSound: Uri
) {
val bigPictureStyle = NotificationCompat.BigPictureStyle()
bigPictureStyle.setBigContentTitle(title)
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString())
bigPictureStyle.bigPicture(bitmap)
val notification: Notification
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.resources, icon))
.setContentText(message)
.build()
val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, "Firebase Notification channel for sample app", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(Configuration.NOTIFICATION_ID_BIG_IMAGE, notification)
}
/**
* Downloading push notification image before displaying it in
* the notification tray
*/
fun getBitmapFromURL(strURL: String): Bitmap? {
try {
val url = URL(strURL)
val connection = url.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val input = connection.inputStream
return BitmapFactory.decodeStream(input)
} catch (e: IOException) {
e.printStackTrace()
return null
}
}
// Playing notification sound
fun playNotificationSound() {
try {
val alarmSound = Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.packageName + "/raw/notification"
)
val r = RingtoneManager.getRingtone(mContext, alarmSound)
r.play()
} catch (e: Exception) {
e.printStackTrace()
}
}
companion object {
fun getTimeMilliSec(timeStamp: String): Long {
val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
try {
val date = format.parse(timeStamp)
return date.time
} catch (e: ParseException) {
e.printStackTrace()
}
return 0
}
}
}
You can’t perform that action at this time.