azure - How to Send a Remote Notification on a Button Click? Xamarin.Android C# -


i need send gcm notification on button click in android application made through xamarin.

i have followed tutorial https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/

button btnclick = findviewbyid<button>(resource.id.btnclikc); btnclick.click += btnclick_click; void btnclick_click (object sender, system.eventargs e) { // here need send notification. not able it. } 

i use messagesender.exe send notification cant make application.

using system; using system.net.http; using system.net.http.headers; using system.text; using system.threading.tasks; using newtonsoft.json.linq;  namespace messagesender  { class program {     public const string api_key =    "api_key";     public const string message = "message";      static void main(string[] args)     {         var jgcmdata = new jobject();         var jdata = new jobject();          jdata.add("message", message);         jgcmdata.add("to", "/topics/global");         jgcmdata.add("data", jdata);          var url = new uri("https://gcm-http.googleapis.com/gcm/send");         try         {             using (var client = new httpclient())             {                 client.defaultrequestheaders.accept.add(                     new mediatypewithqualityheadervalue("application/json"));                  client.defaultrequestheaders.tryaddwithoutvalidation(                     "authorization", "key=" + api_key);                  task.waitall(client.postasync(url,                     new stringcontent(jgcmdata.tostring(), encoding.default, "application/json"))                         .continuewith(response =>                         {                             console.writeline(response);                             console.writeline("message sent: check client device notification tray.");                         }));             }         }         catch (exception e)         {             console.writeline("unable send gcm message:");             console.error.writeline(e.stacktrace);         }     }   } } 

i need make application button click in xamarin.android how should that??

if question right need cross-platform httpclient usage implementation xamarin, right?

try this: consuming restful web service. topic might bit misleading should httpclient code need.

async void mynotificationpost(uri uri, string json) {     httpclient client = new httpclient();     var content = new stringcontent (json, encoding.utf8, "application/json");     httpresponsemessage response = await client.postasync(uri, content);      ...      if (response.issuccessstatuscode)      {         ...     } } 

in example, crossplatform microsoft http client libraries used. if not can use conventional httpwebrequest available in stock.

    task<webresponse> httprequestsend(         uri uri,          byte[] bodydata,         cancellationtoken cancellationtoken)     {         httpwebrequest request = webrequest.createhttp(uri);         request.method = "post";         request.headers["accept"] = "application/json";          return task.factory.fromasync<stream>(             request.begingetrequeststream,              request.endgetrequeststream,              null).continuewith(                 reqstreamtask =>                  {                     using (reqstreamtask.result)                      {                         reqstreamtask.result.write(bodydata, 0, bodydata.length);                     }                      return task.factory.fromasync<webresponse>(                         request.begingetresponse,                          request.endgetresponse,                          null).continuewith(                             restask =>                              {                                 return restask.result;                             },                              cancellationtoken);                 },                  cancellationtoken).unwrap();     } 

if need synchronous, careful not stuck in deadlock. not forget use configureawait(false) or like.

p.s. see using continuewith , not care it's danger. have here: continuewith dangerous, too , not miss main article: startnew dangerous


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo