2

referring to this link : http://www.objc.io/issue-5/multitasking.html i can now send a silent push notification on ios by setting content-available=1

i'm using moon apns on c# to send the push notification but i can not find this property to send a silent push (i'm using a development certificate)

below is my code :

  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;
  }

any idea how can send this using moon apns :

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

1 Answers1

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();
    }
}