3

I'm using Kotlin and Android Studio to try and push a Notification in a test app. I followed the instructions in the Android Developers site on How to create a Notification (https://developer.android.com/training/notify-user/build-notification) but I seem to be doing something wrong as my Notification is not showing anywhere. Here's my code:

 val intent = Intent(context, Profile::class.java)
            val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);
            val builder = NotificationCompat.Builder(this.context)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
                .setPriority(NotificationCompat.PRIORITY_MAX)
            builder.setContentIntent(pendingIntent).setAutoCancel(true)
            val mNotificationManager = message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            with(mNotificationManager) {
                notify(123, builder.build())

I really don't know what I'm missing so I would be grateful for any help.

1
  • Welcome to Stack Overflow. You should post the link of the website you are referring to in the question. Commented Jan 5, 2020 at 12:18

3 Answers 3

6

According to your code, you are not creating a Notification channel.

Notification channel is necessary from Android Oreo and above

So if you are running the app Android O and above devices without a notification channel, your notification won't show up.

Create Notification Channel

fun createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelId = "all_notifications" // You should create a String resource for this instead of storing in a variable
            val mChannel = NotificationChannel(
                channelId,
                "General Notifications",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            mChannel.description = "This is default channel used for all other notifications"

            val notificationManager =
                mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(mChannel)
        }
    }

Then create a notification using the same channelId while creating a notification.

createNotificationChannel()
val channelId = "all_notifications" // Use same Channel ID
val intent = Intent(context, Profile::class.java)
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);
val builder = NotificationCompat.Builder(this.context, channelId) // Create notification with channel Id
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!")
    .setPriority(NotificationCompat.PRIORITY_MAX)
builder.setContentIntent(pendingIntent).setAutoCancel(true)
val mNotificationManager =
    message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(mNotificationManager) {
    notify(123, builder.build())

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Will look into it. Thanks!
1

try something like

val intent = Intent(context, Profile::class.java)
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0)

val mNotificationManager =
    message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Don't forget this check

    val channel = NotificationChannel (
        channelId,
        "my_notification",
        NotificationManager.IMPORTANCE_HIGH
    )

    channel.enableLights(true)
    channel.lightColor = Color.GREEN
    channel.enableVibration(false)

    
    val builder = NotificationCompat.Builder(this.context, "channel_id")
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_MAX)
    builder.setContentIntent(pendingIntent).setAutoCancel(true)

    mNotificationManager.createNotificationChannel(channel)//Notice this
    mNotificationManager.notify(123, builder.build())
}

else {

    //Create Notifcation for below Oreo


}

Does your Logcat say something like:

E/NotificationManager: notifyAsUser: tag=null, id=123, user=UserHandle{0}

Also have a look at your line:

val intent = Intent(context, Profile::class.java)

IF all else fails create a test Actvity with hello world.

And then make the line look like:

val intent = Intent(context, testActivity::class.java)

Remember to add the activity to your Manifest

The following line

 val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);

should not have a semi colon. Kotin doesn't need this. Is Profile your Class or System Class?

Comments

0

Try out this method and you can pass message and title dynamically :

private fun showNotification(title: String?, body: String?) {
    val intent = Intent(this, Profile::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT)

    val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent)

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(0, notificationBuilder.build())
}

6 Comments

Hi! Thanks for the answer but this doesn't work either
Try to change the notificationId to a positive non zero value in this string: notificationManager.notify(0, notificationBuilder.build())
hope you have changed the id of notifications
Thanks for the answer, tried it and still wont work @easy_breezy
do you mean that i should make sure that i dont have two notifications with the same id @haresh?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.