1

I have achieved the push notification using Azure notification hub but I want to extend the functionality to send different notification to different devices. I am registering devices using Azure Web Api using following method. I have completely followed this link but not able to figure our how can I send notification either by Device Id or User?

public async Task RegisterDeviceAsync(params string[] tags)
    {
        var deviceInstallation = DeviceInstallationService?.GetDeviceInstallation(tags);

        await SendAsync<DeviceInstallation>(HttpMethod.Put, RequestUrl, deviceInstallation)
            .ConfigureAwait(false);

        await SecureStorage.SetAsync(CachedDeviceTokenKey, deviceInstallation.PushChannel)
            .ConfigureAwait(false);

        await SecureStorage.SetAsync(CachedTagsKey, JsonConvert.SerializeObject(tags));
    }

I am using below payload template

public class Generic
    {
        public const string Android = "{ \"notification\": { \"title\" : \"PushDemo\", \"body\" : \"$(alertMessage)\"}, \"data\" : { \"action\" : \"$(alertAction)\" } }";
        public const string iOS = "{ \"aps\" : {\"alert\" : \"$(alertMessage)\"}, \"action\" : \"$(alertAction)\" }";
    }

Below method is used to send notifications:

public async Task<bool> RequestNotificationAsync(NotificationRequest notificationRequest, CancellationToken token)
    {
        if ((notificationRequest.Silent &&
            string.IsNullOrWhiteSpace(notificationRequest?.Action)) ||
            (!notificationRequest.Silent &&
            (string.IsNullOrWhiteSpace(notificationRequest?.Text)) ||
            string.IsNullOrWhiteSpace(notificationRequest?.Action)))
            return false;

        var androidPushTemplate = notificationRequest.Silent ?
            PushTemplates.Silent.Android :
            PushTemplates.Generic.Android;

        var iOSPushTemplate = notificationRequest.Silent ?
            PushTemplates.Silent.iOS :
            PushTemplates.Generic.iOS;

        var androidPayload = PrepareNotificationPayload(
            androidPushTemplate,
            notificationRequest.Text,
            notificationRequest.Action);

        var iOSPayload = PrepareNotificationPayload(
            iOSPushTemplate,
            notificationRequest.Text,
            notificationRequest.Action);

        try
        {
            if (notificationRequest.Tags.Length == 0)
            {
                // This will broadcast to all users registered in the notification hub
                await SendPlatformNotificationsAsync(androidPayload, iOSPayload, token);
            }
            else if (notificationRequest.Tags.Length <= 20)
            {
                await SendPlatformNotificationsAsync(androidPayload, iOSPayload, notificationRequest.Tags, token);
            }
            else
            {
                var notificationTasks = notificationRequest.Tags
                    .Select((value, index) => (value, index))
                    .GroupBy(g => g.index / 20, i => i.value)
                    .Select(tags => SendPlatformNotificationsAsync(androidPayload, iOSPayload, tags, token));

                await Task.WhenAll(notificationTasks);
            }

            return true;
        }
        catch (Exception e)
        {
            _logger.LogError(e, "Unexpected error sending notification");
            return false;
        }
    }
Zeeshan shaikh
  • 341
  • 1
  • 5
  • 24
  • You can refer to [Send cross-platform notifications with Azure Notification Hubs](https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-aspnet-cross-platform-notification), [Send push notification to all registered devices with Azure Notification Hub in .NET](https://stackoverflow.com/questions/39590084/send-push-notification-to-all-registered-devices-with-azure-notification-hub-in), and [Develop Cloud Connected Mobile Apps with Xamarin and Microsoft Azure](https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter5/ios/) – Ecstasy Sep 21 '21 at 07:29
  • You need to send `tagged notifications` ,refer to https://learn.microsoft.com/en-us/azure/notification-hubs/push-notifications-android-specific-devices-firebase-cloud-messaging. – ColeX Sep 21 '21 at 08:38
  • @ColeX-MSFT Do you have any example of Xamarin Forms? This is for Native Android App – Zeeshan shaikh Sep 21 '21 at 08:41
  • Which library are you using ? – ColeX Sep 21 '21 at 08:44
  • I am using Azure Notification Hub, APNS for IOS – Zeeshan shaikh Sep 21 '21 at 08:46
  • Could you attach the plugin(blog/tutorial) link here ? – ColeX Sep 22 '21 at 01:46
  • I have followed this link https://learn.microsoft.com/en-us/azure/developer/mobile-apps/notification-hubs-backend-service-xamarin-forms – Zeeshan shaikh Sep 22 '21 at 04:44
  • 1
    First register device/user with tag , then send push notification with tag extension , refer to [Routing and Tag Expressions](https://learn.microsoft.com/en-us/previous-versions/azure/azure-services/dn530749(v=azure.100)?f=255&MSPPError=-2147217396) , – ColeX Sep 23 '21 at 06:10
  • That's fine but I am using Web API to send push notifications, how can I send notification to multiple users at same time using Tags in payload? Any Idea. – Zeeshan shaikh Sep 23 '21 at 07:04
  • 1
    Try to replace `notificationRequest.Tags` with your own tags in `SendPlatformNotificationsAsync(androidPayload, iOSPayload, YourTags, token);` – ColeX Sep 24 '21 at 05:48

0 Answers0