Push Notification with Azure Notification Hub and documentation

Enrico Rossini 201 Reputation points
2024-02-23T08:26:48.8733333+00:00

I'm trying to add push notification in my .NET8 MAUI application. I was looking at the Microsoft documentation and the function public override void RegisteredForRemoteNotifications is probably what I need.

[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public void RegisteredForRemoteNotifications(UIKit.UIApplication application, 
    NSData deviceToken)
{
    // Get current device token
    var DeviceToken = deviceToken.Description;
    if (!string.IsNullOrWhiteSpace(DeviceToken)) {
        DeviceToken = DeviceToken.Trim('<').Trim('>');
    }

    // Get previous device token
    var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

    // Has the token changed?
    if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
    {
        // TODO: Put your own logic here to notify your server 
        // that the device token has changed/been created!
    }

    // Save new device token
    NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}

In the documentation, it is clear that I can have a DeviceToken and then add my custom code to register the device in the Azure Notification Hub. Unfortunately, this override doesn't exist in the AppDelegate in the iOS project.

Developer technologies | .NET | .NET MAUI
{count} votes

2 answers

Sort by: Most helpful
  1. Enrico Rossini 201 Reputation points
    2024-03-03T12:58:57.4166667+00:00

    Finally, I found the solution and I wrote a post to explain all the steps to send and receive push notification from Azure Notification Hub in iOS

    1 person found this answer helpful.

  2. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2024-02-26T04:42:31.25+00:00

    Hello,

    this override doesn't exist in the AppDelegate in the iOS project.

    Please try the following in your AppDelegate class.

      [Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
        public void RegisteredForRemoteNotifications(UIKit.UIApplication application, NSData deviceToken)
        {
            
            ...
        }
    

    Tips:

    The doc you mentioned is about Apple Push Notification Gateway Service (APNS) without Azure Notification Hubs, and it's about Xamarin.iOS. You could refer to Send push notifications to Xamarin using Azure Notification Hubs | Microsoft Learn.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.