1

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
odh1995
  • 13
  • 3

1 Answers1

0

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")
    //...
}
cutiko
  • 9,887
  • 3
  • 45
  • 59
  • 1
    Thanks so much for the help! Able to check with you if I were to pass data to another activity from the receiver as well as display my temperature on the notification, do I have initialize a new intent after i perform "getStringExtra"? Thanks so much once again! – odh1995 Oct 08 '20 at 14:07
  • Yes you need a new intent because the previous intent doesn't have a destination, remember the intent constructor can receive 2 arguments, context and the destination class. So create a new one to the new activity and forward the extras, good thing is you don't need to get them one by one, take a look at this https://stackoverflow.com/a/13686256/4017501 BTW you can also upvote the answer, thanks for marking it as the solution – cutiko Oct 08 '20 at 14:42
  • 1
    Thank you so much for the detailed explanation as well as the link. Unfortunately, as much as I would love to upvote the answer my reputation right now is currently sitting at 2 as I've only created an stackoverflow account today. Once again thanks so much and hope you have a great day! – odh1995 Oct 08 '20 at 14:52