VladislavAntonyuk / WorldExplorer

Explore the globe like never before with World Explorer. Our AI-powered app gives you in-depth insights about any place worldwide, provides a comprehensive description, and recommends local attractions with your personalized travel guide.
https://world-explorer.azurewebsites.net
11 stars 3 forks source link

No push notification working #116

Closed erossini closed 6 months ago

erossini commented 7 months ago

Hello, I was looking at your code here https://github.com/VladislavAntonyuk/WorldExplorer/commit/8ce130a77e1ad29c70c237074b23f5971324eae4#diff-033e38b5872051a85ed80103bb7a5b65c953fd7de051ef370a98e125bdb3d697

I configured Azure with a Notification hub and set the Google part. The code for Android runs without issues but the device is not registered in the Azure portal.

image

I tried with different devices (emulators or real).

VladislavAntonyuk commented 7 months ago

Hi, do you call the code, that registers the device?

erossini commented 7 months ago

Yes, in the MainActivitity

protected override async void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    CrossFingerprint.SetCurrentActivityResolver(() => this);
    CrossMauiMTAdmob.Current.Init(this, Constants.AndroidApp, Constants.MauiMTAdmobLicence);

    await DeviceInstallationService.RegisterDevice("languageinuse", Constants.PushNotificationSecret);
}

With debug, I followed the code. I can see the call to PutAsJsonAsync(...) without errors.

erossini commented 7 months ago

Sorry, I noticed just now that the request received an Unauthorized error.

image
erossini commented 7 months ago

Sorry to disturb you again. I can't find why I get the Unauthorized error.

In Azure, I created a Notification hub

image

and the DefaultListenSharedAccessSignature is

Endpoint=sb://languageinuse.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=11/Ecr0JEXXXGMYqCPZP4r0q89xdjlxxxxx=

So, in the MainActivity, I register the device like

await DeviceInstallationService.RegisterDevice("languageinuse", Constants.PushNotificationSecret);

where languageinuse is the name of the hub (as in the endpoint) and the secret is the last part of the string. What do I do wrong?

I noticed that in the header there is this:

httpClient.DefaultRequestHeaders.Add("x-ms-version", "2015-01");

but there is no definition of this version in the API version on line.

image

Thank you in advance for your help, Enrico

VladislavAntonyuk commented 7 months ago

It's hard to say without knowing how you setup the azure. I suggest you try again with the steps described here https://vladislavantonyuk.github.io/articles/.NET-MAUI-Push-Notifications-using-Azure-Notification-Hub.-Part-1.-Setup-Azure-Notification-Hub.

At least you should use DefaultListenSharedAccessSignature access policy and SharedAccessKey, not the secret value.

erossini commented 7 months ago

๐Ÿ‘‹ I can't find the way to register the device and I tried quite hard. I still can't register my Windows app. So, I was working on the Android app. Using your original code, I get as a result of the call Unauthorized.

I created an Azure Notification Hub and the connection string is

Endpoint=sb://languageinuse.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=XfPbpiz2EGlsjsjAqEmFUoZJqNV=

In the Google Firebase, I created the Server Key and added it to the configuration in the Notification Hub.

image

With this configuration, I call the API and I get Unauthorized.

image

notificationHub: languageinuse key: the last part of the connection string

VladislavAntonyuk commented 7 months ago

I will probably record a video with steps, that you can reproduce to solve the issue. Maybe the issue with GoogleServices.json or some other missed step.

erossini commented 7 months ago

Thank you so much!

erossini commented 7 months ago

Always me ๐Ÿ˜ Now, I can register the app on the Azure Notification Hub. The trick was that the app must be in your Applications from personal account tab and not in the Owned applications tab.

image

The key I have to use in the code, is the Security key I use for registering the app or the key from the Access Policy for the Azure Notification Hub DefaultListenSharedAccessSignature? In any case, I tried both and it is now working; I always get Unauthorized.

erossini commented 7 months ago

I found the issue. In your code, you are using always the notificationHub variable. From the documentation, the URL is different

https://{notificationHubNamespace}.servicebus.windows.net/{notificationHub}/installations

where notificationHubNamespace is the main URL and notificationHub is the name of the hub. By any change, do you know how to implement the iOS version?

Thank you in advance, Enrico

VladislavAntonyuk commented 7 months ago

I donโ€™t have Apple developer account to try and provide the example( Official documentation describes the steps to get a certificate which you need to upload to Azure portal. After that the steps in the app should be the same

erossini commented 7 months ago

I have the certificate. What I don't know the configuration for the deviceInstallation. For example, in Android you have this

var firebaseToken = await FirebaseMessaging.Instance.GetToken();
var deviceInstallation = new
{
    InstallationId = GetDeviceId(),
    Platform = "gcm",
    PushChannel = firebaseToken.ToString()
};

Apart from the platform that I guess is apns, what is the PushChannel? I read somewhere that I have to implement the call in this function in the AppDelegate

[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    // ...
}
VladislavAntonyuk commented 7 months ago

https://learn.microsoft.com/en-us/azure/notification-hubs/ios-sdk-get-started

erossini commented 7 months ago

I saw that document, but they explained how to configure the Azure Notification Hub. Also, this document is quite old, so, some of the steps are completely different. In the documentation, they use the Azure Notification Hub SDK and there is no indication of how to pass the deviceInstallation to the api.

erossini commented 7 months ago

I'm still fighting with the notification now in Apple. Do you have any idea how to fill the deviceInstallation?

var deviceInstallation = new
{
    InstallationId = UIDevice.CurrentDevice.IdentifierForVendor.AsString(),
    Platform = "apns",
    PushChannel = ??
};

I think I can use the UIDevice to get an id for the device but I have no clue how to get the PushChannel.

VladislavAntonyuk commented 7 months ago

this is a request to Notification Service API. I took it from here: https://learn.microsoft.com/en-us/previous-versions/azure/reference/mt621153(v=azure.100)

pushChannel - String - The PNS handle for this installation (in case of WNS the ChannelUri of the ApplicationTile).

VladislavAntonyuk commented 7 months ago

another suggestion: Setup it using Xamarin and Notification hub lib: https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-ios-push-notification-apns-get-started and then see what request does your application send.

erossini commented 6 months ago

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

VladislavAntonyuk commented 6 months ago

Thank you, I will link your post in my article