I have a notification that has an action button. IFF the action is clicked on, it will open up a specific view in my app and the notification will dismiss itself. I know for dismissing notification on notification click, I can simply setAutoCancel(true) and it will does the trick. However, since action is added by NotificationCompat.Builder().addAction, notification will not dismiss itself without a proper notificationManager.cancel(id), so I try to put extra in my intent to pass notification id, but now nothing is working correctly after that. Action now brings the app to foreground if minimize and does nothing if it's already in foreground (instead of my destination) and receiver doesn't seem to have receieved anything.
Heres my code in NotificationPublisher.kt
val intent = Intent(Intent.ACTION_VIEW).apply {
//a uri to destination fragment
data = intents.getDestination()
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
intent.putExtra("notification_id", idHashCode)
val pendingIntent = PendingIntent.getActivity(context,idHashCode,intent,PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
return NotificationCompat.Builder(context,CHANNEL)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(subTitle)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(LongArray(2) { 1000 })
.setNumber(1)
.setOnlyAlertOnce(true)
.setStyle(NotificationCompat.BigTextStyle())
.setAutoCancel(true)
.addAction(0,getActionTextWithColor(context, R.string.go_to_destination, R.color.primary), pendingIntent)
NotificationReceiver.kt
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getIntExtra("notification_id", -1)
if(id > 0){
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.cancel(id)
}
}
}
AndroidManifest
<receiver android:name="com.myApp.notification.NotificationReceiver"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<data
android:host="myApp.com"
android:pathPrefix="/destination"
android:scheme="myApp" />
</intent-filter>
</receiver>
Any thoughts on how I can make this work? Or if this doesn't make sense (I'm not very experience in Android), is there another approach I can try?
I tried puting extra in NotificationCompat.Builder but that also doesn't work Maybe I can call manger.cancelAll() to get rid of all notification but that seems like a very desperate attempt and I feel like there has to be a smart/correct way of doing this
PendingIntent.getActivitywill set up to usestartActivity()to pass the enclosedIntent.If you really want to go to the
BroadcastReceiver, you need to usePendingIntent.getBroadcast().If instead, you really want to go bring the
Activityto foreground, you should register the<intent-filter>on theActivity. You can then get the data from theIntentby looking at theintentproperty inActivity.onCreate().