Created
March 18, 2022 19:20
-
-
Save navczydev/64dd9a8d0f7e6010e044120bd70a1382 to your computer and use it in GitHub Desktop.
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
package com.example.myapplication | |
import android.app.NotificationChannel | |
import android.app.NotificationManager | |
import android.app.PendingIntent.FLAG_IMMUTABLE | |
import android.content.Context | |
import android.os.Build | |
import androidx.core.app.NotificationCompat | |
import androidx.core.app.TaskStackBuilder | |
const val NOTIFICATION_CHANNEL_ID = BuildConfig.APPLICATION_ID + ".channel" | |
/** | |
* @author Nav Singh | |
*/ | |
fun sendNotification(context: Context) { | |
val notificationManager = context | |
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
// We need to create a NotificationChannel associated with our CHANNEL_ID before sending a notification. | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && | |
notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null | |
) { | |
val name = context.getString(R.string.app_name) | |
val channel = NotificationChannel( | |
NOTIFICATION_CHANNEL_ID, | |
name, | |
NotificationManager.IMPORTANCE_DEFAULT | |
) | |
notificationManager.createNotificationChannel(channel) | |
} | |
val intent = MainActivity.newIntent(context.applicationContext) | |
// create a pending intent that opens MainActivity when the user clicks on the notification | |
val stackBuilder = TaskStackBuilder.create(context) | |
.addParentStack(MainActivity::class.java) | |
.addNextIntent(intent) | |
val notificationPendingIntent = stackBuilder | |
.getPendingIntent(getUniqueId(), FLAG_IMMUTABLE) | |
// build the notification object with the data to be shown | |
val notification = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setContentTitle("Title") | |
.setContentText("Content goes here") | |
.setContentIntent(notificationPendingIntent) | |
.setAutoCancel(true) | |
.build() | |
notificationManager.notify(getUniqueId(), notification) | |
} | |
private fun getUniqueId() = ((System.currentTimeMillis() % 10000).toInt()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment