Untitled
unknown
kotlin
4 years ago
2.4 kB
5
Indexable
package com.example.appsem5 import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.Build import android.os.Bundle import androidx.core.app.NotificationCompat import kotlin.properties.Delegates class AlarmReceiver(val contentTitle: String = "", val contentText: String = "") : BroadcastReceiver() { var contentTitleFromConstructor : String by Delegates.notNull() var contentTextFromConstructor : String by Delegates.notNull() init { contentTitleFromConstructor = this.contentTitle contentTextFromConstructor = this.contentText } override fun onReceive(context: Context?, intent: Intent?) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Create the NotificationChannel val name = "Alarme" val descriptionText = "Detalhes do Alarme" val importance = NotificationManager.IMPORTANCE_DEFAULT val mChannel = NotificationChannel("1234", name, importance) mChannel.description = descriptionText val notificationManager = context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(mChannel) } // Create the notification to be shown val mBuilder = NotificationCompat.Builder(context!!, "1234") .setSmallIcon(R.drawable.ic_launcher_background) .setContentTitle(contentTitleFromConstructor) .setContentText(contentTextFromConstructor) .setAutoCancel(true) .setContentIntent( PendingIntent.getActivity( context, 0, Intent(context, AfterNotification::class.java), 0 ) ) .setPriority(NotificationCompat.PRIORITY_DEFAULT) // Get the Notification manager service val am = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager // Generate an Id for each notification val id = System.currentTimeMillis() / 1000 // Show a notification am.notify(id.toInt(), mBuilder.build()) } }
Editor is loading...