3

我已关注这些通知中心资源,但未能成功在我的 iOS 设备上接收推送通知。

我已经检查、重新检查并重新检查了我的设置:

  1. 创建了一个新的通知中心:myhubname
  2. 遵循所有证书配置步骤。 开发推送 SSL 证书
  3. 导出私有证书并上传到Sandbox下的 Notification Hub以确保使用正确的 APS 网关
  4. 下载了配置文件,它与我的项目的 bundle id 匹配,自动检测到代码签名,并成功编译。不要使用“V2D7FJQXP”。如果您想知道,该字符串会显示在您的 App ID 中:V2D7FJQXP.com.yourcompany.yourproject
  5. 在物理设备上运行 - 不在模拟器下

生成推送通知的演示应用程序:

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);
}

不会产生异常或问题。修改任何命名空间、键或集线器名称都会引发异常,并且来自 Azure 的提琴手响应代码是 2xx,所以一切看起来都很好。

我看到注册正确发生在下面的代码中:

  • 我接受了设备上的推送通知
  • 我看到一次 createDefaultRegistrationWithTags 调用,然后在后续调用中看到存在是真的。
  • 没有调用 didFailToRegisterForRemoteNotificationsWithError,这没问题
  • 在此处的代码示例中:http: //msdn.microsoft.com/library/jj927168.aspx,我已将 sb:// 替换为 https://,否则会抛出异常。非问题我在想

-(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);
    }
}];
}

启动演示应用程序,然后运行 ​​iOS 应用程序后,这是生成的仪表板,我不知道如何有效阅读。 在此处输入图像描述

认为我的证书不正确,或者 Azure 和 APS 之间丢失了某些东西,我深入研究了 APS 服务的故障排除并发现了这个:http: //developer.apple.com/library/ios/#technotes/tn2265/_index.html并跳转到连接到推送服务的问题部分

并运行以下命令:

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

但是我没有 .pem 文件(OSX),所以我找到了这个命令来获取它们:

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

其中 keyStore.pfx 是从钥匙串导出的 .p12 的重命名版本,用于推送通知证书。

命令运行良好。怎么了?

4

3 回答 3

3

我在通知中心功能的服务总线团队工作。正如您已经发现的那样,有效负载的格式不正确。

CustomFields 采用 JSON 编码的字符串,因此您可以将整数和对象设置为自定义字段,例如: iosPayload.C​​ustomFields.Add("inAppNotification", "{ \"my\": {\"new\" : "jsonObject" }}");

所以如果你想添加一个字符串,你必须输入一个 json 编码的字符串。iosPayload.C​​ustomFields.Add("inAppNotification", "\"Hello!\"");

考虑到混淆,我们可能会考虑改变这种行为,或者在序列化时添加 JSON 安全检查。

关于通知中心仪表板。当 APNs 接受通知时,通知中心会报告成功的通知。您还可以可视化“无效负载”的图表,该图表将显示 APNs 是否拒绝了来自通知中心的通知。

不幸的是,除了在应用程序运行时使用回调捕获通知之外,我不知道监控设备流量的方法:didReceiveRemoteNotification。

于 2013-02-06T23:44:05.857 回答
1

AppleNotificationJsonBuilder 没有使用最新的 nuget服务总线预览功能正确序列化有效负载。

因此,根据 msdn 指令中的示例:

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

生成:

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

这是畸形的。格式良好的是“字符串”

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

根据http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html,自定义有效负载很好

一种解决方案是使用 Json.NET,或者在 Microsoft 内部 ping 某人。

开放式问题:

  • 我如何监控 iOS 设备上的网络流量?
  • “JSON”是否到达设备,但解析不正确?还是它在 APS 中被杀死?
  • Azure 通知中心仪表板没有太大帮助

希望这可以节省某人的下午。

于 2013-02-06T00:04:54.990 回答
1

尝试这个:

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

--> {"aps":{"alert":"这是一个测试"},"inAppNotification":"Hello!"}

更复杂的:

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

--> {"aps":{"alert":"这是一个测试"},"inAppNotification":[ "bang", "whiz" ]}

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

--> {"aps":{"alert":"这是一个测试"},"inAppNotification":42}

于 2013-02-06T23:41:56.420 回答