/// <summary>
            /// Copies this APNs payload, and validates the content of it to ensure that it can be
            /// serialized into the JSON format expected by the FCM service.
            /// </summary>
            internal ApnsPayload CopyAndValidate()
            {
                var copy = new ApnsPayload()
                {
                    CustomData = this.CustomData?.ToDictionary(e => e.Key, e => e.Value),
                };
                var aps = this.Aps;

                if (aps == null && copy.CustomData?.ContainsKey("aps") == false)
                {
                    throw new ArgumentException("Aps dictionary is required in ApnsConfig");
                }

                copy.Aps = aps?.CopyAndValidate();
                return(copy);
            }
示例#2
0
        public async Task <NotificationOutcomeState> SendAppleNotification(string title, string subTitle, string userId, string eventCode, string type, bool enableCustomSounds, int count, string color, NotificationHubClient hubClient = null)
        {
            try
            {
                if (hubClient == null)
                {
                    hubClient = NotificationHubClient.CreateClientFromConnectionString(Config.ServiceBusConfig.AzureNotificationHub_FullConnectionString, Config.ServiceBusConfig.AzureNotificationHub_PushUrl);
                }

                string appleNotification = null;
                string category          = null;

                //if (type == ((int)PushSoundTypes.CallEmergency).ToString() ||
                //	type == ((int)PushSoundTypes.CallHigh).ToString() ||
                //	type == ((int)PushSoundTypes.CallMedium).ToString() ||
                //	type == ((int)PushSoundTypes.CallLow).ToString())
                //{
                //	appleNotification = "{\"aps\" : { \"alert\" : \"" + subTitle + "\", \"content-available\": 1, \"category\" : \"CALLS\", \"badge\" : " + count + ", \"sound\" : \"" + GetSoundFileNameFromType(Platforms.iPhone, type, enableCustomSounds) + "\" }, \"eventCode\": \"" + eventCode + "\" }";
                //}
                //else
                //{
                //	appleNotification = "{\"aps\" : { \"alert\" : \"" + subTitle + "\", \"badge\" : " + count + ", \"sound\" : \"" + GetSoundFileNameFromType(Platforms.iPhone, type, enableCustomSounds) + "\" }, \"eventCode\": \"" + eventCode + "\" }";
                //}


                if (type == ((int)PushSoundTypes.CallEmergency).ToString() ||
                    type == ((int)PushSoundTypes.CallHigh).ToString() ||
                    type == ((int)PushSoundTypes.CallMedium).ToString() ||
                    type == ((int)PushSoundTypes.CallLow).ToString())
                {
                    category = "calls";
                }
                else if (type == ((int)PushSoundTypes.Message).ToString())
                {
                    category = "message";
                }
                else if (type == ((int)PushSoundTypes.Notifiation).ToString())
                {
                    category = "notif";
                }


                var apnsPayload = new ApnsPayload
                {
                    aps = new ApnsHeader
                    {
                        alert = new ApnsAlert
                        {
                            title = title,
                            body  = subTitle
                        },
                        badge    = count,
                        category = category,
                        sound    = GetSoundFileNameFromType(Platforms.iPhone, type, enableCustomSounds)
                    },
                    eventCode = eventCode
                };

                appleNotification = JsonConvert.SerializeObject(apnsPayload);

                var appleOutcome = await hubClient.SendAppleNativeNotificationAsync(appleNotification, string.Format("userId:{0}", userId));

                return(appleOutcome.State);
            }
            catch (Exception ex)
            {
                string exception = ex.ToString();
            }

            return(NotificationOutcomeState.Unknown);
        }