Not Receiving Notifications on iOS devices from Azure Notification Hub using a Custom Template

Shant Hagopian 21 Reputation points
2022-07-12T21:42:34.097+00:00

I have two registered both Android and iOS devices on my Notification Hub with custom template registrations.
When I send a notification using a custom template "newJobRequestTemplate", Android receives it and displays it, but iOS does not.
Sending a notification using the custom template "textMessageTemplate" works fine on both devices.

My iOS device installation (registration) looks like this:

{  
    "installationId": "ab1ad748**********************",  
    "userId": "1f429963",  
    "pushChannel": "6EB8E**************************",  
    "pushChannelExpired": false,  
    "platform": 2,  
    "expirationTime": "9999-12-31T23:59:59.9999999Z",  
    "tags": [  
        "userId:1f429963"  
    ],  
    "pushVariables": null,  
    "templates": {  
        "newJobRequestTemplate": {  
            "body": "{\"aps\":{\"template\":\"newJobRequestTemplate\",\"title\":\"$(title)\",\"date\":\"$(date)\",\"address\":\"$(address)\"}}",  
            "headers": null,  
            "expiry": null,  
            "tags": [  
                "newJobRequestTemplate"  
            ]  
        },  
       "textMessageTemplate": {  
           "body": "{\"aps\":{\"template\":\"textMessageTemplate\",\"alert\":\"$(message)\",\"title\":\"$(title)\"}}",  
           "headers": null,  
           "expiry": null,  
           "tags": [  
                "textMessageTemplate"  
           ]  
       }  
    },  
    "secondaryTiles": null  
}  

When I do a Test Send from Azure Portal to the Tag expression: newJobRequestTemplate with the payload below, only Android receives it, iOS doesn't

{  
    "title":"New job available",  
    "date":"July 16",  
    "address":"New York"  
}  

But when I do a Test Send from Azure Portal to the Tag expression: textMessageTemplate with the payload below, both Android and iOS receive it no problems.

{  
    "title":"Test title",  
    "message":"Test message"  
}  

Here's the code I have in my iOS project

AppDelegate.cs

 public override bool FinishedLaunching(UIApplication app, NSDictionary options)  
        {  
            global::Xamarin.Forms.Forms.Init();  
            Xamarin.FormsMaps.Init();  
  
            UNUserNotificationCenter.Current.Delegate = new iOSNotificationReceiver();  
  
  
            if(UIDevice.CurrentDevice.CheckSystemVersion(8,0))  
            {  
                var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(  
                                    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,  
                                    new NSSet());  
  
                UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);  
                UIApplication.SharedApplication.RegisterForRemoteNotifications();  
            }  
            else  
            {  
                UIRemoteNotificationType notificationType = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;  
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationType);  
            }  
             
            LoadApplication(new App());  
  
            return base.FinishedLaunching(app, options);  
        }  

And iOSNotificationReceiver.cs

    public class iOSNotificationReceiver : UNUserNotificationCenterDelegate  
    {  
        // Called if app is in the foreground.  
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action
Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
311 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.