public PushNotificationService()
 {
     settings = FileBasedFcmClientSettings.CreateFromFile("pushnotificationpoc-6a4f2", @"E:\New folder\Navvis\codebase\CoreoHome\Push Notification\serviceAccountKey.json");
 }
Ejemplo n.º 2
0
        public async Task <JObject> sendMessage([FromBody] JObject input)
        {
            var id       = input.Value <string>("id");
            var title    = input.Value <string>("title");
            var message  = input.Value <string>("message");
            var time     = input.Value <string>("time");
            var channels = input.Value <JArray>("channels");

            foreach (var item in channels.Children())
            {
                int channelId = item.Value <int>("id");

                _logger.LogError($"channelId:{channelId}");

                string[] strDeviceTokens = item.Value <JArray>("device_token")?.ToObject <string[]>();

                var channel = _dbContext.Channels.Find(channelId);

                if (channel == null)
                {
                    continue;
                }

                _logger.LogError($"channel:{channel}");


                // APP
                if (channel.Type == 3)
                {
                    var subscriberTypes = new int[] { (int)Subscribers.Types.IOS, (int)Subscribers.Types.Android };

                    var subscribers = strDeviceTokens == null?
                                      _dbContext.Subscribers.Where(q => q.DeletedAt == null && q.Status == (byte)Subscribers.Statuses.Enable && subscriberTypes.Contains((int)q.Type) && q.ChannelId == channelId).ToList() :
                                          _dbContext.Subscribers.Where(q => q.DeletedAt == null && q.Status == (byte)Subscribers.Statuses.Enable && subscriberTypes.Contains((int)q.Type) && q.ChannelId == channelId && strDeviceTokens.Contains(q.DeviceToken)).ToList();;

                    _logger.LogError($"subscribers:{subscribers.Count}");

                    if (subscribers.Count == 0)
                    {
                        continue;
                    }

                    _logger.LogError($"{_fir_folder}{Path.DirectorySeparatorChar}{channel.ChannelKey}");

                    //var settings = FileBasedFcmClientSettings.CreateFromFile($"{_fir_folder}{Path.DirectorySeparatorChar}{channel.ChannelKey}");
                    //var settings = new FcmClientSettings("wildmud-push", "{\"type\": \"service_account\",  \"project_id\": \"wildmud-push\",  \"private_key_id\": \"e37f921cee98f70c23060141e72eeedcbe9e1b4b\",  \"private_key\": \"-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEA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A4c+wKBgGhYI9PqV9RK3iraZqARXVOs1t2yEdirFCL8MWqrhn/lvo5bYMmc\nCo3JuPuyDW0XqIeBWazQ8DCtlht4TmkUHU9L5JOWgHJ8ilpZGnx84/hrIWKViUUi\n35M9qNHcH2ZWpbFgdDpK2HW35CcDkKazudH16PH4yLfW3tlgYwIrabebAoGBAJu3\n9PjfXpwAtDwhGyjuyvz/efs0gJlnXrdxkr9wcAyc8sc/ro5t+XplchTrzQF3ZHoB\nA5NDOYUZZCRPGbVatJ/39aP6gABcESMfoD8cR6NbcsfF6cjdQyODW2zVmwRCmZor\n9RMPY8osNlZgXzcNxSQC7Xr9j9g94DGY4Y3eHVNdAoGAVnPc7R1/JSxi0ZgHUGlo\nTPqIvOJaXdHa2t7kxHuZmFk6AYREnQq9tD2VmtvOQHsUAHp4+xFNITv+bfvfBk2f\nuX/8iTnyIXt73qumxh6VEdqroFqhsupirE86fr8eoYyvAxFzsgrk+xo2320dMPCb\nKuyPRjFBhPqOxRhdBxnti+g=\n-----END PRIVATE KEY-----\n\",  \"client_email\": \"[email protected]\",  \"client_id\": \"112247404943112944715\",  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",  \"token_uri\": \"https://oauth2.googleapis.com/token\",  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-6ov47%40wildmud-push.iam.gserviceaccount.com\"}");
                    var fcmKeyObject = JObject.Parse(channel.SecretKey);
                    var projectId    = fcmKeyObject.Value <string>("project_id");
                    var settings     = new FcmClientSettings(projectId, channel.SecretKey);

                    using (var client = new FcmClient(settings))
                    {
                        var notification = new Notification
                        {
                            Title = title,
                            Body  = message
                        };

                        foreach (var subscriber in subscribers)
                        {
                            try
                            {
                                // The Message should be sent to the News Topic:
                                var msg = new FcmMessage()
                                {
                                    ValidateOnly = false,
                                    Message      = new Message
                                    {
                                        //Topic = "news",
                                        Token        = subscriber.DeviceToken,
                                        Notification = notification
                                    }
                                };

                                // Finally send the Message and wait for the Result:
                                CancellationTokenSource cts = new CancellationTokenSource();

                                // Send the Message and wait synchronously:
                                var result = client.SendAsync(msg, cts.Token).GetAwaiter().GetResult();

                                _logger.LogInformation($"Data Message ID = {result.Name}");
                            }
                            catch (Exception ex)
                            {
                                _logger.LogInformation($"Exception: {ex.ToString()}");
                                continue;
                            }
                        }

                        //return new JObject(){
                        //    { "code", 200},
                        //    { "reason", "scuesss" },
                        //    { "data", new JObject(){{ "message_id", result.Name }} }
                        //};
                    }
                }

                // Line
                if (channel.Type == 4)
                {
                    var subscriberTypes = new int[] { (int)Subscribers.Types.Line };

                    var subscribers = strDeviceTokens == null?
                                      _dbContext.Subscribers.Where(q => q.DeletedAt == null && q.Status == (byte)Subscribers.Statuses.Enable && subscriberTypes.Contains((int)q.Type) && q.ChannelId == channelId).ToList() :
                                          _dbContext.Subscribers.Where(q => q.DeletedAt == null && q.Status == (byte)Subscribers.Statuses.Enable && subscriberTypes.Contains((int)q.Type) && q.ChannelId == channelId && strDeviceTokens.Contains(q.DeviceToken)).ToList();;

                    _logger.LogError($"subscribers:{subscribers.Count}");

                    if (subscribers.Count == 0)
                    {
                        continue;
                    }

                    foreach (var subscriber in subscribers)
                    {
                        try
                        {
                            var client = new HttpClient();

                            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.SecretKey}");

                            var data = new
                            {
                                to       = subscriber.DeviceToken,
                                messages = new[] {
                                    new {
                                        type = "text",
                                        text = message,
                                    }
                                }
                            };

                            var json = JsonConvert.SerializeObject(data);

                            var content = new StringContent(json, Encoding.UTF8, "application/json");

                            content.Headers.ContentLength = json.Length;

                            var result = await client.PostAsync("https://api.line.me/v2/bot/message/push", content);

                            _logger.LogError($"result.Content.ToString(): {result}");
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"Exception: {ex.ToString()}");
                            continue;
                        }
                    }
                }
            }

            dynamic output = new JObject();

            output["message_id"] = 3345678;

            dynamic res = new JObject();

            res.code   = 200;
            res.reason = "success";
            res.data   = output;

            return(res);
        }