【问题标题】:Not receiving push notification on iOS with Azure Notification Hub- didReceiveRemoteNotification is not called未使用 Azure 通知中心在 iOS 上接收推送通知 - 未调用 didReceiveRemoteNotification
【发布时间】:2013-01-21 01:47:03
【问题描述】:

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

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

  1. 创建了一个新的通知中心:myhubname
  2. 遵循所有证书配置步骤。
  3. 导出私有证书并上传到 沙盒下的通知中心,以确保使用正确的 APS 网关
  4. 已下载配置文件,它与我的项目的捆绑包 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 的重命名版本。

命令运行良好。发生了什么?

【问题讨论】:

    标签: c# azure push-notification apple-push-notifications azure-notificationhub


    【解决方案1】:

    AppleNotificationJsonBuilder 未使用 Service Bus 预览功能的最新 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 通知中心仪表板没有太大帮助

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

    【讨论】:

    • 那么下一个问题,“为什么推送通知似乎很慢到达”? stackoverflow.com/questions/5324729/…
    • 而且我认为它在 APS 中被杀死了。
    • 谢谢。我还注意到注册“模板通知”也不起作用,可能是相关的。不过,使用此解决方法注册本机通知是可行的。
    【解决方案2】:

    试试这个:

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

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

    更复杂的:

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

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

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

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

    【讨论】:

      【解决方案3】:

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

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

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

      鉴于混淆,我们可能会考虑更改此行为,或在序列化时添加 JSON 安全检查。

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

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

      【讨论】:

      • 谢谢 - 我最终暂时使用 JSON.net 作为 JsonConvert.SerializeObject(new { aps = new { alert = "My message", badge = 1 }}) 例如。
      • 我意识到这有点老了,但我遇到了类似的问题。使用 NWPusher,使用我上传到服务总线的证书,通知会很好地显示在设备上。当我在服务总线中使用调试功能时,消息永远不会到达。如果我使用代码通过服务总线发送通知,结果相同。我想也许我的证书不正确,但我重新上传了它以确保它们是相同的。关于在哪里看的任何想法?
      • @Elio,感谢您提供线索。花了几个小时仔细研究各种帖子,仔细检查我的设置,尝试调试无济于事。这是一个格式错误的 json 有效负载。你的帖子成功了。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 2016-07-22
      • 2021-08-03
      • 2015-11-26
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多