-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathP37_a.kt
101 lines (83 loc) · 3.65 KB
/
P37_a.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package alpha.dex.one
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.RemoteInput
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.media.AudioAttributes
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.RemoteViews
import androidx.annotation.RequiresApi
class Notif : AppCompatActivity() {
lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
lateinit var btnNotify: Button
lateinit var remoteCollapsedViews: RemoteViews
lateinit var remoteExpandedViews: RemoteViews
lateinit var pendingIntent: PendingIntent
lateinit var soundUri: Uri
lateinit var audioAttr: AudioAttributes
lateinit var remoteInput: RemoteInput
private val channelId = "My channel Id"
private val description = "Test Notification"
private val title = "Notification"
val myKey = "Remote Key"
val notificationId = 1234
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notif)
btnNotify = findViewById(R.id.btnNotifyy)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
btnNotify.setOnClickListener {
val intent = Intent(this, NotifMain::class.java)
pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
soundUri = Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + applicationContext.packageName + "/" + R.raw.ringtone
)
audioAttr = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
myNotificationChannel()
notificationManager.notify(notificationId, builder.build())
}
}
private fun myNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel =
NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.GREEN
notificationChannel.enableVibration(false)
notificationChannel.setSound(soundUri, audioAttr)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(this, channelId)
.setSmallIcon(R.drawable.twotone_notifications_24)
.setContentTitle(title)
.setContentText(description)
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.img))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
} else {
builder = Notification.Builder(this)
.setSmallIcon(R.drawable.twotone_notifications_24)
.setContentTitle(title)
.setContentText(description)
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.img))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
}
}
}