예제 #1
0
        private Message CreateAppleNotification(string token)
        {
            Notification notification = new Notification();

            notification.Title = NOTIFICATION_TITLE;

            ApnsConfig apnsConfig = new ApnsConfig();

            apnsConfig.FcmOptions = new ApnsFcmOptions();

            Message message = new Message();

            message.Notification = notification;
            message.Apns         = apnsConfig;
            message.Token        = token;

            return(message);
        }
예제 #2
0
        public void ApnsConfigSerializationTest()
        {
            ApnsConfig config = new ApnsConfig()
            {
                Payload = new ApnsConfigPayload()
                {
                    Aps = new Aps()
                    {
                        Badge = "badge",
                        Alert = new ApsAlert()
                        {
                            TitleLocKey  = "title_loc_key",
                            ActionLocKey = "action_loc_key",
                            TitleLocArgs = new[] { "1", "2" },
                            Title        = "Title",
                            Body         = "Body",
                            LaunchImage  = "LaunchImage",
                            LocArgs      = new[] { "3", "4" },
                            LocKey       = "LocKey"
                        },
                        Category   = "category",
                        Sound      = "sound",
                        CustomData = new Dictionary <string, object>()
                        {
                            { "CustomKey1", "CustomValue1" }
                        },
                        ContentAvailable = true,
                        MutableContent   = true,
                        ThreadId         = "1"
                    },
                    CustomData = new Dictionary <string, object>()
                    {
                        { "CustomKey2", "CustomValue2" }
                    }
                }
            };

            string result = serializer.SerializeObject(config);

            Assert.IsNotNull(result);
        }
예제 #3
0
        public static Message ToFirebaseMessage(this BaseUserNotification notification, string token, bool wakeup, bool isConfirmed)
        {
            var message = new Message
            {
                Token = token
            };

            if (wakeup)
            {
                message.Data =
                    new Dictionary <string, string>()
                    .WithNonEmpty("id", notification.Id.ToString());

                return(message);
            }

            var formatting = notification.Formatting;

            message.Data =
                new Dictionary <string, string>()
                .WithNonEmpty("id", notification.Id.ToString())
                .WithNonEmpty("confirmText", formatting.ConfirmText)
                .WithNonEmpty("confirmUrl", notification.ComputeConfirmUrl(Providers.MobilePush, token))
                .WithNonEmpty("isConfirmed", isConfirmed.ToString())
                .WithNonEmpty("imageLarge", formatting.ImageLarge)
                .WithNonEmpty("imageSmall", formatting.ImageSmall)
                .WithNonEmpty("linkText", formatting.LinkText)
                .WithNonEmpty("linkUrl", formatting.LinkUrl)
                .WithNonEmpty("silent", notification.Silent.ToString())
                .WithNonEmpty("trackDeliveredUrl", notification.ComputeTrackDeliveredUrl(Providers.MobilePush, token))
                .WithNonEmpty("trackSeenUrl", notification.ComputeTrackSeenUrl(Providers.MobilePush, token))
                .WithNonEmpty("trackingUrl", notification.ComputeTrackSeenUrl(Providers.MobilePush, token))
                .WithNonEmpty("data", notification.Data);

            var androidData =
                new Dictionary <string, string>()
                .WithNonEmpty("subject", formatting.Subject)
                .WithNonEmpty("body", formatting.Body);

            var androidConfig = new AndroidConfig
            {
                Data     = androidData,
                Priority = Priority.High
            };

            var apsAlert = new ApsAlert
            {
                Title = formatting.Subject
            };

            if (!string.IsNullOrWhiteSpace(formatting.Body))
            {
                apsAlert.Body = formatting.Body;
            }

            var apnsHeaders = new Dictionary <string, string>
            {
                ["apns-collapse-id"] = notification.Id.ToString(),
                ["apns-push-type"]   = "alert"
            };

            if (notification.TimeToLiveInSeconds is int timeToLive)
            {
                androidConfig.TimeToLive = TimeSpan.FromSeconds(timeToLive);

                var unixTimeSeconds = DateTimeOffset.UtcNow.AddSeconds(timeToLive).ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture);

                apnsHeaders["apns-expiration"] = timeToLive == 0 ? "0" : unixTimeSeconds;
            }

            var apnsConfig = new ApnsConfig
            {
                Headers = apnsHeaders,
                Aps     = new Aps
                {
                    Alert          = apsAlert,
                    MutableContent = true
                }
            };

            message.Android = androidConfig;
            message.Apns    = apnsConfig;

            return(message);
        }