2017-06-20 63 views
1

我使用PushSharp 4.0.10.0庫來發送iOS設備上的通知,但它不起作用。我調試了它,發現有一些ApnsConfiguration連接問題。Pushsharp 4.0.10.0 iOS設備令牌的ApnsConfiguration連接錯誤

我使用此代碼發送notificaiton:

public IHttpActionResult Notify() 
    { 
     HttpResponseMessage response = new HttpResponseMessage(); 
     HttpContent requestContent = Request.Content; 
     string errorMessage = "Some error occured.please try again later"; 
     HttpStatusCode responseCode = HttpStatusCode.Unauthorized; 
     string requestParameter = requestContent.ReadAsStringAsync().Result; 
     string tokan = ""; 
     var r = Request; 
     var header = r.Headers; 
     try 
     { 
      if (requestParameter != null) 
      { 
       PushNotificationModel complaintModel = JsonConvert.DeserializeObject<PushNotificationModel>(requestParameter); 
       JsonConvert.DeserializeObject<PushNotificationModel>(requestParameter); 
       var appleCert = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Images/User/xyz.pem")); 
       var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, 
           appleCert, "xyz"); 

       // Create a new broker 
       var push = new ApnsServiceBroker(config); 

       int DeviceType = 1; 
       string deviceId = Convert.ToString(complaintModel.deviceToken); 
       string message = "New notification!!"; 
       Guid complaintId = complaintModel.ComplaintId; 
       string detail = complaintModel.detail; 
       try 
       { 
        //System.Web.Hosting.HostingEnvironment.MapPath("~/Images/User/") 
        // var appleCert = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Images/User/CTPwd.pem")); 
        push.OnNotificationFailed += (notification, aggregateEx) => 
        { 

         aggregateEx.Handle(ex => 
         { 

          // See what kind of exception it was to further diagnose 
          if (ex is ApnsNotificationException) 
          { 

           message = ex.Message; 
          } 
          else 
          { 
           message = "Not an APNSException"; 
          } 

          // Mark it as handled 
          return true; 
         }); 
        }; 
        try 
        { 
         push.OnNotificationSucceeded += (notification) => 
         { 
          message = "New Notification"; 
         }; 
         push.Start(); 
         string appleJsonFormat = "{\"aps\": {\"alert\":" + '"' + message + '"' + ",\"sound\": \"default\"}}"; 
         //string appleJsonFormat = "{\"aps\": {\"alert\": " + "Hello World" + ",\"sound\": \"default\"}}"; 
         push.QueueNotification(new ApnsNotification 
         { 
          DeviceToken = deviceId, 
          Payload = JObject.Parse(appleJsonFormat) 
         }); 

         push.Stop(); 
        } 
        catch(Exception ex) 
        { 

        } 

我已搜查谷歌,但沒有發現任何相關的答案。有沒有語法問題?

在此先感謝。

回答

1

請使用.P12文件格式進行推送通知快樂編碼:)

+0

我找到了解決方案,但感謝您的回答:) –