I have a notification in my Wear OS application with an action, and I've been searching how to send a post request when the notification action is triggered. Is there a way to do this?
I know that Android Wear 2.0 has Internet capabilities, and I've seen examples of opening a browser with a notification, but nothing for sending an HTTP request.
NotificationManager notificationManager = (NotificationManager) context.GetSystemService(Context.NotificationService);
Intent respondIntent = new Intent(context, typeof(AlarmReceiver));
PendingIntent respontPendingIntent = PendingIntent.GetActivity(context, 0, respondIntent, PendingIntentFlags.UpdateCurrent);
Notification.Action action = new Notification.Action(Resource.Drawable.generic_confirmation,"hello", respontPendingIntent);
var noti = new Notification.Builder(context)
.SetContentTitle("Title").SetContentText("content text")
.SetSmallIcon(Resource.Drawable.pills)
.AddAction(action);
notificationManager.Notify(notificationId, noti.Build());
That's what I have so far (currently the action doesn't do anything).
There are many different ways to accomplish this, but the common theme is that a PendingIntent
is just a hook to some other component of your app where the work actually happens. You don't put your HTTP request in the Intent
, as it were, you put it in the app component that the Intent
invokes.
In your case, rather than opening the AlarmReceiver
Activity
from your 'PendingIntent', you'll probably want to launch something like an IntentService
or a BroadcastReceiver
instead and put your network operations in there. If you also need to open AlarmReceiver
, you can do that in your BroadcastReceiver
as well.
Which component to use depends on the bigger picture of what your app is doing, and is beyond the scope of a StackOverflow answer. But this is pretty basic Android app-architecture stuff, really.