Created
September 6, 2023 09:48
-
-
Save amoozeshbebin/662035962afca32c6213deadd1db5477 to your computer and use it in GitHub Desktop.
Notification Permission In Android/kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MainActivity.kt: | |
import android.Manifest | |
import android.app.NotificationChannel | |
import android.app.NotificationManager | |
import android.content.Context | |
import android.content.pm.PackageManager | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.Button | |
import androidx.core.app.NotificationCompat | |
class MainActivity : AppCompatActivity() { | |
private lateinit var button: Button | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
button = findViewById(R.id.button_send_notification) | |
button.setOnClickListener { | |
sendNotification() | |
} | |
} | |
private fun sendNotification() { | |
// Create a notification channel | |
val channel = NotificationChannel( | |
"my_channel", | |
"My Notification Channel", | |
NotificationManager.IMPORTANCE_DEFAULT | |
) | |
channel.description = "This is my notification channel" | |
channel.setAllowBubbles(false) | |
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
notificationManager.createNotificationChannel(channel) | |
// Request permission | |
val permission = Manifest.permission.POST_NOTIFICATIONS | |
val requestCode = 1 | |
requestPermissions(arrayOf(permission), requestCode) | |
// Create notification | |
val notificationBuilder = NotificationCompat.Builder(this, "my_channel") | |
.setContentTitle("My Notification") | |
.setContentText("This is my notification") | |
.setSmallIcon(R.drawable.baseline_circle_notifications_24) | |
// Send the notification | |
val notificationManager1 = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
notificationManager1.notify(1, notificationBuilder.build()) | |
} | |
override fun onRequestPermissionsResult( | |
requestCode: Int, | |
permissions: Array<out String>, | |
grantResults: IntArray | |
) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
if (requestCode == 1 && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
sendNotification() | |
} | |
} | |
} | |
____________________________________________________________________________________________ | |
activity_main.xml: | |
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/button_send_notification" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="40dp" | |
android:onClick="sendNotification" | |
android:text="Send Notification" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment