Notification Android Kotlin - Android O

Notification Android Kotlin - Android O


compileSdkVersion 27
minSdkVersion 15targetSdkVersion 23

import android.app.Notification
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.app.NotificationManager
import android.support.v4.app.NotificationCompat
import android.app.PendingIntent
import android.app.NotificationChannel
import android.os.Build


class MyReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, p1: Intent?) {
        createNotification(context, "Test")
    }

    private fun createNotification(context: Context?, aMessage: String) {
        var notificationManager: NotificationManager = context!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        // There are hardcoding only for show it's just strings        val name = "my_package_channel"        val id = "1001" // The user-visible name of the channel.        val description = "my_package_first_channel" // The user-visible description of the channel.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            val importance = NotificationManager.IMPORTANCE_HIGH            var mChannel = notificationManager.getNotificationChannel(id)
            if (mChannel == null) {
                mChannel = NotificationChannel(id, name, importance)
                mChannel.description = description
                mChannel.enableVibration(true)
                mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
                notificationManager.createNotificationChannel(mChannel)
            }
        }

        val builder : NotificationCompat.Builder = NotificationCompat.Builder(context, id)
        val intent = Intent(context, Main2Activity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP        val pendingIntent : PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
        builder.setContentTitle(aMessage)                           // required                .setSmallIcon(android.R.drawable.ic_popup_reminder) // required                .setContentText(context.getString(R.string.app_name))  // required                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setTicker(aMessage)
                .setVibrate(longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)).priority = Notification.PRIORITY_HIGH        val notification = builder.build()
        notificationManager.notify(1001, notification)
    }
}

Comments

Popular posts from this blog