1

望ましい動作は、アプリで生の通知をインターセプトすることです (バックグラウンド タスクやユーザーが貴重なバックグラウンド タスク スポットの 1 つを放棄する必要はありません)。http://msdn.microsoft.com/en-us/library/windows/apps/xaml/JJ709907(v=win.10).aspxのサンプル コードに従って、生の通知をインターセプトするイベント ハンドラーを作成しました。私が作成した 1 つのアプリでは、Azure Mobile Services を使用して WNS 経由で通知を送信し、イベント ハンドラーがトリガーされ、うまく機能します。

しかし、2 つ目のアプリでは機能しません。通知は問題ないようです。つまり、Web サービスが WNS を取得してトースト通知を送信すると、トースト通知が表示されます。また、未加工の通知をデバッグ/トレースすることもでき、それらも問題なく作成されているように見えます。不足している設定はありますか?

Fiddler 経由で要求と結果が表示されますが、MVC 4.0 Web API プロジェクトをローカルで実行しているからかもしれません。リクエストは次のようになります。

{Channel URI:              https://bn1.notify.windows.com/?token=AgYAAACuGgtx...
TTL:                      -
Cache:                    no
Request for status:       no
Tag:                      
Priority:                 Normal
Token retry count:        0

really
}

次に、これを追跡できます (すべて、WAT サンプルの一部である WNSRecipe/NotificationsExtensions コードを使用して実行されます)。私は得る:

{Sent WNS notification: Received
Channel URI:              https://bn1.notify.windows.com/?token=AgYAAACuGgtx...
Notification status:      Received
Status code:              OK
Device connection status: NotApplicable
Error description:        
Debug Trace:              BN1WNS2011828
MessageId:                3FA318CE5C48E9CF
Timestamp:                3/8/2013 9:23:18 PM -07:00


- REQUEST ------------------------------------------------------------------
X-WNS-Type                    : wns/raw
Content-Type                  : application/octet-stream
Authorization                 : Bearer EgAaAQMAAAAEgAAACoAAx1d3DqT9jZxJdOFIUJ9...
Host                          : bn1.notify.windows.com
Content-Length                : 6

really

- RESPONSE -----------------------------------------------------------------
X-WNS-NOTIFICATIONSTATUS      : received
X-WNS-MSG-ID                  : 3FA318CE5C48E9CF
X-WNS-DEBUG-TRACE             : BN1WNS2011828
Content-Length                : 0
Date                          : Sat, 09 Mar 2013 04:23:11 GMT
}

そして結果:

{Channel URI:              https://bn1.notify.windows.com/?token=AgYAAACuGgtx...
Notification status:      Received
Status code:              OK
Device connection status: NotApplicable
Error description:        
Debug Trace:              BN1WNS2011828
MessageId:                3FA318CE5C48E9CF
Timestamp:                3/8/2013 9:23:18 PM -07:00
}

したがって、通知が送信されていると思います。

更新: 戻って設定を再確認し、リクエストを POST するためのテスト ボタンをアプリに追加しました。(だから私はアプリがアクティブであることを知っています、間違いありません)。通知リクエストに追加RequestForStatus = trueしたところ、「デバイスの接続ステータス: 接続済み」と、イベント ビューアーに新しいログ エントリ セットが返されました。

4

1 に答える 1

1

考えられるあらゆる種類の問題/解決策について頭を悩ませた後、最終的に何が間違っているのかを見つけました。原因は、私のイベント ハンドラー コードにありました。何らかの理由で、NewtonSoft JObject を使用するコードは、私の別のテスト プロジェクトでは問題なく動作しましたが、このプロジェクトでは厄介で静かな方法で失敗していました。私は ServiceStack.Text を使用するように切り替えました(これは、はるかに高速であると言われているため、とにかく意味がありました...今後は、簡潔な生の通知に加えて、アプリとの間でより多くのデータをパイプします)。

最初に最も単純なコードに戻り、次のことだけを行いました。

private async void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
    if (e.NotificationType == PushNotificationType.Raw)
    {
        e.Cancel = true;

        String notificationContent = String.Empty;

        notificationContent = e.RawNotification.Content;
    }
}

これは、(FYI)次のように見える問題のメソッドを置き換えました。

private async void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
    String notificationContent = String.Empty;

    switch (e.NotificationType)
    {
        case PushNotificationType.Badge:
        case PushNotificationType.Tile:
        case PushNotificationType.Toast:
            notificationContent = e.TileNotification.Content.GetXml();
            break;


        case PushNotificationType.Raw:
            notificationContent = e.RawNotification.Content;
            break;
    }

    e.Cancel = true;


    try
    {
        JObject jObject = JObject.Parse(notificationContent);

        JToken jToken;

        jObject.TryGetValue("refresh", out jToken);

        bool refreshValue = jToken != null && Convert.ToBoolean(jToken.ToString());

        if (refreshValue)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, RefreshTodoItems);
        }
    }
    catch (Exception err)
    {
        Debug.WriteLine(err.Message);
    }
}

最後に、私はこれで終わりました。これはうまく機能します:

private async void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            if (e.NotificationType != PushNotificationType.Raw) return;
            e.Cancel = true;

            try
            {
                // Use ServiceStack.Text.WinRT to get this
                var jsonObj = JsonSerializer.DeserializeFromString<JsonObject>(e.RawNotification.Content);

                // TODO: Do something with this value now that it works
            }
            catch (Exception err)
            {
                Debug.WriteLine(err.Message);
            }
        });
}
于 2013-03-12T11:40:20.887 に答える