0

Windows Phone 7 デバイスのプッシュ通知サービスについて質問があります。Web アプリケーションを使用して電話にプッシュ通知を送信し、タイルのデータを変更できるようになりました。しかし、問題は、アプリを起動するときに、デバッガーの出力に URI を表示し、それを Web アプリケーション内にコピーして貼り付ける必要があることです。これにより、MPNS に接続されます。更新には問題ありません。単一の電話。しかし、複数の呼び出しを自動的に行うことができる Web サービスを作成し、アプリケーションの URI を取得し (アプリを閉じて開いた後に変更されると思います)、それにプッシュ通知を送信したいと考えています。しかし、MSDN が見つかりませんでした。これを扱うトピックです。彼らは、「後で必要な URI に置き換える」と言って、称賛を使用するだけです。だから私の質問は:電話を使用してそのようなメッセージを Web サービスに送信し、それに応答し、電話に再度接続して、そのような要求を処理するにはどうすればよいですか? また、認証済みの Web サービスが必要ですか、それともデバッグ バージョンはありますか?

これは私がこれまでに持っているものです:

  /// <summary>
    /// Setup a connection with a webservice, in order to update a shell, either a toast- or a tile shell.
    /// </summary>
    /// <param name="shellType">The type of shell you wish to update</param>
    public void SetupShellChannel ( ShellBindType shellType )
    {
        //holds the push notification that is created. Since we can only have one notification channel open at any one time, 
        //we will need to check for existance. That is why, the channelName shouldn't be changed
        HttpNotificationChannel _httpChannel = HttpNotificationChannel.Find( _channelName );

        //if the _httpChannel was not found ( read: does not exist )
        if ( _httpChannel == null )
        {
            _httpChannel = new HttpNotificationChannel( _channelName  );
            _httpChannel.Open( );

            //because there is more than one shelltype we can open, we will use a switch to call the method we seek
            BindToShell( _httpChannel, shellType );
        }
            //Only one push notification service is allowed per application, so we cannot send a tile notification, as well as 
            //a toast message notification. When we attempt this, we get a InvalidOperationException
        else
        { 
            //in this case, the _httpChannel did already exist, but the problem is, we cannot just add the eventHandlers, 
            //because there is the danger that it didn't exist, and we would get a null pointer exception.
            //_httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>( httpChannel_ChannelUriUpdated );
            //_httpChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>( httpChannel_ErrorOccurred );

            //For testing purposes, we now display the URI to the user, and as output. Normally, we would pass this URI back to the webserver
            System.Diagnostics.Debug.WriteLine( _httpChannel.ChannelUri.ToString( ) );
        }

        //if ( _httpChannel.ChannelUri )

        //When the URI is updated, we want this to be sent to the server as well, so we know that the adress has changed, 
        //and don't just send data somewhere into the void. Also, when encountering an error, we want to show the user when 
        //an error has occured.
        _httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>( HttpChannel_ChannelUriUpdated );
        _httpChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>( HttpChannel_ErrorOccurred );
    }

    //here, also we would return the URI to the server, but for debugging purposes, we display them to the user.
    void HttpChannel_ChannelUriUpdated( object sender, NotificationChannelUriEventArgs e )
    {
        Deployment.Current.Dispatcher.BeginInvoke( ( ) => 
        {
            System.Diagnostics.Debug.WriteLine( e.ChannelUri.ToString( ) );
            MessageBox.Show( String.Format( "the URI is {0}", e.ChannelUri.ToString( ) ) );
        } );
    }

    private void BindToShell( HttpNotificationChannel channel, ShellBindType shellType )
    {
        switch ( shellType )
        {
            case ShellBindType.BindToShellTile:
                channel.BindToShellTile( );
                break;
            case ShellBindType.BindToShellToast:
                channel.BindToShellToast( );
                break;
        }        
    }

    void HttpChannel_ErrorOccurred( object sender, NotificationChannelErrorEventArgs e )
    {
        //getting an error would be caugth here, and then displayed to the user.
        Deployment.Current.Dispatcher.BeginInvoke( ( ) =>
            {
                MessageBox.Show( String.Format( "A push notification {0} error occured. {1}{(2)}{3}", 
                    e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData ) );
            } );
    }
4

2 に答える 2

1

わかりました、あなたの質問を理解しました。私が行ったことは、MPNS から URI を取得したら、これをパラメーターとしてサービスで Web メソッドを呼び出すことです - Subscribe(int subscriberId, Uri channelUri);

そのため、アプリでsubscriberIdを生成してユーザーを識別し、Isolated Storageに保存する必要があります。これは GUID にすることができます。

サブスクライバから Uri へのマッピングを永続的なストレージに保存する責任は、サーバーにあります。

また、ユーザーがプッシュ通知をオプトアウトするには、UnSubscribe メソッドを提供する必要があります。これは、プッシュ通知の認証要件の 1 つです。

2番目の質問について-はい、サービスを保護する必要があります-不明なリクエストを処理したくない.

私が個人的に行っていることは、出版サービスと購読サービスの 2 つのサービスに分けられます。発行サービスは通知を送信しますが、サブスクリプションにはサブスクライブ/サブスクライブ解除メソッドがあります。

于 2012-02-08T06:17:41.897 に答える
0

MSDN のサンプル アプリケーションで説明されているように、Windows Phone 自体からプッシュ通知を送信できるか、代わりに他のサーバー側の ASP/PHP を使用できないかを尋ねようとしていると思います。はい。電話/デバイス自体から通知を送信できます。MSDN に記載されているサンプル アプリの送信機能を変更するだけです。質問があれば返信してください。

static async Task<string> SendPushNotification(string textToSend)
{
    //You can maintain a DB to query different channel URIs of devices
    string subscriptionUri = "<Uri To Which You Want Send Notification>";
    HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
    sendNotificationRequest.Method = "POST";

    string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<wp:Notification xmlns:wp=\"WPNotification\">" +
           "<wp:Toast>" +
                "<wp:Text1>" + textToSend + "</wp:Text1>" +
                "<wp:Param>/NotificationDetails.xaml?Message=" + textToSend + "</wp:Param>" +
           "</wp:Toast> " +
        "</wp:Notification>";
    byte[] notificationMessage = Encoding.UTF8.GetBytes(toastMessage);

    sendNotificationRequest.ContentLength = notificationMessage.Length;
    sendNotificationRequest.ContentType = "text/xml";
    sendNotificationRequest.Headers["X-WindowsPhone-Target"] = "toast";
    sendNotificationRequest.Headers["X-NotificationClass"] = "2";

    using (var requestStream = await Task.Factory.FromAsync<Stream>(sendNotificationRequest.BeginGetRequestStream, sendNotificationRequest.EndGetRequestStream, null))
    {
        requestStream.Write(notificationMessage, 0, notificationMessage.Length);
    }

    string notificationStatus;
    using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(sendNotificationRequest.BeginGetResponse, sendNotificationRequest.EndGetResponse, null)))
    {
        //StreamReader reader = new StreamReader(response.GetResponseStream());
        //result = reader.ReadToEnd();
        notificationStatus = response.Headers["X-NotificationStatus"];
        MessageBox.Show(notificationStatus);
    }
    return notificationStatus;
}
于 2014-08-18T12:05:45.810 に答える