// TODO(developer): Handle FCM messages here. var type = 0 if (remoteMessage.data.isNotEmpty()) { Log.d("TAG", "Message data payload: " + remoteMessage.data) if (remoteMessage.data["type"] != null){ type = remoteMessage.data["type"]?.toInt() ?: 0 } if (type == 3) { val title = remoteMessage.data["title"] ?: "Test Action Notification" val body = remoteMessage.data["body"] ?: "" sendActionNotification(title, body) } } ... }
privatefunsendActionNotification(title: String, body: String) { val notifID = System.currentTimeMillis().hashCode()
val button1Intent = Intent(this, NotificationActionReceiver::class.java) button1Intent.putExtra("notificationId", notifID) button1Intent.putExtra("action", "Action1") val btn1PendingIntent = PendingIntent.getBroadcast( this, notifID, button1Intent, PendingIntent.FLAG_UPDATE_CURRENT )
val button2Intent = Intent(this, NotificationActionReceiver::class.java) button2Intent.putExtra("notificationId", notifID) button2Intent.putExtra("action", "Action2") val btn2PendingIntent = PendingIntent.getBroadcast( this, notifID, button2Intent, PendingIntent.FLAG_UPDATE_CURRENT )
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) val notificationBuilder = NotificationCompat.Builder(this, "default") .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(title) .setContentText(body) .setSound(defaultSoundUri) .addAction(R.drawable.common_alert_button, "Action1", btn1PendingIntent) .addAction(R.drawable.common_alert_button, "Action2", btn2PendingIntent) .setOngoing(true)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channelID = "pushnotificationtest"; val channel = this.createNotificationChannel( notificationManager, channelID, channelID, NotificationManager.IMPORTANCE_HIGH )