I'm new on SO, My problem is sending notification with image. I setup OneSignal plugin in wordpress website and created a sample android application. I am receiving notification but without large image. In Android I'm getting this json in notification class.
{custom={"u":"https:my post url","i":"fe37dc8e-8e6c-4f00-9d69-a"}, alert=Kapil Sharma Show वापस आ रहा है नए मेहमान के साथ, जानिए कैसे हुआ शूट, title=My Website}
This is my method to show notification.
private fun sendNotification(notification: RemoteMessage.Notification?, data: Map<String, String>) {
val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val notificationBuilder = NotificationCompat.Builder(this, "my_channel")
.setContentTitle(notification!!.title)
.setContentText(notification.body)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setContentInfo(notification.title)
.setLargeIcon(icon)
.setColor(Color.RED)
.setLights(Color.RED, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.mipmap.ic_launcher)
try {
val picture_url = data["picture_url"]
if (picture_url != null && "" != picture_url) {
val url = URL(picture_url)
val bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream())
notificationBuilder.setStyle(
NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.body)
)
}
} catch (e: IOException) {
e.printStackTrace()
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Notification Channel is required for Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"my_channel", "my_channel_name", NotificationManager.IMPORTANCE_DEFAULT
)
channel.description = "channel description"
channel.setShowBadge(true)
channel.canShowBadge()
channel.enableLights(true)
channel.lightColor = Color.RED
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0, notificationBuilder.build())
}
I just want to send notification with image when new post added in my wordpress site.