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

标签 c# ios objective-c apple-push-notifications pushsharp

我正在使用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/

相关文章:

c# - 使用 CaSTLe Windsor 在服务类的构造函数中使用参数化构造函数初始化类

ios - RingCentral:构建失败任务失败,退出代码为 65:迦太基安装

iphone - 如何以编程方式向下移动整个 UITableView

ios - Open GL ES,多个着色器

objective-c - objc/cocoa 的类变量范围问题?

ios - 试图追踪需要在 iOS 中实现 JSON 字典值的位置

c# - 从 linq select 查询中读取结果

c# - 通过 asp.net 3.5 发送电子邮件

c# - .NET 中如何实现 "Event"对象?

ios - UIView 的 getter 和 setter