使用月亮apns设置内容

时间:2014-10-20 08:11:55

标签: c# ios apple-push-notifications moonapns

引用此链接: http://www.objc.io/issue-5/multitasking.html我现在可以通过设置content-available=1

在ios上发送静音推送通知

我正在使用c#上的月亮apns来发送推送通知但是我找不到这个属性来发送静默推送(我正在使用开发证书)

下面是我的代码:

  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
}

1 个答案:

答案 0 :(得分: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();
    }
}