Search code examples
androidkotlinbroadcastreceiverandroid-notificationsandroid-pendingintent

How to pass from Activity to BroadcastReceiver() using pendingIntent Kotlin


Hi I am trying to display my temperature value and weather description notification from my WeatherBroadcater BroadcastReceiver using the values from my WeatherNotification activity. However, I was not able to retrieve the values as it was presented null on the BroadcastReceiver. Check how do I do this and thanks in advance

WeatherNotification.kt

private fun setIntent() {
    val intent = Intent(this, WeatherBroadcaster::class.java)

    Log.d("Broadcast", "sending to broadcast Temp: ${weatherResult.temp}, Desc: ${weatherResult.weatherDescription}")
    intent.putExtra("Temp", weatherResult.temp.toString())
    intent.putExtra("WeatherDes", weatherResult.weatherDescription.toString())
    val pendingIntent: PendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

    val timeStart = System.currentTimeMillis()
    val interval = 1000

    alarmManager.setRepeating(
        AlarmManager.RTC_WAKEUP,
        timeStart,interval.toLong(), pendingIntent
    )
}

WeatherBroadcaster.kt

    override fun onReceive(context: Context?, intent: Intent?) {
    val intent = Intent(context, WeatherNotification::class.java)

    val temp: String? = intent.getStringExtra("Temp")
    val desc: String? = intent.getStringExtra("WeatherDes")
    Log.d("Broadcast", "sending to broadcast Temp: $temp, Desc: $desc")

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

    var bitmap = BitmapFactory.decodeResource(context?.resources, R.drawable.pressure)
    var bitmapSun =
        BitmapFactory.decodeResource(context?.resources, R.drawable.sunrise)

    val builder = NotificationCompat.Builder(context!!, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle(temp)
        .setContentText(desc)
        .setLargeIcon(bitmapSun)
        .setStyle(NotificationCompat.BigPictureStyle().bigPicture(bitmap))
        .setContentIntent(pendingIntent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

    with(NotificationManagerCompat.from(context)) {
        notify(notificationId, builder.build())
    }
}

LogCat

2020-10-08 16:56:13.774 6462-18591/com.sgtech.ict3104.racecar D/Broadcast: sending to broadcast Temp: 30.97°C, Desc: light rain

2020-10-08 16:56:33.227 6462-6462/com.sgtech.ict3104.racecar D/Broadcast: sending to broadcast Temp: null, Desc: null

Solution

  • There is typo on the key for the temperature and you are initialization a new intent on the receiver

    override fun onReceive(context: Context?, intent: Intent?) {
        val intent = Intent(context, WeatherNotification::class.java)
        //...
    }
    

    That val intent is a new Intent you are creating from the context argument on the method signature. What you need is to examine the intent argument instead.

    override fun onReceive(context: Context?, intent: Intent?) {
        //the intent is now the same as in the argument, the same received
        intent ?: return
        val temp: String? = intent.getStringExtra("temp")
        val desc: String? = intent.getStringExtra("WeatherDes")
        Log.d("Broadcast", "sending to broadcast Temp: $temp, Desc: $desc")
        //...
    }