@@ -28,127 +28,5 @@ public IActionResult Get()
2828 {
2929 return Ok ( ) ;
3030 }
31-
32- [ HttpPost ( "push2" ) ]
33- public async Task < IActionResult > Push2 ( )
34- {
35- var request = WebRequest . Create ( "https://onesignal.com/api/v1/notifications" ) as HttpWebRequest ;
36-
37- request . Method = "POST" ;
38- request . ContentType = "application/json; charset=utf-8" ;
39- request . Headers [ "authorization" ] = "Basic NTNmZWI0MjktOWI2My00M2FiLTk1ZDQtMDljYjBhMjhhM2Vh" ;
40-
41- var sendJson = JObject . FromObject ( new {
42- app_id = "ffe9789a-e9bc-4789-9cbb-4552664ba3fe" ,
43- contents = new { en = "Test message" } ,
44- filters = new [ ] { new { field = "tag" , key = "custom" , relation = "=" , value = "tag" } } ,
45- send_after = "2018-04-23 17:58:00 GMT+0300"
46- } ) ;
47-
48- var byteArray = Encoding . UTF8 . GetBytes ( sendJson . ToString ( ) ) ;
49-
50- try
51- {
52- using ( var writer = await request . GetRequestStreamAsync ( ) )
53- {
54- writer . Write ( byteArray , 0 , byteArray . Length ) ;
55- }
56-
57- using ( var response = await request . GetResponseAsync ( ) )
58- using ( var reader = new StreamReader ( response . GetResponseStream ( ) ) )
59- {
60- var json = JObject . Parse ( reader . ReadToEnd ( ) ) ;
61- System . Diagnostics . Debug . WriteLine ( String . Format ( "id = {0}" , json [ "id" ] ) ) ;
62- }
63- }
64- catch ( WebException ex )
65- {
66- System . Diagnostics . Debug . WriteLine ( ex . Message ) ;
67- System . Diagnostics . Debug . WriteLine ( new StreamReader ( ex . Response . GetResponseStream ( ) ) . ReadToEnd ( ) ) ;
68- }
69-
70- return Ok ( ) ;
71- }
72-
73- [ HttpGet ( "push" ) ]
74- public async Task < IActionResult > Push ( )
75- {
76- var deviceId = "LySqbodo7WMqWO24vEULM1x4na/sm43fYuNs9/eZHCs=" ;
77- int port = 2195 ;
78- string hostname = "gateway.sandbox.push.apple.com" ;
79- //var hostname = "gateway.push.apple.com";
80-
81- //I have removed certificate. Keep your certificate in wwwroot/certificate location. This location is not mandatory
82- string certificatePath = "/Users/AntonEfimenko/Documents/Programming/todo-cert/cert-prod.pfx" ;
83- //var clientCertificate = new X509Certificate2(certificatePath, "11", X509KeyStorageFlags.PersistKeySet);
84- X509Certificate2 clientCertificate = new X509Certificate2 ( System . IO . File . ReadAllBytes ( certificatePath ) , "" ) ;
85- X509Certificate2Collection certificatesCollection = new X509Certificate2Collection ( clientCertificate ) ;
86-
87- TcpClient client = new TcpClient ( AddressFamily . InterNetwork ) ;
88- await client . ConnectAsync ( hostname , port ) ;
89-
90-
91- //SslStream sslStream = new SslStream(client.GetStream(), false);
92- SslStream sslStream = new SslStream (
93- client . GetStream ( ) , false ,
94- new RemoteCertificateValidationCallback ( ValidateServerCertificate ) ,
95- null ) ;
96-
97- try
98- {
99- await sslStream . AuthenticateAsClientAsync ( hostname , certificatesCollection , SslProtocols . Tls , false ) ;
100- MemoryStream memoryStream = new MemoryStream ( ) ;
101- BinaryWriter writer = new BinaryWriter ( memoryStream ) ;
102- writer . Write ( ( byte ) 0 ) ;
103- writer . Write ( ( byte ) 0 ) ;
104- writer . Write ( ( byte ) 32 ) ;
105-
106- writer . Write ( Convert . FromBase64String ( deviceId ) ) ;
107- string payload = "{\" aps\" :{\" alert\" :\" " + "Test message" + "\" ,\" badge\" :0,\" sound\" :\" default\" }}" ;
108- writer . Write ( ( byte ) 0 ) ;
109- writer . Write ( ( byte ) payload . Length ) ;
110- byte [ ] b1 = System . Text . Encoding . UTF8 . GetBytes ( payload ) ;
111- writer . Write ( b1 ) ;
112- writer . Flush ( ) ;
113- byte [ ] array = memoryStream . ToArray ( ) ;
114- sslStream . Write ( array ) ;
115- sslStream . Flush ( ) ;
116- client . Dispose ( ) ;
117- }
118- catch ( Exception ex )
119- {
120- var m = ex . Message ;
121- throw ex ;
122- }
123- finally
124- {
125- client . Dispose ( ) ;
126- }
127- //catch// (AuthenticationException ex)
128- //{
129- // client.Dispose();
130- //}
131- //catch// (Exception e)
132- //{
133- // client.Dispose();
134- //}
135-
136- return Content ( "Notification sent. Check your device." ) ;
137- }
138-
139- private static bool ValidateServerCertificate (
140- object sender ,
141- X509Certificate certificate ,
142- X509Chain chain ,
143- SslPolicyErrors sslPolicyErrors )
144- {
145- if ( sslPolicyErrors == SslPolicyErrors . None )
146- return true ;
147-
148- Console . WriteLine ( "Certificate error: {0}" , sslPolicyErrors ) ;
149-
150- // Do not allow this client to communicate with unauthenticated servers.
151- return false ;
152- }
15331 }
15432}
0 commit comments