Skip to content

Instantly share code, notes, and snippets.

@iqan
Created July 7, 2020 00:35
Show Gist options
  • Save iqan/23c9e7b21f80c2d6b6bd018098d250d9 to your computer and use it in GitHub Desktop.
Save iqan/23c9e7b21f80c2d6b6bd018098d250d9 to your computer and use it in GitHub Desktop.
Azure Function implementation that listens to Cosmos DB updates and sends notification to Android
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.NotificationHubs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace PromoNotificationFunc
{
public static class NotifyOnPromo
{
[FunctionName(nameof(NotifyOnPromo))]
public static async Task Run([CosmosDBTrigger(
databaseName: "iqanscosmosdb1",
collectionName: "promo",
ConnectionStringSetting = "CosmosDB.ConnectionString",
LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
{
if (input != null && input.Count > 0)
{
log.LogInformation("Documents modified " + input.Count);
var promoText = input[0].GetPropertyValue<string>("text");
log.LogInformation("Promo Id " + input[0].Id);
log.LogInformation("Promo Text " + promoText);
var connectionString = "REPLACE WITH YOUR NOTIFICATION HUB CONNECTION STRING";
var hubName = "REPLACE WITH YOUR NOTIFICATION HUB NAME";
var nhClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);
var notificationPayload = "{\"data\":{\"body\":\"" + promoText + "\",\"title\":\"" + input[0].Id + "\"}}";
var notification = new FcmNotification(notificationPayload);
var outcomeFcmByTag = await nhClient.SendFcmNativeNotificationAsync(notificationPayload);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment