public async Task <IActionResult> Index()
        {
            var         httpClient = new HttpClient();
            ApnSettings settings   = new ApnSettings();

            settings.ServerType          = ApnServerType.Production;
            settings.TeamId              = "44KGXK85M8";
            settings.AppBundleIdentifier = "com.ultronnet.seekit24";
            settings.P8PrivateKey        = "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg8dK9Q2N3e4XfYW4e" +
                                           "fn5v5TCnv2MmMSU349XsxVCewJGgCgYIKoZIzj0DAQehRANCAATCm9YLSdOtPS3n" +
                                           "hhtYV2R7rjbUogF3cl4jD8evA3XHqRrH28YrOeLKFEt73ofFbxUKLvTkup0cNtTo" +
                                           "GJALzQMu";
            settings.P8PrivateKeyId = "QS47LJVNG3";

            var apn = new ApnSender(settings, httpClient);

            AppleNotification notification = new AppleNotification();

            notification.Aps          = new AppleNotification.ApsPayload();
            notification.ExperienceId = "@choby/seekit24";
            notification.Aps.Alert    = new AppleNotificationAlert
            {
                Title = "你有一条新的消息",
                Body  = "你屁股着火拉✅"
            };

            string deviceToken = "a0ba4576e8be019e88a3d9b4258eb04ebefdb7ad87f9d1fcc9dd727632760d06";
            var    response    = await apn.SendAsync(notification, deviceToken);


            return(Content($"ios push result:{response.IsSuccess.ToString()}"));
        }
Exemple #2
0
        static async Task Main(string[] args)
        {
            var path = args.Length > 0 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "apple.json");
            var json = JObject.Parse(File.ReadAllText(path));

            var p8privateKey        = json["p8privateKey"].ToString();
            var p8privateKeyId      = json["p8privateKeyId"].ToString();
            var teamId              = json["teamId"].ToString();
            var appBundleIdentifier = json["appBundleIdentifier"].ToString();
            var server              = json["server"].ToString().Equals("Development") ? CorePush.Apple.ApnServerType.Development : CorePush.Apple.ApnServerType.Production;
            var deviceToken         = json["deviceToken"].ToString();

            var settings = new ApnSettings
            {
                AppBundleIdentifier = appBundleIdentifier,
                P8PrivateKey        = p8privateKey,
                P8PrivateKeyId      = p8privateKeyId,
                TeamId     = teamId,
                ServerType = server,
            };

            var notification = json["payload"].ToObject <AppleNotification>();

            var apn      = new ApnSender(settings, http);
            var response = await apn.SendAsync(notification, deviceToken);

            Console.WriteLine($"Response: {JsonConvert.SerializeObject(response)}");
        }
Exemple #3
0
        public override bool SendMessage(SendMessage message)
        {
            if (apnSender == null)
            {
                var keyID             = SendCacheStore.GetSystemValue("barkKeyId");
                var teamID            = SendCacheStore.GetSystemValue("barkTeamId");
                var privateKey        = SendCacheStore.GetSystemValue("barkPrivateKey");
                var privateKeyContent = privateKey.Split('\n')[1];
                var apnSettings       = new ApnSettings()
                {
                    TeamId = teamID,
                    AppBundleIdentifier = "me.fin.bark",
                    P8PrivateKey        = privateKeyContent,
                    ServerType          = ApnServerType.Production,
                    P8PrivateKeyId      = keyID,
                };

                apnSender = new ApnSender(apnSettings, new HttpClient());
            }

            var payload = new AppleNotification(
                Guid.NewGuid(),
                message.Data,
                message.Title)
            {
                IsArchive         = Auth.IsArchive,
                AutoMaticallyCopy = Auth.AutoMaticallyCopy,
            };

            payload.Aps.Sound = Auth.Sound;

            var response = apnSender.Send(payload, Auth.DeviceToken);

            if (response.IsSuccess)
            {
                return(true);
            }
            return(false);
        }
Exemple #4
0
        //[HttpGet]
        //[Route("AppleNotificaion")]
        private async Task ApplePushNotification(string deviceToken, Notification notification)
        {
            try
            {
                HttpClient  httpClient  = new HttpClient();
                ApnSettings apnSettings = new ApnSettings()
                {
                    AppBundleIdentifier = "com.nextleveltraining", P8PrivateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgZ1ugPXE4Hhh3L1embZmjfUdYBij8HbsrolZnzfR49X6gCgYIKoZIzj0DAQehRANCAARbCwj0VnMCOzw/Tyx4GsS4W+QN4LLCe6RRgIR/LZBJQqKi0q4XWg/p4Qa6JQAdKOZziemK4/dJZaqH/EFijM1S", P8PrivateKeyId = "FQ6ZXC7U8L", ServerType = ApnServerType.Production, TeamId = "Y77A2C426U"
                };
                AppleNotification appleNotification = new AppleNotification();
                appleNotification.Aps.AlertBody = notification.Text;
                appleNotification.Notification  = JsonConvert.SerializeObject(notification);
                var apn    = new ApnSender(apnSettings, httpClient);
                var result = await apn.SendAsync(appleNotification, deviceToken.Trim());

                if (!result.IsSuccess)
                {
                    ApnSettings devApnSettings = new ApnSettings()
                    {
                        AppBundleIdentifier = "com.nextleveltraining", P8PrivateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgZ1ugPXE4Hhh3L1embZmjfUdYBij8HbsrolZnzfR49X6gCgYIKoZIzj0DAQehRANCAARbCwj0VnMCOzw/Tyx4GsS4W+QN4LLCe6RRgIR/LZBJQqKi0q4XWg/p4Qa6JQAdKOZziemK4/dJZaqH/EFijM1S", P8PrivateKeyId = "FQ6ZXC7U8L", ServerType = ApnServerType.Development, TeamId = "Y77A2C426U"
                    };
                    AppleNotification appleNotificationDev = new AppleNotification();
                    appleNotificationDev.Aps.AlertBody = notification.Text;
                    appleNotificationDev.Notification  = JsonConvert.SerializeObject(notification);
                    var apnDev    = new ApnSender(devApnSettings, httpClient);
                    var resultDev = await apnDev.SendAsync(appleNotificationDev, deviceToken.Trim());

                    if (!resultDev.IsSuccess)
                    {
                        var    notificationError = result.Error.Reason;
                        var    device_Token      = deviceToken;
                        var    user    = _unitOfWork.UserRepository.FindOne(x => x.DeviceToken.ToLower() == device_Token.ToLower());
                        string path    = Directory.GetCurrentDirectory();
                        var    num     = string.IsNullOrEmpty(user.MobileNo) ? "" : user.MobileNo.ToString();
                        var    email   = string.IsNullOrEmpty(user.EmailID) ? "" : user.EmailID.ToString();
                        string newPath = path + "\\wwwroot\\ErrorLogFile\\AppleErrorDevices.txt";
                        using (StreamWriter writer = new StreamWriter(newPath, true))
                        {
                            writer.WriteLine("-----------------------------------------------------------------------------");
                            writer.WriteLine("Email : " + email.ToString());
                            writer.WriteLine("Mobile_No : " + num);
                            writer.WriteLine("Device_Token : " + device_Token.ToString());
                            writer.WriteLine("Error : " + notificationError.ToString());
                            writer.WriteLine("Mode : Development");
                            writer.WriteLine("-----------------------------------------------------------------------------");
                            writer.WriteLine();
                        }
                    }
                    else
                    {
                        var    notificationError = result.Error.Reason;
                        var    device_Token      = deviceToken;
                        var    user    = _unitOfWork.UserRepository.FindOne(x => x.DeviceToken.ToLower() == device_Token.ToLower());
                        string path    = Directory.GetCurrentDirectory();
                        var    num     = string.IsNullOrEmpty(user.MobileNo) ? "" : user.MobileNo.ToString();
                        var    email   = string.IsNullOrEmpty(user.EmailID) ? "" : user.EmailID.ToString();
                        string newPath = path + "\\wwwroot\\ErrorLogFile\\AppleWorkingDevices.txt";
                        using (StreamWriter writer = new StreamWriter(newPath, true))
                        {
                            writer.WriteLine("-----------------------------------------------------------------------------");
                            writer.WriteLine("Email : " + email.ToString());
                            writer.WriteLine("Mobile_No : " + num);
                            writer.WriteLine("Device_Token : " + device_Token.ToString());
                            writer.WriteLine("Mode : Development");
                            writer.WriteLine("-----------------------------------------------------------------------------");
                            writer.WriteLine();
                        }
                    }
                }
                else
                {
                    var    device_Token = deviceToken;
                    var    user         = _unitOfWork.UserRepository.FindOne(x => x.DeviceToken.ToLower() == device_Token.ToLower());
                    string path         = Directory.GetCurrentDirectory();
                    var    num          = string.IsNullOrEmpty(user.MobileNo) ? "" : user.MobileNo.ToString();
                    var    email        = string.IsNullOrEmpty(user.EmailID) ? "" : user.EmailID.ToString();
                    string newPath      = path + "\\wwwroot\\ErrorLogFile\\AppleWorkingDevices.txt";
                    using (StreamWriter writer = new StreamWriter(newPath, true))
                    {
                        writer.WriteLine("-----------------------------------------------------------------------------");
                        writer.WriteLine("Email : " + email);
                        writer.WriteLine("Mobile_No : " + num);
                        writer.WriteLine("Device_Token : " + device_Token.ToString());
                        writer.WriteLine("Mode : Production");
                        writer.WriteLine("-----------------------------------------------------------------------------");
                        writer.WriteLine();
                    }
                }
            }
            catch (Exception ex)
            {
                string path    = Directory.GetCurrentDirectory();
                string newPath = path + "\\wwwroot\\ErrorLogFile\\ErrorLogs.txt";
                using (StreamWriter writer = new StreamWriter(newPath, true))
                {
                    writer.WriteLine("-----------------------------------------------------------------------------");
                    writer.WriteLine("Message : " + ex.Message.ToString());
                    writer.WriteLine("Eception : " + ex.ToString());
                    writer.WriteLine("-----------------------------------------------------------------------------");
                    writer.WriteLine();
                }
            }
        }
Exemple #5
0
 public async Task PostNotification(string apns, Notification notification)
 {
     ApnSettings apnSettings = _apiSettings.APNSSettings.ConvertToAPNSettings();
     var         apn         = new ApnSender(apnSettings, new System.Net.Http.HttpClient());
     await apn.SendAsync(notification, apns);
 }