You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求基于Azure的桌面应用按钮触发跨端同步推送通知基础框架

Hey there! Let's break down a solid, straightforward framework to build your cross-platform notification system using Azure. Here's how you can make that button trigger notifications to both desktop and mobile apps smoothly:

Core Azure Service Selection

The star of the show here will be Azure Notification Hubs — it’s designed specifically to handle cross-platform push notifications in a unified way, supporting all major platforms (iOS, Android, Windows/macOS desktop, etc.). For added security (so you don’t expose Azure credentials directly in your desktop app), we’ll also use an Azure Function as a lightweight middle layer.

Overall Architecture Flow
  1. User clicks the button in your desktop app
  2. Desktop app sends a notification request to your Azure Function endpoint
  3. Azure Function forwards the request to Azure Notification Hubs
  4. Notification Hubs routes the notification to registered desktop and mobile devices
Step-by-Step Implementation

1. Set Up Azure Resources

  • Create an Azure Notification Hubs instance in the Azure Portal: Pick a region close to your users, configure basic settings, and note down the connection string (you’ll need this later).
  • Create an Azure Function (HTTP trigger): This acts as a secure proxy. Set it up with a POST endpoint, and grant it access to your Notification Hubs instance using managed identity (no hardcoding credentials!).

2. Configure Platform-Specific Notification Channels

Mobile Platforms

  • Android:
    1. Create a Firebase project, generate an FCM (Firebase Cloud Messaging) server key.
    2. In your Notification Hubs instance, go to the Google (FCM) tab and paste the server key.
  • iOS:
    1. Generate an APNs (Apple Push Notification service) certificate or token in the Apple Developer Portal.
    2. Upload the certificate/token to the Apple (APNs) tab in your Notification Hubs instance.

Desktop Platforms

  • Windows Desktop:
    1. Register your app with the Microsoft Store to get a WNS (Windows Notification Service) SID and secret key.
    2. Add these credentials to the Windows (WNS) tab in your Notification Hubs instance.
  • macOS Desktop: Use the same APNs configuration you set up for iOS — macOS supports APNs natively.

3. Integrate Client Apps (Desktop + Mobile)

All clients need to register their device with Notification Hubs to receive pushes:

  • Mobile Clients:
    • Android: Integrate the Firebase SDK, fetch the device’s FCM token, and send it to Notification Hubs to complete registration.
    • iOS: Use the UserNotifications framework to get the device’s APNs token, then register it with Notification Hubs.
  • Desktop Clients:
    • Windows: Use the Windows Push Notification Services SDK to get the device’s push URI, then register it with Notification Hubs.
    • MacOS: Use the APNs SDK to fetch the device token and register it, just like iOS.

4. Add Button Trigger Logic to Your Desktop App

When the button is clicked, your desktop app sends a POST request to your Azure Function’s endpoint with the notification content (title, body, any custom data).

Example snippet for a C# desktop app using HttpClient:

private async void NotifyButton_Click(object sender, EventArgs e)
{
    var notificationContent = new
    {
        Title = "Action Triggered!",
        Body = "The desktop button was pressed!",
        CustomData = new { Source = "DesktopApp" }
    };

    using var client = new HttpClient();
    var jsonContent = JsonContent.Create(notificationContent);
    await client.PostAsync("https://your-azure-function-endpoint.azurewebsites.net/api/SendNotification", jsonContent);
}

5. Azure Function Logic to Forward Notifications

In your Azure Function, add code to receive the request and send it to Notification Hubs. Example in C#:

[FunctionName("SendNotification")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
    ILogger log)
{
    var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    var notificationData = JsonConvert.DeserializeObject<NotificationData>(requestBody);

    var hubClient = NotificationHubClient.CreateClientFromConnectionString(Environment.GetEnvironmentVariable("NotificationHubsConnectionString"), "YourHubName");
    
    // Send to all platforms
    await hubClient.SendFcmNativeNotificationAsync(JsonConvert.SerializeObject(notificationData));
    await hubClient.SendAppleNativeNotificationAsync(JsonConvert.SerializeObject(notificationData));
    await hubClient.SendWindowsNativeNotificationAsync(JsonConvert.SerializeObject(notificationData));

    return new OkResult();
}

public class NotificationData
{
    public string Title { get; set; }
    public string Body { get; set; }
    public object CustomData { get; set; }
}

6. Test & Debug

  • Use the Test Send feature in Azure Notification Hubs to verify pushes work for each platform first.
  • Add logging in your client apps to confirm device registration and notification reception.
  • Handle edge cases: Update expired device tokens/URIs in Notification Hubs when clients restart or refresh their credentials.

内容的提问来源于stack exchange,提问作者Sonali Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:59:25