Issue
I'm setting up an application with some notifications that are triggered by some Alarm. In order to set different notification, I take the current millis time as Id. However, when I'm passing it to intent through extra, it always receive the same value.
Here's my code, it'll be clearer : In mainActivity we've :
class MainActivity : AppCompatActivity() {
private lateinit var alarmManager: AlarmManager
private lateinit var pendingIntent: PendingIntent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createNotificationChannel() //create a channel
}
//Set a notification that will be triggered in a given time in ms.
//you can pass a title/description and Id in parameter
private fun setNotification(timeMS: Long, title: String, description: String, id: Int){
alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, ReminderBroadcastReceiver::class.java)
intent.putExtra("title", title)
intent.putExtra("description", description)
intent.putExtra("id", id)
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeMS, pendingIntent)
}
//this button trigger a basics notification in 1 sec
//here we use an id based on current time. We may use some parsed part of the corresponding deadline later.
fun triggerNotification(view:View) {
var id = System.currentTimeMillis().toInt()
setNotification(System.currentTimeMillis()+1000, "foo", "ouafouaf", id)
}
Then in ReminderBroadcastReceiver :
class ReminderBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
var channelId = "remindersChannel"
var title = intent!!.getStringExtra("title")
var content = intent!!.getStringExtra("description")
var notifId = intent!!.getIntExtra("id", 0)
val intent2 = Intent(context, MainActivity::class.java)
intent2!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_IMMUTABLE)
val notifBuilder = NotificationCompat.Builder(context!!, channelId)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title + notifId.toString())
.setContentText(content)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(notifId, notifBuilder.build())
}
}
My problem is that, if I click twice on the button at different time, the id received doesn't change. When I put System.currentTimeMillis().toInt()
directly in notify's parameter, it works well.
Any Idea how to fix this?
Solution
The second parameter to PendingIntent.getBroadcast() needs to be different for each different notification.
(just use comment from CommonsWare to answer and close this thread)
Answered By - L.DZ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.