Azure Messaging - RegisterTemplateAsync isn't working?

I have my Notification Hub all setup with certificates etc. Everything works when using Hub.RegisterNativeAsync on my iOS app.
I'm using this: http://components.xamarin.com/view/azure-messaging
When I try to use the RegisterTemplateAsync I get the following error:

Error Domain=WindowsAzureMessaging Code=400 "URLRequest failed for { URL: https://XXXXXX.servicebus.windows.net/XXXXXX/Registrations/2534220209409331729-149XXX596012142536-13?api-version=2013-04 } with status code: invalid request" UserInfo=0x18b74bf0 {NSLocalizedDescription=URLRequest failed for { URL: https://XXXXXX.servicebus.windows.net/XXXXXX/Registrations/2534220209409331729-149625596012142536-13?api-version=2013-04 } with status code: invalid request}

The code I'm executing looks like this, and note that the first Native registration is working just fine, so all the connection/certificate etc. should be alright.

`Hub = new SBNotificationHub(ConnectionString, NotificationHubPath);

        Hub.UnregisterAllAsync (deviceToken, (error) => {
            if (error != null) 
            {
                Console.WriteLine("Error calling Unregister: {0}", error.ToString());
                return;
            } 

            NSSet tags = new NSSet(); // create tags if you want
            Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) => 
            {
                if (errorCallback != null)
                    Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
            });

            const string template = "{\"aps\":{\"alert\":\"$(message)\"}}";
            Hub.RegisterTemplateAsync(deviceToken, "tweetAlert", template, "expiryTemplate?", tags, (errorCallback) => 
                {
                    if (errorCallback != null)
                        Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
                });
        });`

I have tried a lot of things but can't make it work - anyone with luck out there?
It seems like it might be related to the actual request, that somehow failed before it were even sent to Azure, but how to fix that? :S

Over all the sample for this component is really bad, as the way to provide "connectionstring/credentials" isn't how the Azure portal shows it to you.
http://components.xamarin.com/gettingstarted/azure-messaging
The sample tells you to provide:
const string HUB_NAME = "XXX";
const string HUB_LISTEN_SECRET = "XXX"

But the Azure Portal talks about "Access Connection" or "Connection Information", which is ONE string which includes BOTH the hub name and the sharedkeys/secret if you like.

I figured this out myself to make the Hub.RegisterNativeAsync work and that's all fine - but still, the Sample looks old and obsolete, then things like this doesn't match. :)

Besides that the Sample doesn't work at all, as the RegisteredForRemoteNotifications isn't called at all, because PushNotifications isn't enabled on the project.

I know this isn't really Xamarins "problem", but for us developers, things gets really confusing when we start out following this guide, which is great until it gets to the Xamarin code part.
http://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-notification-hubs-ios-get-started/
Here they don't mention this Azure Messaging Components...you wonder, should I use it then? Well, it says in the Component description it works for the NotificationHub, so you use it, instead of building your own assembly, as the official NotificationHub tutorial/guide tells you to.

Hope someone who can update the Component, sees this :)

Posts

  • JonathanDickJonathanDick CAXamarin Team, Developer Group Leader Xamurai
    edited June 2014

    @Laumania‌ You're right, we should probably change the sample to only use a single string for connection string, I'll put that on my todo list for updating the sample.

    In the meantime, take a look at the sample code for a talk I did using this component: https://github.com/Redth/Techorama-2014/blob/master/Xamarin-and-Azure-Mobile-Services/MyFridge/MyFridge/Gcm.cs#L43

    Basically, you can use the entire string connection string and just pass that into the ctor of the NotificationHub object. This sample worked in my demo, so I'm wondering if there's something else with your setup that might be causing the error... One suggestion I might have is using RegisterTemplate instead of RegisterTemplateAsync to see if that helps at all (just to rule out one variable)...

    Let me know if that sample helps at all or if you're still having trouble after taking a look at it.

  • MadsLaumannMadsLaumann DKMember ✭✭

    Thanks for your reply!

    Changed to the None-Async method...and that didn't work, BUT, it gave me a error message that I have an idea how to fix.

    Error Response:400Expiry value could not be deserialized to a date time..TrackingId:6fef420f-9555-4417-aab9-0d37cb060740_G16,TimeStamp:6/20/2014 6:09:25 PM

    I'll get back...

  • MadsLaumannMadsLaumann DKMember ✭✭

    Ok, got it working now - thanks for pointing me in the right direction :)

    The problem was that this ExpiryTemplate wasn't clear for me. I did find any docs on that one, but apparently, it's a DateTime formatted as en-us.

    So my final working code looks like this:

    const string template = "{\"aps\":{\"alert\":\"$(message)\"}}"; var expire = DateTime.Now.AddDays(90).ToString(CultureInfo.CreateSpecificCulture("en-US")); Hub.RegisterTemplateAsync(deviceToken, "tweetAlert", template, expire, tags, (errorCallback) => { if (errorCallback != null) Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString()); });

Sign In or Register to comment.