gpt4 book ai didi

c# - 使用 moon apns 设置内容可用

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:40 26 4
gpt4 key购买 nike

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

在 iOS 上发送静默推送通知

我在 C# 上使用 moon 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;
}

知道如何使用 moon apns 发送这个:

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

最佳答案

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

关于c# - 使用 moon apns 设置内容可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26461104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com