2

Im having a bit of issue setting up a console application that will send push notifications to iOS/Android devices.

Error shown is below:

Unhandled Exception: System.AggregateException: One or more errors
occurred. ---System.UnauthorizedAccessException: The remote server
returned an error: (401)
Unauthorized..TrackingId:ea158550-8761-41f8-821d-dbcb88fcce56,TimeStamp:18/07/2016
08:58:21 ---System.Net.WebException: The remote server returned an
error: (401) Unauthorized.    at
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)   
at
Microsoft.Azure.NotificationHubs.NotificationRequestAsyncResult`1.<GetAsyncSteps>b__3(TAsyncResult
thisPtr, IAsyncResult r)    at
Microsoft.Azure.NotificationHubs.Messaging.IteratorAsyncResult`1.StepCallback(IAsyncResult
result)    --- End of inner exception stack trace ---

Server stack trace:


Exception rethrown at [0]:    at
Microsoft.Azure.NotificationHubs.Common.AsyncResult.End[TAsyncResult](IAsyncResult
result)    at
Microsoft.Azure.NotificationHubs.NotificationHubManager.EndSendNotification(IAsyncResult
result)    at
System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult
iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean
requiresSynchronization)    --- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean
includeTaskCanceledExceptions)    at
System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout,
CancellationToken cancellationToken)    at
System.Threading.Tasks.Task.Wait()    at
XamarinAzurePushNotification.Server.Program.Main(String[] args) in
B:\Personal Development\Visual Studio
2015\AzurePushNotifications\XamarinAzurePushNotification.Server\Program.cs:line
26

Here is my code for the console application:

private static void Main(string[] args)
{
    var hub = NotificationHubClient.CreateClientFromConnectionString("Endpoint=sb://ConnectionStringName.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=ConnectionStringKey", "HubName");

    var iOS_alert = "{\"aps\":{\"alert\":\"Hello. This is a iOS notification! Tada!\", \"sound\":\"default\"}}";
    hub.SendAppleNativeNotificationAsync(iOS_alert).Wait();

    var android_alert = "{\"message\": \"Can I interest you in a once in a lifetime push notification?!\", \"title\":\"Ding, dong!\"}";
    hub.SendGcmNativeNotificationAsync(android_alert).Wait();

    Console.WriteLine("MESSAGE SENT");
}

If anyone could give me a hand with this it'd be great.

Nikita R.
  • 7,245
  • 3
  • 51
  • 62
ADuggan
  • 501
  • 2
  • 8
  • 20
  • Can you verify if your API Key is a Server Key? See my [answer here](http://stackoverflow.com/a/37801206/4625829). – AL. Jul 18 '16 at 10:09
  • You see I have tested my application in Azure by using there test push notification function and my app is receiving notifications but when i tried to implement the same in my console application(server) it just gives that error. – ADuggan Jul 18 '16 at 10:17
  • I just checked there now and it seems that I am already using the server key. – ADuggan Jul 18 '16 at 10:18
  • That's odd.. Have you also checked the Recommended Actions in the [official doc](https://developers.google.com/cloud-messaging/http-server-ref#error-codes) for Error Code 401? – AL. Jul 18 '16 at 10:44
  • I have but it doesn't seem to help me much... – ADuggan Jul 18 '16 at 12:08
  • Did you try sending a simple curl request? Can you post the response? – AL. Jul 18 '16 at 12:29

2 Answers2

8

It looks like you are using the DefaultListenSharedAccessSignature; instead use the DefaultFullSharedAccessSignature.

mrtnkrstn
  • 318
  • 5
  • 15
1

If any of the answers above did not work, please check if the server/PC has the correct Date Time configured. In my case, on a client, the server had wrong Date Time settings, and I got the Error:

Error Code: 0x17 System.AggregateException: One or more errors occurred. ---> System.UnauthorizedAccessException: The remote server returned an error: (401) Unauthorized. ExpiredToken: .TrackingId:4c2cdbg3-e1a8-4240-b963-d6f55e8f2c76_G29,TimeStamp:8/23/2018 8:58:27 AM ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at Microsoft.Azure.NotificationHubs.NotificationRequestAsyncResult1.<>c.<GetAsyncSteps>b__26_3(TAsyncResult thisPtr, IAsyncResult r) at Microsoft.Azure.NotificationHubs.Messaging.IteratorAsyncResult1.StepCallback(IAsyncResult result) --- End of inner exception stack trace ---

Very frustrating until I found the problem.

MikeMakos
  • 61
  • 8