2014-10-20 61 views
2

参照此链接:通过设置content-available=1集内容可利用月球的APN

我使用C#月球APNS发送推送通知,但我可以http://www.objc.io/issue-5/multitasking.html我现在可以在iOS上发送一个无声推送通知找不到这个属性来发送一个无声的推(我使用开发证书)

下面

是我的代码:

string p12File = "test.p12"; 
    string p12FilePassword = "1234"; 
    bool sandbox = false; 
    var push = new PushNotification(sandbox, p12File, p12FilePassword); 
    NotificationPayload payload1; 
    payload1 = new NotificationPayload(testDeviceToken, message, 0, "Silence.m4R"); 
    payload1.AddCustom("message", "HIDDEN"); 
    var notificationList = new List<NotificationPayload>() { payload1 }; 
    var rejected = push.SendToApple(notificationList); 
    foreach (var item in rejected) 
    { 
    return false; 
    } 

任何想法如何可以发送该利用月球APNS:

{ 
"aps" : { 
    "content-available" : 1 
}, 
"content-id" : 42 
} 
+0

有你找到了一种方法来发送使用月球APNS – User382 2017-01-16 14:25:26

回答

0
System; 
using System.Collections.Generic; 
using System.Text; 
using Newtonsoft.Json.Linq; 

namespace MoonAPNS 
{ 
public class NotificationPayload 
{ 
public NotificationAlert Alert { get; set; } 

    public string DeviceToken { get; set; } 

    public int? Badge { get; set; } 

    public string Sound { get; set; } 

    internal int PayloadId { get; set; } 
    public int Content_available { get; set; } 
    public Dictionary<string, object[]> CustomItems 
    { 
     get; 
     private set; 
    } 

    public NotificationPayload(string deviceToken) 
    { 
     DeviceToken = deviceToken; 
     Alert = new NotificationAlert(); 
     CustomItems = new Dictionary<string, object[]>(); 
    } 

    public NotificationPayload(string deviceToken, string alert) 
    { 
     DeviceToken = deviceToken; 
     Alert = new NotificationAlert() { Body = alert }; 
     CustomItems = new Dictionary<string, object[]>(); 
    } 

    public NotificationPayload(string deviceToken, string alert, int badge) 
    { 
     DeviceToken = deviceToken; 
     Alert = new NotificationAlert() { Body = alert }; 
     Badge = badge; 
     CustomItems = new Dictionary<string, object[]>(); 
    } 

    public NotificationPayload(string deviceToken, string alert, int badge, string sound) 
    { 
     DeviceToken = deviceToken; 
     Alert = new NotificationAlert() { Body = alert }; 
     Badge = badge; 
     Sound = sound; 

     CustomItems = new Dictionary<string, object[]>(); 

    } 

public NotificationPayload(string deviceToken, string alert, int badge, string sound,int content_available) 
{ 
DeviceToken = deviceToken; 
Alert = new NotificationAlert() { Body = alert }; 
Badge = badge; 
Sound = sound; 
Content_available = content_available; 
CustomItems = new Dictionary(); 

    } 
    public void AddCustom(string key, params object[] values) 
    { 
     if (values != null) 
      this.CustomItems.Add(key, values); 
    } 

    public string ToJson() 
    { 
     JObject json = new JObject(); 

     JObject aps = new JObject(); 

     if (!this.Alert.IsEmpty) 
     { 
      if (!string.IsNullOrEmpty(this.Alert.Body) 
       && string.IsNullOrEmpty(this.Alert.LocalizedKey) 
       && string.IsNullOrEmpty(this.Alert.ActionLocalizedKey) 
       && (this.Alert.LocalizedArgs == null || this.Alert.LocalizedArgs.Count <= 0)) 
      { 
       aps["alert"] = new JValue(this.Alert.Body); 
      } 
      else 
      { 
       JObject jsonAlert = new JObject(); 

       if (!string.IsNullOrEmpty(this.Alert.LocalizedKey)) 
        jsonAlert["loc-key"] = new JValue(this.Alert.LocalizedKey); 

       if (this.Alert.LocalizedArgs != null && this.Alert.LocalizedArgs.Count > 0) 
        jsonAlert["loc-args"] = new JArray(this.Alert.LocalizedArgs.ToArray()); 

       if (!string.IsNullOrEmpty(this.Alert.Body)) 
        jsonAlert["body"] = new JValue(this.Alert.Body); 

       if (!string.IsNullOrEmpty(this.Alert.ActionLocalizedKey)) 
        jsonAlert["action-loc-key"] = new JValue(this.Alert.ActionLocalizedKey); 

       aps["alert"] = jsonAlert; 
      } 
     } 

     if (this.Badge.HasValue) 
      aps["badge"] = new JValue(this.Badge.Value); 

     if (!string.IsNullOrEmpty(this.Sound)) 
      aps["sound"] = new JValue(this.Sound); 


     if (this.Content_available == 1) 
      aps["content-available"] = new JValue(this.Content_available); 

     json["aps"] = aps; 

     foreach (string key in this.CustomItems.Keys) 
     { 
      if (this.CustomItems[key].Length == 1) 
       json[key] = new JValue(this.CustomItems[key][0]); 
      else if (this.CustomItems[key].Length > 1) 
       json[key] = new JArray(this.CustomItems[key]); 
     } 

     string rawString = json.ToString(Newtonsoft.Json.Formatting.None, null); 

     StringBuilder encodedString = new StringBuilder(); 
     foreach (char c in rawString) 
     { 
      if ((int)c < 32 || (int)c > 127) 
       encodedString.Append("\\u" + String.Format("{0:x4}", Convert.ToUInt32(c))); 
      else 
       encodedString.Append(c); 
     } 
     return rawString;// encodedString.ToString(); 
    } 

    public override string ToString() 
    { 
     return ToJson(); 
    } 
} 
+1

尼斯代码转储无声的通知。在将来你这样做时,请添加散文。 – 2015-12-22 13:34:30

+0

请解释为什么这是问题的答案。 – 2015-12-22 14:36:43