Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin.Forms (iOS & Droid) guide for Azure Notification Hub that actually works as of July 2019?

LaumaniaLaumania DKMember ✭✭✭

Ok, I'm getting really tired of this now!
I have an app that haven't been updated for about 3 years. Therefore I'm rewriting the entire app now and all is fine.

Now I'm at the point where I need the new app to use the Azure Notification Hub, with template registrations, as the old one does.

... and it seems everything in relation to Xamarin and Notification Hub have just been forgotten or abandon, because none of the guides I find works.

Even the official one here, doesn't work and complains about some of the classes used in the sample code being obsolete.
https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm

Notifications in mobile apps is such a common thing, I assume, why is it still SO hard to get working? (I know MS is not here to blame for how Google and Apple do the actual notification services, but, Microsoft build Notification Hub as a wrapper and around that - and that idea is brilliant!

This question is already too long..so i stop here :)

So anybody have a Xamarin.Forms (or just a Android and iOS) specific guide to make hook the app(s) up to Notification Hub?

Answers

  • LaumaniaLaumania DKMember ✭✭✭

    Just another sample of how insane it is to find your way around getting Notification Hub working in Xamarin.Forms.

    https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-push

    The title is: "Add push notifications to your Xamarin.Forms app"
    I might misunderstand this title, but my guess would be that this article/guide would inform me "how to add push notification to your Xamarin.Forms app"....but hey, I might misunderstand... (facepalm).

    Then the article goes through setting up Notification Hub - awesome this looks promissing!

    ...and it starts out good.

    "Complete this section to enable push notifications for the Xamarin.Forms Droid project for Android."

    Ok, I start to get my hopes up that this could actually tell about what I want to know!

    ..but then:
    "For Android package name, copy the value of your applicationId in your application's build.gradle file. In this example, it's com.fabrikam.fcmtutorial1app."

    "build.gradle files"? What is that? Googling it tells me it's some Java build tool - awesome when I'm trying to build an Xamarin.Forms Android app!

    From what I can see, we are suppose to use Nuget for Xamarin.Forms, like all other .NET apps, so why mention this "gradle" shi* - https://stackoverflow.com/questions/38049536/gradle-dependencies-in-xamarin

    And then it continues with setting up a FirebaseInstanceIdService, which is depricated - awesome! (facepalm)

    Am I the only one with these problems?

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    Mads, thanks for bringing this to my attention. I will reach out to the teams about the docs as they should work just fine. App Center uses Notification Hubs under the hood, but wrap up everything into a nice bundle. It looks like the docs did not migrate correctly.

    You would want to use the notification hub docs via Xamarin docs directly.

  • JoLapralineJoLapraline FRMember ✭✭
    edited July 2019

    just a quick tip...on IOS side when you register your hub, it should be done on mainthread (usualy directly in the appdelegate). Doesn't work otherwise (doesn't seems to work in an async method). So if you're planning to be async (like registering after a login process to have a unique push token for every users) don't forget that. It took me a while to understand why it worked when registering where placed in the AppDelegate, and not working when registered in another class called asynchronously.

  • LaumaniaLaumania DKMember ✭✭✭

    @JoLapraline said:
    just a quick tip...on IOS side when you register your hub, it should be done on mainthread (usualy directly in the appdelegate). Doesn't work otherwise (doesn't seems to work in an async method). So if you're planning to be async (like registering after a login process to have a unique push token for every users) don't forget that. It took me a while to understand why it worked when registering where placed in the AppDelegate, and not working when registered in another class called asynchronously.

    Thanks for mentioning that - however that not my current problem, but good to know.

  • LaumaniaLaumania DKMember ✭✭✭

    @JamesMontemagno said:
    Mads, thanks for bringing this to my attention. I will reach out to the teams about the docs as they should work just fine. App Center uses Notification Hubs under the hood, but wrap up everything into a nice bundle. It looks like the docs did not migrate correctly.

    You would want to use the notification hub docs via Xamarin docs directly.

    Thanks for replying and acting so quickly!

    I know App Center do PUSH notifications, but I'm interested in using the Notification Hub myself, as I'm using the "template" setup to send info to the app etc.
    I got it all setup and working in my current version of the app in the AppStore, but now it doesn't work in the new rewrite of the app.
    So I know how smart and useful it is, just a pain to get setup and working in the first place.

    On a side note: I'm currently testing in the Android Emulator, but from what I can read that should work. It's only the iOS emulator that doesn't support push notifications - but here I have a device so that's no problem.

  • LaumaniaLaumania DKMember ✭✭✭

    @JamesMontemagno said:
    Mads, thanks for bringing this to my attention. I will reach out to the teams about the docs as they should work just fine. App Center uses Notification Hubs under the hood, but wrap up everything into a nice bundle. It looks like the docs did not migrate correctly.

    You would want to use the notification hub docs via Xamarin docs directly.

    Another thing, the docs you link to seems a little better than the ones I found on docs.microsoft.com, but yet again, they use the FirebaseInstanceIdService and OnTokenRefresh() with is depricated. So, seeing this depricated in the code makes me think that's the main reason it's not working.

    Normally I start assuming I'm doing something wrong when things are not working, but in this case, seeing these "depricated" classes and methods used in the tutorial, makes me bet it's the sample thats wrong. :)

  • LaumaniaLaumania DKMember ✭✭✭

    Still haven't got it working, but I'm starting to suspect that it's my Android emulator that doesn't support push notifications...so need to get a device and try on it.

    Meanwhile....I'm spending days on trying to generate a freaking JWT Auth token shit for iOS APNS notifications...as Notification Hub needs that.

    Got this guide, which is actually pretty ok:
    https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns#2943371

    Right to it get to:
    "Encrypt the resulting JSON data using your authentication token signing key and the specified algorithm."

    But it doesn't explain how or link to any help.... and google haven't been able to help me yet!

  • LaumaniaLaumania DKMember ✭✭✭

    Created this now, as I'm starting to go mad over these "simple things" that takes to long time :)

    https://stackoverflow.com/questions/57357354/guide-how-to-actually-encrypt-json-token-for-apns

  • LaumaniaLaumania DKMember ✭✭✭

    @JamesMontemagno Sad to see nothing have happened yet. I saw you tagged people on Github, but nothing happened as far as I can see.

    If you started with an empty Azure and vanilla Xamarin.Forms app and followed the documentation you would see it didn't work.
    Ex. the documentation uses some methods, that you get told are obsolete by VS when you try and use them for Droid integration.

    Please update these guides - I can't be the only one with this problem.

  • LaumaniaLaumania DKMember ✭✭✭

    @JamesMontemagno Finally I found something that worked for Android - and sadly it wasn't MS's docs, but a random dude on YouTube.

    Now I at least get a push notification through, I can work from there :)

  • FrankSzendzielarzFrankSzendzielarz CZMember ✭✭

    @JoLapraline Wish I'd seen your comment a week or two ago. However, I've been playing around with this and got it working on main or background thread. I believe this to be a platform bug. Please see here https://github.com/xamarin/XamarinComponents/issues/925
    Basically the callback overload of the Register methods "fail" on a background thread, but the overload that just directly returns the NSError works fine. I believe this to be some issue with the way callbacks are translated to Tasks on ios, or something of that nature. The "fail" is that method succeeds, but does nothing.

Sign In or Register to comment.