Skip to content

Commit

Permalink
Merge pull request #261 from Azure/addXiaomiNotification
Browse files Browse the repository at this point in the history
Add xiaomi notification
  • Loading branch information
weihengSu2 authored Mar 10, 2023
2 parents 5f9e17a + fe0c2b0 commit fc34bd4
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Samples/SendRestExample/SendRestExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ static void Main(string[] args)
string hubName = ConfigurationManager.AppSettings["HubName"];
string fullConnectionString = ConfigurationManager.AppSettings["DefaultFullSharedAccessSignature"];


// Example sending a native notification
Console.WriteLine("\nNotification Message ID : ");
//String messageId = SendNativeNotificationREST(hubName, fullConnectionString, "Hello From REST", "GCM").Result;
//String messageId = SendNativeNotificationREST(hubName, fullConnectionString, "Hello From REST", "WNS").Result;
//String messageId = SendNativeNotificationREST(hubName, fullConnectionString, "Hello From REST", "XIAOMI").Result;
String messageId = SendNativeNotificationREST(hubName, fullConnectionString, "Hello From REST", "APNS").Result;

if (messageId != null)
Expand Down Expand Up @@ -108,6 +108,12 @@ private static async Task<string> SendNativeNotificationREST(string hubName, str
"</toast>";
response = await ExecuteREST("POST", uri, sasToken, headers, body, "application/xml");
break;
case "xiaomi":
headers.Add("X-Target-Pipeline", "New");
headers.Add("ServiceBusNotification-Format", "xiaomi");
body = "{\"title\":\"Title\",\"payload\":\"" + message + "\",\"description\":\"Description\"}";
response = await ExecuteREST("POST", uri, sasToken, headers, body);
break;
}

char[] seps1 = { '?' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void NotificationContentTypeCorrespondsToApi()
Assert.Equal($"application/json;charset={Encoding.UTF8.WebName}", new TemplateNotification(new Dictionary<string, string>()).ContentType);
Assert.Equal("application/json", new AdmNotification("{\"data\":{\"key1\":\"value1\"}}").ContentType);
Assert.Equal("application/x-www-form-urlencoded", new BaiduNotification("{\"title\":\"Title\",\"description\":\"Description\"}").ContentType);
Assert.Equal($"application/json;charset={Encoding.UTF8.WebName}", new XiaomiNotification("{\"payload\":\"Payload\",\"title\":\"Title\",\"description\":\"Description\"}").ContentType);
}

[Theory]
Expand Down
25 changes: 25 additions & 0 deletions src/Microsoft.Azure.NotificationHubs/NotificationHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,31 @@ public Task<NotificationOutcome> SendMpnsNativeNotificationAsync(string nativePa
return SendNotificationAsync(new MpnsNotification(nativePayload), tags, cancellationToken);
}

/// <summary>
/// Sends a Xiaomi native notification. To specify headers for Xiaomi, use the <see cref="M:Microsoft.Azure.NotificationHubs.NotificationHubClient.SendNotificationAsync(Microsoft.Azure.NotificationHubs.Notification)" /> method.
/// </summary>
/// <param name="nativePayload">The native payload.</param>
/// <returns>
/// <see cref="Microsoft.Azure.NotificationHubs.NotificationOutcome" /> which describes the result of the Send operation.
/// </returns>
public Task<NotificationOutcome> SendXiaomiNativeNotificationAsync(string nativePayload)
{
return SendNotificationAsync(new XiaomiNotification(nativePayload), string.Empty);
}

/// <summary>
/// Sends a Xiaomi native notification. To specify headers for Xiaomi, use the <see cref="M:Microsoft.Azure.NotificationHubs.NotificationHubClient.SendNotificationAsync(Microsoft.Azure.NotificationHubs.Notification)" /> method.
/// </summary>
/// <param name="nativePayload">The native payload.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for a task to complete.</param>
/// <returns>
/// <see cref="Microsoft.Azure.NotificationHubs.NotificationOutcome" /> which describes the result of the Send operation.
/// </returns>
public Task<NotificationOutcome> SendXiaomiNativeNotificationAsync(string nativePayload, CancellationToken cancellationToken)
{
return SendNotificationAsync(new XiaomiNotification(nativePayload), string.Empty, cancellationToken);
}

/// <summary>
/// Sends a notification to a non-empty set of tags (max 20). This is equivalent to a tag expression with boolean ORs ("||").
/// </summary>
Expand Down
75 changes: 75 additions & 0 deletions src/Microsoft.Azure.NotificationHubs/XiaomiNotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
//----------------------------------------------------------------

using System;
using System.Text;

namespace Microsoft.Azure.NotificationHubs
{
/// <summary>
/// Represents a Xiaomi notification.
/// </summary>
public sealed class XiaomiNotification : Notification, INativeNotification
{
static string contentType = $"application/json;charset={Encoding.UTF8.WebName}";
static string targetPipeline = "X-Target-Pipeline";
static string serviceBusNotificationFormat = "ServiceBusNotification-Format";
static string newPipeline = "New";

/// <summary>
/// Initializes a new instance of the <see cref="T:Microsoft.Azure.NotificationHubs.XiaomiNotification"/> class.
/// </summary>
/// <param name="jsonPayload">The JSON payload.</param>
public XiaomiNotification(string jsonPayload)
: base(null, null, contentType)
{
if (string.IsNullOrWhiteSpace(jsonPayload))
{
throw new ArgumentNullException("jsonPayload");
}

this.Body = jsonPayload;
}

/// <summary>
/// Initializes a new instance of the <see cref="T:Microsoft.Azure.NotificationHubs.FcmNotification"/> class.
/// </summary>
/// <param name="jsonPayload">The JSON payload.</param><param name="tag">The notification tag.</param>
[Obsolete("This method is obsolete.")]
public XiaomiNotification(string jsonPayload, string tag)
: base(null, tag, contentType)
{
if (string.IsNullOrWhiteSpace(jsonPayload))
{
throw new ArgumentNullException("jsonPayload");
}

this.Body = jsonPayload;
}

/// <summary>
/// Gets the type of the platform.
/// </summary>
/// <value>
/// The type of the platform.
/// </value>
protected override string PlatformType
{
// This will be changed into XiaomiCredential.AppPlatformName when we add the XiaomiCredential class in a following PR
get { return "xiaomi"; }
}

/// <summary>
/// Validate and populates the headers.
/// </summary>
protected override void OnValidateAndPopulateHeaders()
{
this.AddOrUpdateHeader(serviceBusNotificationFormat, PlatformType);
this.AddOrUpdateHeader(targetPipeline, newPipeline);
}
}
}

0 comments on commit fc34bd4

Please sign in to comment.