|
package com.test.uploadtos3 |
|
|
|
import android.annotation.TargetApi |
|
import android.app.NotificationChannel |
|
import android.app.NotificationManager |
|
import android.app.PendingIntent |
|
import android.content.Context |
|
import android.content.ContextWrapper |
|
import android.content.Intent |
|
import android.graphics.Color |
|
import android.media.RingtoneManager |
|
import android.os.Build |
|
import android.widget.Toast |
|
import androidx.core.app.NotificationCompat |
|
|
|
class NotificationUtils(base: Context) : ContextWrapper(base) { |
|
val MYCHANNEL_ID = "App Alert Notification ID" |
|
val MYCHANNEL_NAME = "App Alert Notification" |
|
|
|
private var manager: NotificationManager? = null |
|
|
|
init { |
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
createChannels() |
|
} |
|
Toast.makeText(base, "Alarm triggered", Toast.LENGTH_SHORT).show() |
|
} |
|
|
|
// Create channel for Android version 26+ |
|
@TargetApi(Build.VERSION_CODES.O) |
|
private fun createChannels() { |
|
val channel = |
|
NotificationChannel(MYCHANNEL_ID, MYCHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH) |
|
channel.enableVibration(true) |
|
|
|
getManager().createNotificationChannel(channel) |
|
} |
|
|
|
// Get Manager |
|
fun getManager(): NotificationManager { |
|
if (manager == null) manager = |
|
getSystemService(NOTIFICATION_SERVICE) as NotificationManager |
|
return manager as NotificationManager |
|
} |
|
|
|
fun getNotificationBuilder(): NotificationCompat.Builder { |
|
val intent = Intent(this, AlarmActivity::class.java).apply { |
|
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK |
|
} |
|
var pendingIntent: PendingIntent? = null |
|
pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
|
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE) |
|
} else { |
|
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT) |
|
} |
|
return NotificationCompat.Builder(applicationContext, MYCHANNEL_ID) |
|
.setContentTitle("Alarm!") |
|
.setContentText("Your AlarmManager is working.") |
|
.setSmallIcon(R.drawable.ic_launcher_background) |
|
.setColor(Color.YELLOW) |
|
.setContentIntent(pendingIntent) |
|
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) |
|
.setAutoCancel(true) |
|
} |
|
} |