0

항상 404 오류가 발생합니다. 아래는 wcf 서비스에서 토스트 유형의 푸시 알림을 보내는 코드입니다. 메시지에 문제가 있습니까?wp7에서 토스트 푸시 알림을 보낼 수 없습니다.

 string channelURI = "http://db3.notify.live.net/throttledthirdparty/01.00/AgAAAAQUZm52OjBEMTRBNDEzQjc4RUFBRTY"; 
     HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(channelURI); 

     //Indicate that you'll send toast notifications! 
     sendNotificationRequest.ContentType = "text/xml"; 

     sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast"); 
     sendNotificationRequest.Headers.Add("X-NotificationClass", "2"); 

     sendNotificationRequest.Method = "POST"; 

     sendNotificationRequest.Headers.Add("X-MessageID",Guid.NewGuid().ToString()); 

     if (string.IsNullOrEmpty(message)) return "empty cannot be sent"; 

     //send it 
     var msg = string.Format("sample toast message", "Toast Message", "This is from server"); 

     byte[] notificationMessage = Encoding.UTF8.GetBytes(msg); 
     sendNotificationRequest.ContentLength = notificationMessage.Length; 



     //Push data to stream 
     using (Stream requestStream = sendNotificationRequest.GetRequestStream()) 
     { 
      requestStream.Write(notificationMessage, 0, notificationMessage.Length); 
     } 


     //Get reponse for message sending 
     HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse(); 
     string notificationStatus = response.Headers["X-NotificationStatus"]; 
     string notificationChannelStatus = response.Headers["X-SubscriptionStatus"]; 
     string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"]; 
     return notificationStatus; 

답변

0

이 코드는 NotificationType 여기에 무엇입니까 당신에게

private static byte[] prepareToastPayload(string text1, string text2) 
    { 
     MemoryStream stream = new MemoryStream(); 

     XmlWriterSettings settings = new XmlWriterSettings() { Indent = true, Encoding = Encoding.UTF8 }; 
     XmlWriter writer = XmlTextWriter.Create(stream, settings); 
     writer.WriteStartDocument(); 
     writer.WriteStartElement("wp", "Notification", "WPNotification"); 
     writer.WriteStartElement("wp", "Toast", "WPNotification"); 
     writer.WriteStartElement("wp", "Text1", "WPNotification"); 
     writer.WriteValue(text1); 
     writer.WriteEndElement(); 
     writer.WriteStartElement("wp", "Text2", "WPNotification"); 
     writer.WriteValue(text2); 
     writer.WriteEndElement(); 
     writer.WriteEndElement(); 
     writer.WriteEndDocument(); 
     writer.Close(); 

     byte[] payload = stream.ToArray(); 
     return payload; 
    } 
private void SendMessage(Uri channelUri, byte[] payload, NotificationType notificationType, SendNotificationToMPNSCompleted callback) 
    { 
     //Check the length of the payload and reject it if too long 
     if (payload.Length > MAX_PAYLOAD_LENGTH) 
      throw new ArgumentOutOfRangeException(
     "Payload is too long. Maximum payload size shouldn't exceed " + MAX_PAYLOAD_LENGTH.ToString() + " bytes"); 

     try 
     { 
      //Create and initialize the request object 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelUri); 
      request.Method = WebRequestMethods.Http.Post; 
      request.ContentType = "text/xml; charset=utf-8"; 
      request.ContentLength = payload.Length; 
      request.Headers[MESSAGE_ID_HEADER] = Guid.NewGuid().ToString(); 
      request.Headers[NOTIFICATION_CLASS_HEADER] = ((int)notificationType).ToString(); 

      if (notificationType == NotificationType.Toast) 
       request.Headers[WINDOWSPHONE_TARGET_HEADER] = "toast"; 
      else if (notificationType == NotificationType.Token) 
       request.Headers[WINDOWSPHONE_TARGET_HEADER] = "token"; 

      request.BeginGetRequestStream((ar) => 
      { 
       //Once async call returns get the Stream object 
       Stream requestStream = request.EndGetRequestStream(ar); 

       //and start to write the payload to the stream asynchronously 
       requestStream.BeginWrite(payload, 0, payload.Length, (iar) => 
       { 
        //When the writing is done, close the stream 
        requestStream.EndWrite(iar); 
        requestStream.Close(); 

        //and switch to receiving the response from MPNS 
        request.BeginGetResponse((iarr) => 
        { 
         using (WebResponse response = request.EndGetResponse(iarr)) 
         { 
          //Notify the caller with the MPNS results 
          OnNotified(notificationType, (HttpWebResponse)response, callback); 
         } 
        }, 
        null); 
       }, 
       null); 
      }, 
      null); 
     } 
     catch (WebException ex) 
     { 
      if (ex.Status == WebExceptionStatus.ProtocolError) 
      { 
       //Notify client on exception 
       OnNotified(notificationType, (HttpWebResponse)ex.Response, callback); 
      } 
      throw; 
     } 
    } 

protected void OnNotified(NotificationType notificationType, HttpWebResponse response, SendNotificationToMPNSCompleted callback) 
    { 
     CallbackArgs args = new CallbackArgs(notificationType, response); 
     if (null != callback) 
      callback(args); 
    } 
public class CallbackArgs 
{ 
    public CallbackArgs(NotificationType notificationType, HttpWebResponse response) 
    { 
     this.Timestamp = DateTimeOffset.Now; 
     this.MessageId = response.Headers[NotificationSenderUtility.MESSAGE_ID_HEADER]; 
     this.ChannelUri = response.ResponseUri.ToString(); 
     this.NotificationType = notificationType; 
     this.StatusCode = response.StatusCode; 
     this.NotificationStatus = response.Headers[NotificationSenderUtility.NOTIFICATION_STATUS_HEADER]; 
     this.DeviceConnectionStatus = response.Headers[NotificationSenderUtility.DEVICE_CONNECTION_STATUS_HEADER]; 
     this.SubscriptionStatus = response.Headers[NotificationSenderUtility.SUBSCRIPTION_STATUS_HEADER]; 
    } 

    public DateTimeOffset Timestamp { get; private set; } 
    public string MessageId { get; private set; } 
    public string ChannelUri { get; private set; } 
    public NotificationType NotificationType { get; private set; } 
    public HttpStatusCode StatusCode { get; private set; } 
    public string NotificationStatus { get; private set; } 
    public string DeviceConnectionStatus { get; private set; } 
    public string SubscriptionStatus { get; private set; } 
} 
public enum NotificationType 
{ 
    Token = 1, 
    Toast = 2, 
    Raw = 3 
} 
+0

을 도와 드릴까요? 위의 코드에 추가하지 않은 클래스가 있습니까? public NotificationType NotificationType {get; 개인 집합; } – krrishna

+0

업데이트 된 코드를 참조하십시오. – Mahantesh

+0

http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202967(v=vs.105).aspx이 링크는 알림을 보내는 데 도움이됩니다. – krrishna

관련 문제