Remote notifications walkthrough - 401 Unauthorized on sending

YumeYume USMember
edited June 2016 in Xamarin.Android

Hello everyone.

I'm currently learning how to handle notifications using Xamarin, and working on a Class Library project that will handle that functionality for my future apps.

To learn how to do so, I'm using the Remote notifications Walkthrough from the Xamarin docs. Adapting the code a bit so it can fit in a DLL and be used by a test app, I've been able to generate that dll, add a reference to it in my test app, then send a token request to GCM, get my token, and using a web API, store it with some other user informations in a database.

The problem comes with the last part of the walkthrough : the MessageSender. I added a new project to my Test App solution, and used the code found in the documentation, replacing of course the API key with my own key, that I got from the API console, passing the package name and SHA1 from keytool.

So, I run my test app on my device (everything used here is on the same network, computer, phone and my custom API). I look in the output : the token arrived correctly, and my API registered it in database correctly. Double checked in database, and he's here. Then I launch MessageSender.exe using VS2015 dev console, and it prints this :

System.Threading.Tasks.Task`1[System.Net.Http.HttpResponseMessage]
Message sent: check the client device notification tray.

I go "yay working !", but a few minutes later, still nothing on my app. Looking to the MessageSender console, the Console.Writeline is printing an object type. So, i changed it from Console.WriteLine(response); to Console.WriteLine(response.Result);. And here's the new printing :

StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
    X-Content-Type-Options: nosniff
    X-Frame-Options: SAMEORIGIN
    X-XSS-Protection: 1; mode=block
    Alternate-Protocol: 443:quic
    Alt-Svc: quic=":443"; ma=2592000; v="34,33,32,31,30,29,28,27,26,25"
    Vary: Accept-Encoding
    Transfer-Encoding: chunked
    Accept-Ranges: none
    Cache-Control: max-age=0, private
    Date: Tue, 14 Jun 2016 07:57:07 GMT
    Server: GSE
    Content-Type: text/html; charset=UTF-8
    Expires: Tue, 14 Jun 2016 07:57:07 GMT
}
Message sent: check the client device notification tray.

This is why the notification never showed up : the message never left the sender. But the used code is the exact code from the documentation. I double checked my API key, it's the good one. I also thought about the three TCP ports that have to be opened, but they normally are, and I worked on an app a few weeks ago that was using notifications with GCM, and it was working good. Plus, without the ports opened, that error would be 404.

Any clues about why this happens ? What am I missing ? Thanks in advance !

Posts

  • @Yume Did you manage to solve this issue?

  • YumeYume USMember
    edited July 2016

    @WendelFabianChinsamy Yes, I found a solution, but it is completely different from what is done in the walkthrough I linked above. Instead of using this method, I use a reference to GCM.Client (which you can have by adding the Google Cloud Messaging Client component to your project, then adding a reference to Gcm.Client in your project references, and add using Gcm.Client to your C# file.

    Then, create another C# class file that will contain two classes :

    [BroadcastReceiver (Permission = Constants.PERMISSION_GCM_INTENTS)]
     [IntentFilter (new [] { Intent.ActionBootCompleted })] // Allow GCM on boot and when app is closed
     [IntentFilter (new [] { Constants.INTENT_FROM_GCM_MESSAGE })]
     [IntentFilter (new [] { Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK })]
     [IntentFilter (new [] { Constants.INTENT_FROM_GCM_LIBRARY_RETRY })]
     public class GcmBroadcastReceiver : GcmBroadcastReceiverBase<GcmService>
     {
         public static string [] SENDER_IDS = new string [] { "YOUR SENDER ID" };
     }
    
    [Service]
    public class GcmService : GcmServiceBase
    {
        /*Adding the inheritance will prompt you that the class should implement some methods,
     and Intellisense allows you to automatically add them. You should have OnRegistered, OnUnregistered, 
    OnMessage and OnError. OnRegistered is the listener called when you receive your token from GCM,
     then do what you want with it, add it to database for example. OnMessage is called whenever a
     notification is submitted to your token. Build the notification in here. */
    }
    

    Then, when you need the token, you just call GcmClient.Register(this, "YOUR SENDER ID"); where this is the activity context. You should have OnRegistered called soon after this method is called.

    I noticed that the token I got from the walkthrough method was slightly different, with a few more characters in the beginning. I suppose it is normal behavior, but is never mentionned in the walkthrough. Using the above method got everything working :smile:

    Feel free to ask if anything is unclear.

  • @Yume thanks so much for taking the time to help me. Actually turned out for me that I needed to delete the existing app on my phone. A day of banging my head against a wall.

Sign In or Register to comment.