Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Azure Notification Hub on MAUI #298

Open
MouratidisA opened this issue Sep 23, 2023 · 25 comments
Open

[Question] Azure Notification Hub on MAUI #298

MouratidisA opened this issue Sep 23, 2023 · 25 comments

Comments

@MouratidisA
Copy link

MouratidisA commented Sep 23, 2023

Question
Is there a way to implement notification hub registration on MAUI applications (Android and iOS)?

I've tried implementing notification hub registration using NotificationHubClient on IOS( but I don't think a client application should use this implementation to register).

Implementation:

[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
 {
        //Create the client
        var nhClient = NotificationHubClient.CreateClientFromConnectionString(hubConnectionString,notificationHubName);
        //Set the notification tags for specific tag notifications
        string[] tags = { "sampletag" };
        //Retrieve the device token
        byte[] deviceTokenBytes = new byte[deviceToken.Length];
        System.Runtime.InteropServices.Marshal.Copy(deviceToken.Bytes, deviceTokenBytes, 0, (int)deviceToken.Length);
        //Convert token to string value
        string deviceTokenStr = BitConverter.ToString(deviceTokenBytes).Replace("-", string.Empty).Replace(" ", string.Empty);
        
        // Register
        AppleRegistrationDescription created = await 
        nhClient.CreateAppleNativeRegistrationAsync(deviceTokenStr,tags.ToArray());

        if (await nhClient.RegistrationExistsAsync(created.RegistrationId))
            Debug.WriteLine("Found the registration!!");
        else
            Debug.WriteLine("Registration not found");
 }

What is the proper way to implement push notifications on MAUI for Android and iOS using the Azure Notification hubs?

@Denny966
Copy link

Denny966 commented Oct 9, 2023

I'm having the same issue, how would one use it to register on the hub? Where are the examples?

@lukampa
Copy link

lukampa commented Dec 8, 2023

Same problem here..

@adambarath
Copy link

Any update on this?

@michaelonz
Copy link

Same issue - I see they have released 4.2.0-beta1 .... will this mean maui 8 is supported?

@developer9969
Copy link

Why is it that is so difficult to get any kind of official response? Anyone on the Azure notificationHubs-dotnet team ? thanks

@a-martsineuski
Copy link

Is there are any updates or solutions? how to implement support of Notification Hub on MAUI?

@michaelonz
Copy link

We basically cant wait any longer for a response - does anyone know of a different notification framework that works for android and IOS that is being supported on maui?

@developer9969
Copy link

@michaelonz that is big issue at the moment. there is nothing long term post june that will work on both iOS and android. and for people on this forum that moderate it and not to reply is just awful . I guess is to try onesignal (free) but does not work with hotrestart if you are using a pc.. or any paid alternative and would be nice to know if someone has implemented anything that works

@gundetirevanth
Copy link

Anyone from Azure notificationHubs-dotnet team respond on the above issue, its been long time that we are waiting for MAUI supported package of IOS Azure Push notification

@RobertHedgate
Copy link

I solved iOS push by using Microsoft.Azure.NotificationHubs

var hub = NotificationHubClient.CreateClientFromConnectionString(this.connectionString, this.notificationHubName);
var installation = new Installation
{
InstallationId = deviceToken,
PushChannel = deviceToken,
Platform = NotificationPlatform.Apns,
Tags = Array.Empty()
};

await hub.CreateOrUpdateInstallationAsync(installation);

deviceToken is obtained from RegisteredForRemoteNotifications

@DeveloperLookBook
Copy link

DeveloperLookBook commented Mar 30, 2024

Please, update your docs and examples and show how to use Azure Notification Hub with MAUI.

@michaelonz
Copy link

@RobertHedgate - What nuget packages did you have installed to make this work - I cant find the NotificationHubClient from the IOS platform code.

@RobertHedgate
Copy link

@a-martsineuski
Copy link

@RobertHedgate this package works for .NET 6, but not with the .NET8

@RobertHedgate
Copy link

@a-martsineuski My iOS app is on .net8. Doesn´t the .net6 flag mean .net6 or higher?

@a-martsineuski
Copy link

@RobertHedgate let's clarify: your app is net8.0-ios\net8.0-android? Yes, that means .NET6 or higher, but not .NETX-android.NETX-ios. It not possible to install it in the MAUI project

@RobertHedgate
Copy link

@a-martsineuski no it is MAUI app with platforms and all.

@a-martsineuski
Copy link

@RobertHedgate could you show a demo app, how did you install it?

@RobertHedgate
Copy link

@a-martsineuski I created a quick repo of my solution. It is not complete but contains where one gets the tokens and calls the azure hub. Can be found here https://github.com/RobertHedgate/MauiAppPush

@DeveloperLookBook
Copy link

DeveloperLookBook commented Apr 2, 2024

@RobertHedgate
I tried to run/build your test project as it is without any changes on Visual Studio for Mac 17.6.10 (build 428), and I get an error during the build proccess:

/Users/yevhenmyroshnychenko/Projects/Work/MauiAppPush/MauiAppPush: Error JAVA0000: Error in /Users/yevhenmyroshnychenko/.nuget/packages/xamarin.androidx.collection.jvm/1.3.0.2/buildTransitive/net7.0-android33.0/../../jar/androidx.collection.collection-jvm.jar:androidx/collection/ArrayMapKt.class: Type androidx.collection.ArrayMapKt is defined multiple times: /Users/yevhenmyroshnychenko/.nuget/packages/xamarin.androidx.collection.jvm/1.3.0.2/buildTransitive/net7.0-android33.0/../../jar/androidx.collection.collection-jvm.jar:androidx/collection/ArrayMapKt.class, /Users/yevhenmyroshnychenko/.nuget/packages/xamarin.androidx.collection.ktx/1.2.0.9/buildTransitive/net6.0-android31.0/../../jar/androidx.collection.collection-ktx.jar:androidx/collection/ArrayMapKt.class Compilation failed java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: /Users/yevhenmyroshnychenko/.nuget/packages/xamarin.androidx.collection.jvm/1.3.0.2/buildTransitive/net7.0-android33.0/../../jar/androidx.collection.collection-jvm.jar androidx/collection/ArrayMapKt.class at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:135) at com.android.tools.r8.D8.main(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:5) Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: /Users/yevhenmyroshnychenko/.nuget/packages/xamarin.androidx.collection.jvm/1.3.0.2/buildTransitive/net7.0-android33.0/../../jar/androidx.collection.collection-jvm.jar:androidx/collection/ArrayMapKt.class at Version.fakeStackEntry(Version_8.2.33.java:0) at com.android.tools.r8.T.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:5) at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:82) at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:32) at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:31) at com.android.tools.r8.utils.S0.b(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:2) at com.android.tools.r8.D8.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:42) at com.android.tools.r8.D8.b(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:13) at com.android.tools.r8.D8.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:40) at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:122) ... 1 more Caused by: com.android.tools.r8.utils.b: Type androidx.collection.ArrayMapKt is defined multiple times: /Users/yevhenmyroshnychenko/.nuget/packages/xamarin.androidx.collection.jvm/1.3.0.2/buildTransitive/net7.0-android33.0/../../jar/androidx.collection.collection-jvm.jar:androidx/collection/ArrayMapKt.class, /Users/yevhenmyroshnychenko/.nuget/packages/xamarin.androidx.collection.ktx/1.2.0.9/buildTransitive/net6.0-android31.0/../../jar/androidx.collection.collection-ktx.jar:androidx/collection/ArrayMapKt.class at com.android.tools.r8.utils.Q2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:21) at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:54) at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:10) at java.base/java.util.concurrent.ConcurrentHashMap.merge(ConcurrentHashMap.java:2048) at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:6) at com.android.tools.r8.graph.m4$a.d(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:6) at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:61) at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:12) at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:9) at com.android.tools.r8.D8.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:45) at com.android.tools.r8.D8.d(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:17) at com.android.tools.r8.D8.c(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:69) at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:28) ... 6 more (JAVA0000) (MauiAppPush) java

@RobertHedgate
Copy link

@DeveloperLookBook I have added some nuget to android so it should build now but you still need to add google-sevice,json and all certificate to your project to make it work. You should more look at the project and use its registration to azure in your own projects.

@michaelonz
Copy link

michaelonz commented Apr 2, 2024

Hi @RobertHedgate - can you please specify what certificates we need to add and where we need to put them in the folder structure along with if any properties are needed on the files (eg embedded resource etc etc) - so good to finally see an example working with .net8

@RobertHedgate
Copy link

@michaelonz Added this below to the readme of the project. Hope it helps.

For iOS you don´t need anything else in your app except selecting the correct certificate when you build.

Android you need the google-service.json file you download from you firebase console and add it directly under Android folder.

Here are a tutorial on iOS. https://learn.microsoft.com/en-us/azure/notification-hubs/ios-sdk-get-started

Setting up Android FCM V1 https://learn.microsoft.com/en-us/azure/notification-hubs/firebase-migration-rest

If GoogleServieJson doesn´t show up as build action dotnet/maui#14486

@michaelonz
Copy link

michaelonz commented Apr 6, 2024

@RobertHedgate - i downloaded your sample (thank you) and i downloaded my google-services.json and set the buildaction to googleservices.json.

When I run it on android (havent tried apple yet) I get the following error:

[FirebaseApp] Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project.

Any ideas?

NOTE: I have already tried re downloading the google-services.json file also.

@michaelonz
Copy link

Hi @RobertHedgate - No need for you to look into my issue above .... I worked it out.
The in the .csproj file MUST match the project settings in firebase console. So on the Project settings/general tab - down scroll down to where it says "your apps" then "android apps" - the android app name MUST match - it will be something like "com.demo.sample" (eg com. company. product)
I always assumed the connect string worked this out - but it must also check the ApplicationId

Hope this helps others also.

I still havent got the end to end message working but this resolved the following error:

[FirebaseApp] Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants