Beispiel #1
0
        async static Task DoSomething()
        {
            var notification = GetTestPayload();


            var jsonObj = JsonConvert.SerializeObject(notification);

            Debug.WriteLine("JSON Dictionary: " + jsonObj);

            List <string> deviceTokens = new List <string>();

            deviceTokens.Add(devicetoken1);


            //---回覆的資料---
            foreach (var devicetoken in deviceTokens)
            {
                APNsResponse aPNsResponse = await iOSPushService.PushAsync(notification, devicetoken);

                // Debug.WriteLine("yoyoyoyoy");
                Debug.WriteLine("APNsResponse: " + aPNsResponse);
            }
        }
        public async Task <APNsResponse> SendAsync(object notification,
                                                   string deviceToken,
                                                   APNsServer envType,
                                                   string apnsId      = null,
                                                   int apnsExpiration = 0,
                                                   int apnsPriority   = 10, //apple官方建議
                                                   bool isBackground  = false)
        {
            var path = $"/3/device/{deviceToken}";
            var json = JsonConvert.SerializeObject(notification);

            //@@test
            //Uri to request
            // var uri = new Uri(string.Format("https://{0}:{1}/3/device/{2}", "api.development.push.apple.com", "443", deviceToken));
            //@@---


            var request = new HttpRequestMessage(HttpMethod.Post, new Uri(servers[envType] + path))
            {
                Version = new Version(2, 0),
                Content = new StringContent(json),
                Method  = HttpMethod.Post,
                //    RequestUri = uri
            };

            var testOneHourToken = "eyJhbGciOiJFUzI1NiIsImtpZCI6IlJSWTg1NFU0TjQifQ==.eyJpc3MiOiJCSzc2Rjk4RFk3IiwiaWF0IjoxNTgwMDE5NjQ1fQ==.EK2ju4LCWjpk8CmQDBMCRWWt77kaefqx5BVAB9zHgheDfbCjySAd7+5P/EE6MZD5LjstRrbg7AkbhQaYGknBBQ==";

            request.Headers.Authorization = new AuthenticationHeaderValue("bearer", _jwt); //jwtToken.Value
            request.Headers.TryAddWithoutValidation(":path", path);
            request.Headers.Add(APN_ID_TOPIC, appBundleId);
            request.Headers.Add(APN_EXPIRATION, apnsExpiration.ToString());
            request.Headers.Add(APN_PRIORITY, apnsPriority.ToString());
            request.Headers.Add(APN_PUSHTYPE, isBackground ? "background" : "alert");  // for iOS 13 required

            if (!string.IsNullOrWhiteSpace(apnsId))
            {
                request.Headers.Add(APN_ID, apnsId);
            }

            var response = await http.Value.SendAsync(request);


            if (!response.IsSuccessStatusCode)
            {
                var code = response.StatusCode;
                var body = await response.Content.ReadAsStringAsync();

                var error = JsonConvert.DeserializeObject <APNsError>(body);

                var result = new APNsResponse
                {
                    statusCode = code,
                    IsSuccess  = false,
                    Error      = error
                };
                Debug.WriteLine("response失敗回應: ");
                Debug.WriteLine("  statusCode:  " + result.statusCode);
                Debug.WriteLine("  IsSuccess:  " + result.IsSuccess);
                Debug.WriteLine("  Error:  " + result.Error.Reason);
                return(result);
            }
            else
            {
                var code    = response.StatusCode;
                var succeed = response.IsSuccessStatusCode;
                var content = await response.Content.ReadAsStringAsync();

                var error = JsonConvert.DeserializeObject <APNsError>(content);

                var result = new APNsResponse
                {
                    statusCode = code,
                    IsSuccess  = succeed,
                    Error      = error
                };
                Debug.WriteLine("response成功回應: ");
                Debug.WriteLine("  statusCode:  " + result.statusCode);
                Debug.WriteLine("  IsSuccess:  " + result.IsSuccess);
                Debug.WriteLine("  Error:  " + result.Error);
                return(result);
            }
        }