Notifications

Present short messages to the user.

  1. Define a notificationBuilder and pass a small icon, content title, and content text as follows:

        val notificationBuilder = NotificationCompat.Builder(context) 
                .setSmallIcon(R.drawable.icon) 
                .setContentTitle("Hello!") 
                .setContentText("We love Android!")
  1. Define Intent for activity of your application:
        val result = Intent(context, MyActivity::class.java)
  1. Now define the stack builder object that will contain the back stack for the activity as follows:
        val builder = TaskStackBuilder.create(context)
  1. Add back stack for the intent:
        builder.addParentStack(MyActivity::class.java)
  1. Add intent at the top of the stack:
        builder.addNextIntent(result) 
        val resultPendingIntent = builder.getPendingIntent( 
          0, 
          PendingIntent.FLAG_UPDATE_CURRENT )Define ID for the   
          notification and notify:
        val id = 0 
        notificationBuilder.setContentIntent(resultPendingIntent) 
        val manager = getSystemService(NOTIFICATION_SERVICE) as
        NotificationManager 
        manager.notify(id, notificationBuilder.build())

results matching ""

    No results matching ""