Skip to content

Instantly share code, notes, and snippets.

@Judas
Created September 22, 2021 07:21
Show Gist options
  • Save Judas/7266ff2d11e5b726a95ccf7b590d4e32 to your computer and use it in GitHub Desktop.
Save Judas/7266ff2d11e5b726a95ccf7b590d4e32 to your computer and use it in GitHub Desktop.
iAdvize Android notification
class NotificationService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
IAdvizeSDK.notificationController.registerPushToken(token)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (IAdvizeSDK.notificationController.isIAdvizePushNotification(remoteMessage.data)
&& !IAdvizeSDK.chatboxController.isChatboxPresented()
) {
// This is an iAdvize notification and the chatbox is not open => show notification
sendNotification(remoteMessage.data["content"] ?: "A file has been sent to you")
}
}
private fun sendNotification(text: String) {
val intent = Intent(this, RootActivity::class.java)
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, FLAG_ONE_SHOT)
val channelId = "sample-channel-id"
val defaultSoundUri = RingtoneManager.getDefaultUri(TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setColor(0xff00ff00.toInt())
.setContentTitle("iAdvize Sample")
.setContentText(text)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (SDK_INT >= O) {
notificationManager.createNotificationChannel(
NotificationChannel(
channelId,
channelId,
NotificationManager.IMPORTANCE_DEFAULT
)
)
}
notificationManager.notify(0, notificationBuilder.build())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment