Created
August 21, 2019 18:46
-
-
Save SurajBahadur/de8c72b63679cd81e591618381ebc461 to your computer and use it in GitHub Desktop.
Android Notification With Big Picture Style
This file contains hidden or 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
package com.app.base.firebase | |
import Preferences | |
import android.annotation.SuppressLint | |
import android.annotation.TargetApi | |
import android.app.NotificationChannel | |
import android.app.NotificationManager | |
import android.app.PendingIntent | |
import android.content.Context | |
import android.content.Intent | |
import android.graphics.Bitmap | |
import android.graphics.BitmapFactory | |
import android.graphics.Color | |
import android.graphics.drawable.Drawable | |
import android.media.RingtoneManager | |
import android.os.AsyncTask | |
import android.os.Build | |
import android.util.Log | |
import androidx.annotation.RequiresApi | |
import androidx.core.app.NotificationCompat | |
import com.app.base.ui.dashboard.DashboardActivity2 | |
import com.app.base.utils.Constants | |
import com.app.base.utils.saveValue | |
import com.google.firebase.messaging.FirebaseMessagingService | |
import com.google.firebase.messaging.RemoteMessage | |
import com.squareup.picasso.Picasso | |
import com.squareup.picasso.Target | |
import java.io.IOException | |
import java.io.InputStream | |
import java.net.HttpURLConnection | |
import java.net.MalformedURLException | |
import java.net.URL | |
import java.util.* | |
class MyFirebaseMessagingService : FirebaseMessagingService() { | |
private var notificationManager: NotificationManager? = null | |
companion object { | |
private val ADMIN_CHANNEL_ID = "admin_channel" | |
} | |
@SuppressLint("WrongThread") | |
override fun onMessageReceived(remoteMessage: RemoteMessage?) { | |
super.onMessageReceived(remoteMessage) | |
Log.d("Message", " " + remoteMessage) | |
//sendMyNotification(remoteMessage!!.notification!!.body, remoteMessage) | |
//.setContentTitle(remoteMessage.notification!!.title) //the "title" value you sent in your notification | |
//.setContentText(remoteMessage.data["message"]) //ditto | |
generatePictureStyleNotification(this, remoteMessage!!.notification!!.title.toString(), remoteMessage.data["message"].toString(), remoteMessage.data["largeIcon"].toString()).execute() | |
} | |
override fun onNewToken(token: String?) { | |
super.onNewToken(token) | |
Log.d("Token", " " + token) | |
Preferences.prefs!!.saveValue(Constants.FCM_TOKEN, token) | |
} | |
@RequiresApi(api = Build.VERSION_CODES.O) | |
private fun setupChannels() { | |
val adminChannelName = "Global channel" | |
val adminChannelDescription = "Notifications sent from the app admin" | |
val adminChannel: NotificationChannel | |
adminChannel = NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW) | |
adminChannel.description = adminChannelDescription | |
adminChannel.enableLights(true) | |
adminChannel.lightColor = Color.RED | |
adminChannel.enableVibration(true) | |
if (notificationManager != null) { | |
notificationManager!!.createNotificationChannel(adminChannel) | |
} | |
} | |
inner class generatePictureStyleNotification(private val mContext: Context, private val title: String, private val message: String, private val imageUrl: String) : AsyncTask<String, Void, Bitmap>() { | |
override fun doInBackground(vararg params: String): Bitmap? { | |
val `in`: InputStream | |
try { | |
val url = URL(this.imageUrl) | |
val connection = url.openConnection() as HttpURLConnection | |
connection.setDoInput(true) | |
connection.connect() | |
`in` = connection.getInputStream() | |
return BitmapFactory.decodeStream(`in`) | |
} catch (e: MalformedURLException) { | |
e.printStackTrace() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
return null | |
} | |
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) | |
override fun onPostExecute(largeIcon: Bitmap) { | |
super.onPostExecute(largeIcon) | |
//On click of notification it redirect to this Activity i.e. MainActivity | |
val intent = Intent(mContext, DashboardActivity2::class.java) | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | |
intent.putExtra("Notification", true); | |
val pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT) | |
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
//Setting up Notification channels for android O and above | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
setupChannels() | |
} | |
val notificationId = Random().nextInt(60000) | |
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) | |
val notificationBuilder = NotificationCompat.Builder(mContext, ADMIN_CHANNEL_ID) | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
notificationBuilder | |
.setSmallIcon(com.app.base.R.drawable.ic_background) //a resource for your custom small icon | |
.setContentTitle(title) //the "title" value you sent in your notification | |
.setContentText(message) //ditto | |
.setLargeIcon(largeIcon) | |
.setStyle(NotificationCompat.BigPictureStyle().bigPicture(largeIcon)) | |
.setAutoCancel(true) //dismisses the notification on click | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent) | |
} else { | |
notificationBuilder | |
.setSmallIcon(com.app.base.R.drawable.ic_background) //a resource for your custom small icon | |
.setContentTitle(title) //the "title" value you sent in your notification | |
.setContentText(message) //ditto | |
.setContentText(message) | |
.setLargeIcon(largeIcon) | |
.setStyle(NotificationCompat.BigPictureStyle().bigPicture(largeIcon)) | |
.setAutoCancel(true) //dismisses the notification on click | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent) | |
} | |
notificationManager!!.notify(notificationId /* ID of notification */, notificationBuilder.build()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment