Not receiving push notification on iOS with Azure Notification Hub-didReceiveRemoteNotification not being called

I have followed these Notification Hub resources and have not received successful push notification requests on my iOS device.

I checked, rechecked and rechecked my setup:

  • New notification center created: myhubname
  • Follow all steps to prepare the certificate. Development Push SSL Certificate
  • The private certificate is exported and uploaded to the Notification Hub in the Sandbox to ensure correct use of the APS gateway
  • Downloaded a provisioning profile that matches the bundle id for my project, auto-detected for code signing and compiles successfully. DO NOT use "V2D7FJQXP". this line that shows up in your app id, if you're interested: V2D7FJQXP.com.yourcompany.yourproject
  • Working on a physical device - not under a simulator

Demo app that generates push notifications:

while (true)
{
    string connectionString = ServiceBusConnectionStringBuilder.CreateUsingSharedAccessSecretWithFullAccess("myhubname-ns", "…snip");
    var hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, "myhubname");
    var iosPayload = AppleNotificationJsonBuilder.Create("Hello!");
    iosPayload.CustomFields.Add("inAppNotification", "Hello!");
    hubClient.SendAppleNativeNotification(iosPayload.ToJsonString());

    Console.WriteLine("Sent");

    Thread.Sleep(20000);
}

      

No exceptions or problems are thrown. Mangling any of the namespace, keys or hostnames throws exceptions and the script response code from Azure is 2xx, so everything looks good.

I can see that the registration is happening correctly with the following code:

  • I have accepted push notifications on the device
  • I see the call to createDefaultRegistrationWithTags once, and then I see that true exists on subsequent calls.
  • No calls for didFailToRegisterForRemoteNotificationsWithError which is ok
  • In the sample code here: http://msdn.microsoft.com/library/jj927168.aspx , I replaced sb: // with https: // as it will throw otherwise. No problem I think

:

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken {
NSString* connectionString = [SBConnectionString stringWithEndpoint:@"https://gift-ns.servicebus.windows.net/" listenAccessSecret:@"…snip"];
self.hub = [[SBNotificationHub alloc] initWithConnectionString:connectionString notificationHubPath:@"gift-usw-dev"];
[self.hub refreshRegistrationsWithDeviceToken:deviceToken completion:^(NSError* error) {
    if (error == nil) {
        [self.hub defaultRegistrationExistsWithCompletion:^(BOOL exists, NSError* error2) {
            if (error2 == nil) {
                if (!exists) {
                    [self.hub createDefaultRegistrationWithTags:nil completion:^(NSError* error3) {
                        if (error3 != nil) {
                            NSLog(@"Error creating default registration: %@", error3);
                        }
                    }];
                }
            } else {
                NSLog(@"Error retrieving default registration: %@", error2);
            }
        }];
    } else {
        NSLog(@"Error refreshing device token: %@", error);
    }
}];
}

      

After launching the demo app, then launch the iOS app, here is the resulting dashboard that I don't know how to read effectively about. enter image description here

Thinking that my certificates weren't correct, or something was lost between Azure and APS, I dug into APS troubleshooting and found this: http://developer.apple.com/library/ios/#technotes/ tn2265 / _index.html and went to Problems connecting to Push service

And ran this command:

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert YourSSLCertAndPrivateKey.pem -debug -showcerts -CAfile server-ca-cert.pem

But I didn't have the .pem file (OSX), so I found this command to get them:

openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes

where keyStore.pfx is the renamed .p12 version exported from Keychain for push push certificate.

The command completed normally. What's happening?

+3


source to share


3 answers


AppleNotificationJsonBuilder does not serialize the payload correctly using the latest Service Bus preview features.

So, in the examples from the msdn instructions:

var iosPayload = AppleNotificationJsonBuilder.Create("Hello!");
iosPayload.CustomFields.Add("inAppNotification", "Hello!");
Console.Log(iosPayload.ToJsonString());

      

Forms:

{"aps":{"alert":"This is a test"},"inAppNotification":Hello! This is a test}

      

This is not true. Well-formed "string"



{"aps":{"alert":"This is a test"},"inAppNotification":"Hello! This is a test"}

      

Custom payload in order http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html

One solution is to use Json.NET or ping someone inside Microsoft.

Open problems:

  • How could I monitor network traffic from an iOS device?
  • Has "JSON" reached the device but parsed incorrectly? Or was he killed in the APS?
  • Azure Notification Hub dashboard didn't help.

Hope this saves someone in the afternoon.

+1


source


I work on the Service Bus team in the Notification Hub feature. As you already figured out, the payload is not formatted correctly.

Custom fields handle JSON encoded strings, so you can specify integer and objects as custom fields, for example: iosPayload.CustomFields.Add ("inAppNotification", "{\" my \ ": {\" new \ ":" jsonObject "}}");

So, if you want to add a string, you have to put json encoded string. iosPayload.CustomFields.Add ("inAppNotification", "\" Hello! \ "");



Given this confusion, we might consider changing this behavior or adding JSON security checks during serialization.

Regarding the Hub notification bar. Notification Agents notify of successful notifications when APNs receive a notification. You can also visualize a graph for Invalid Payload, which will show if the APN has rejected the notification from the notification hub.

Unfortunately I am not aware of a way to track traffic on the device, other than to capture a notification when the application is running with the: didReceiveRemoteNotification callback.

+3


source


Try the following:

var iosPayload = AppleNotificationJsonBuilder.Create("This is a test");
iosPayload.CustomFields.Add("inAppNotification", "\"Hello!\"");

      

-> {"aps": {"alert": "This is a test"}, "inAppNotification": "Hello!" }

More complex ones:

iosPayload.CustomFields.Add("inAppNotification", "[ \"bang\",  \"whiz\" ]");

      

-> {"aps": {"alert": "This is a test"}, "inAppNotification": ["bang", "whiz"]}

iosPayload.CustomFields.Add("inAppNotification", "42");

      

-> {"aps": {"alert": "This is a test"}, "inAppNotification": 42}

+1


source







All Articles