在 Android Wear / Wear OS 上的通知操作中发送 post 请求

Sending a post request in a notification action on Android Wear / Wear OS

我的 Wear OS 应用程序中有一个带有操作的通知,我一直在寻找如何在触发通知操作时发送 post 请求。有办法吗?

我知道Android Wear 2.0 有上网功能,我见过打开浏览器有通知的例子,但没有看到发送 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());

这就是我目前所做的(目前该操作没有做任何事情)。

许多 种不同的方法来实现这一点,但共同的主题是 PendingIntent 只是一个挂钩到您的应用程序的其他组件实际上发生了。您没有将 HTTP 请求 放入 Intent 中,而是将其放入 Intent 调用的应用程序组件中。

在你的情况下,而不是从你的 'PendingIntent' 打开 AlarmReceiver Activity,你可能想要启动 IntentServiceBroadcastReceiver 而不是将您的网络操作放在那里。如果您还需要打开 AlarmReceiver,您也可以在 BroadcastReceiver 中打开。

Which component to use 取决于您的应用正在做什么的大局,并且超出了 Whosebug 答案的范围。但这是非常基本的 Android 应用程序架构的东西,真的。