如何在 Push Sharp 库中的 APNS 有效负载中添加类别以进行交互式通知 ios
Posted
技术标签:
【中文标题】如何在 Push Sharp 库中的 APNS 有效负载中添加类别以进行交互式通知 ios【英文标题】:How to add category in APNS payload in Push Sharp library for interacive notification ios 【发布时间】:2016-06-28 07:14:41 【问题描述】:我正在使用PushSharp 库在 ios 中发送推送通知,它工作正常,但现在我想使用交互式通知,需要在 APNS Payload 中添加“类别”。但我找不到任何方法来做到这一点。 请让我知道我该怎么做。
谢谢。
【问题讨论】:
【参考方案1】:从 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"
给我一些反馈代码是否有用。
【讨论】:
感谢您的回复。我们已经在 pushsharp 库中做到了这一点。这个答案应该对其他人有帮助。以上是关于如何在 Push Sharp 库中的 APNS 有效负载中添加类别以进行交互式通知 ios的主要内容,如果未能解决你的问题,请参考以下文章
apns sharp 中的 iPhone 证书错误调用 SSPI 失败
Push sharp call back 成功但没有收到通知?
Send push notification on Apple (APNS) on c#.net