gpt4 book ai didi

c# - 如何在Push Sharp库的APNS有效负载中添加类别以进行交互式通知iOS

转载 作者:行者123 更新时间:2023-12-01 20:09:14 28 4
gpt4 key购买 nike

我正在使用PushSharp库在ios中发送推送通知,并且运行良好,但是现在我想使用交互式通知,并且需要在APNS Payload中添加“类别”。但是我找不到任何方法来做到这一点。
请让我知道我该怎么做。

谢谢。

最佳答案

从PushSharp版本3开始,您必须自己构建有效负载。
我正在使用以下代码来生成有效负载。它使用Newtonsoft.Json生成包括“类别”在内的JSON有效负载:

//
// @(#) APNsDeviceToken.cs
//
// Project: usis Push Notification Router
// System: Microsoft Visual Studio 2015
// Author: Udo Schäfer

using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;

namespace usis.PushNotification
{
// ----------------------
// APNsNotification class
// ----------------------

/// <summary>
/// Represents a notification that can be send to a device.
/// </summary>

public class APNsNotification
{
#region constants

// ---------------------
// DefaultSound constant
// ---------------------

/// <summary>
/// The name of the default sound file.
/// </summary>

public const string DefaultSound = "sound.caf";

#endregion constants

#region properties

// --------------
// Alert property
// --------------

/// <summary>
/// Gets or sets the alert message to display to the user.
/// </summary>
/// <value>
/// The alert message to display to the user.
/// </value>

[JsonProperty(PropertyName = "alert", NullValueHandling = NullValueHandling.Ignore)]
public string Alert { get; set; }

// --------------
// Badge property
// --------------

/// <summary>
/// Gets or sets the number to badge the app icon with.
/// </summary>
/// <value>
/// The number to badge the app icon with.
/// </value>

[JsonProperty(PropertyName = "badge", NullValueHandling = NullValueHandling.Ignore)]
public int? Badge { get; set; }

// --------------
// Sound property
// --------------

/// <summary>
/// Gets or sets the sound to play.
/// </summary>
/// <value>
/// The sound to play.
/// </value>

[JsonProperty(PropertyName = "sound", NullValueHandling = NullValueHandling.Ignore)]
public string Sound { get; set; }

// -------------------------
// ContentAvailable property
// -------------------------

/// <summary>
/// Gets or sets a value indicating whether new content is available.
/// </summary>
/// <value>
/// <c>true</c> if new content is available; otherwise, <c>false</c>.
/// </value>

[JsonProperty(PropertyName = "content-available", NullValueHandling = NullValueHandling.Ignore)]
public bool ContentAvailable { get; set; }

// -----------------
// Category property
// -----------------

/// <summary>
/// Gets or sets a string value that represents the notification category.
/// </summary>
/// <value>
/// A string value that represents the <c>identifier</c> property of the
/// <c>UIMutableUserNotificationCategory</c> object you created to define custom actions.
/// </value>

[JsonProperty(PropertyName = "category", NullValueHandling = NullValueHandling.Ignore)]
public string Category { get; set; }

#endregion properties

#region JsonWrapper class

// -----------------
// JsonWrapper class
// -----------------

private class JsonWrapper
{
#region construction

// ------------
// construction
// ------------

public JsonWrapper(APNsNotification aps) { Aps = aps; }

#endregion construction

#region properties

// ------------
// Aps property
// ------------

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[JsonProperty(PropertyName = "aps")]
public APNsNotification Aps { get; private set; }

#endregion properties
}

#endregion JsonWrapper class

#region overrides

// ---------------
// ToString method
// ---------------

/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="string" /> that represents this instance.
/// </returns>

public override string ToString()
{
return JsonConvert.SerializeObject(new JsonWrapper(this));
}

#endregion overrides
}
}

// eof "APNsDeviceToken.cs"

给我一些反馈,看看代码是否有用。

关于c# - 如何在Push Sharp库的APNS有效负载中添加类别以进行交互式通知iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069453/

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