Skip to content

Instantly share code, notes, and snippets.

@PrashamTrivedi
Created April 20, 2017 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashamTrivedi/791d73590070882550828995f6d129e9 to your computer and use it in GitHub Desktop.
Save PrashamTrivedi/791d73590070882550828995f6d129e9 to your computer and use it in GitHub Desktop.
//Functions.kt
public fun Context.setNotification(id: Int = 0, builderMethod: NotificationCompat.Builder.() -> Any) {
val builder = NotificationCompat.Builder(this)
builder.apply {
builderMethod()
}
notificationManager().notify(id, builder.build())
}
//FCM Service.kt
this@MyFCMService.setNotification(2488, {
setContentTitle(it.title)
setContentText(it.body)
setColor(R.color.colorPrimary)
setSmallIcon(R.drawable.ic_notification_icon)
setAutoCancel(true)
val bigTextStyle = NotificationCompat.BigTextStyle(this)
bigTextStyle.setBigContentTitle(it.title)
bigTextStyle.bigText(it.body)
bigTextStyle.setSummaryText(it.body)
setStyle(bigTextStyle)
val intent = Intent(this@MyFCMService, MainActivity::class.java)
intent.putExtras(remoteMessage.data.extractBundle())
val stackBuilder: TaskStackBuilder = TaskStackBuilder.create(this@MyFCMService)
stackBuilder.addNextIntent(intent)
val pendingIntent = stackBuilder.getPendingIntent(2488, PendingIntent.FLAG_UPDATE_CURRENT)
setContentIntent(pendingIntent)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment