post - 在 Android Wear / Wear OS 上的通知操作中发送发布请求
问题描述
我的 Wear OS 应用程序中有一个带有操作的通知,并且我一直在搜索如何在触发通知操作时发送发布请求。有没有办法做到这一点?
我知道 Android Wear 2.0 具有 Internet 功能,并且我看到了打开带有通知的浏览器的示例,但没有发送 HTTP 请求。
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());
这就是我到目前为止所拥有的(目前该动作没有做任何事情)。
解决方案
有许多不同的方法可以实现这一点,但共同的主题是 a PendingIntent
只是连接到应用程序中实际发生工作的某个其他组件的钩子。您没有将 HTTP 请求放在中Intent
,而是将其放在Intent
调用的应用程序组件中。
在您的情况下,AlarmReceiver
Activity
您可能希望启动类似 anIntentService
或 a 的 东西,而不是从您的“PendingIntent”打开BroadcastReceiver
,并将您的网络操作放在那里。如果你还需要打开AlarmReceiver
,你也可以在你BroadcastReceiver
的.
使用哪个组件取决于您的应用程序正在做什么,并且超出了 StackOverflow 答案的范围。但这是非常基本的 Android 应用程序架构的东西,真的。