PendingIntent doesn't work in android studio

306 views Asked by At

This is my code:

     package com.example.notifsimple

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val CHNANNEL_ID = "channel_99"
    val id = 123456

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnNotif.setOnClickListener{

            val importance = NotificationManager.IMPORTANCE_HIGH
            val mChannel = NotificationChannel(CHNANNEL_ID, "اسم الفناه", importance) //

            val notification = NotificationCompat.Builder(this,CHNANNEL_ID)//
                .setSmallIcon(R.drawable.android)
                .setContentTitle("New Animal")
                .setContentText("a new Cowala has been dicovered in the Amazons!")
                .build()


              //val intent = Intent(this,MainActivity::class.java)
             // val pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
             // notification.contentIntent = pendingIntent

            val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            mNotificationManager.createNotificationChannel(mChannel)

            mNotificationManager.notify(id, notification)
        }
    }
}

and the Code that i COMMENTED is the problem. when I UNcomment it the app will just give me the notification above but then exit app and tell me that "The app stopped working" and when I click on the notification doesn't do anything

1

There are 1 answers

1
wuujcik On

Try this:

class MainActivity : AppCompatActivity() {

  val CHNANNEL_ID = "channel_99"
  val id = 123456

  @RequiresApi(Build.VERSION_CODES.O)
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    btnNotif.setOnClickListener{

      val importance = NotificationManager.IMPORTANCE_HIGH
      val mChannel = NotificationChannel(CHNANNEL_ID, "اسم الفناه", importance) //
      val intent = Intent(applicationContext,MainActivity::class.java)
      val pendingIntent = PendingIntent.getActivity(applicationContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT) // use applicationContext, not `this`
      
      val notification = NotificationCompat.Builder(this,CHNANNEL_ID)//
        .setSmallIcon(R.drawable.android)
        .setContentTitle("New Animal")
        .setContentText("a new Cowala has been dicovered in the Amazons!")
        .setContentIntent(pendingIntent) // this is how I set the pending intent and it works
        .build()

      val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
      mNotificationManager.createNotificationChannel(mChannel)

      mNotificationManager.notify(id, notification)
    }
  }
}