Harsh Prasad
Harsh Prasad

Reputation: 23

How can I handle action button in notification?

Description : I am using Kotlin, Jetpack compose, MVVM, hilt, FCM. My notification will show two buttons "accept" and "ignore". I have a function in one of my viewmodel which accepts a Boolean parameter. I want that on click of any buttons that particular screen should be displayed and that function to be called.

What I tried and where have I reached : So right now in my NotificationReciever I have a intent through which I can open that screen using deepLink.

Approach 1 : I think if I re-write my deeplink pattern by addidg one more param and then in my route, I can check if that param is present then perform some function. For eg. right now my deeplink uri patterns is https://abcd.com/thatRoute/{xyid}, I was thinking to make it https://abcd.com/thatRoute/{xyid}/{trueOrFalse}.

Approach 2 : I can catch intent in my MainActivity and perform something with it. The problem with this approach is I don't know how to create that particular viewModel in my MainActivity.

Approach 3 : Creating a broadcast receiver in which I can inject that viewModel and then call that function.

I am really confused if any of this will work. I am very new to android please, if I mistakenly wrote anything wrong then ignore please. If you know or can attach a doc in which they have shown how do you call a function of a viewModel from click of notification button please reply or attach.

EXAMPLE

I have a route User in which I have User Screen and UserViewModel. UserViewModel has a function acceptOrRejectFreindRequest(Boolean). User A got a notification that "user B has sent you friend request". Now user B can click "accept" or "reject". Now upon clicking any button acceptOrRejectFreindRequest(Boolean) this function should be called.

Upvotes: 1

Views: 46

Answers (0)

Related Questions