2016-06-02 5 views
1

xamarinで作成されたアンドロイドアプリケーションをクリックすると、GCM通知を送信する必要があります。ボタンでリモート通知を送信する方法をクリックしますか? Xamarin.Android C#

私はこのチュートリアルで、私は私の通知を送信するためにMessageSender.exeを使用しますが、私は自分のアプリケーションにそれを作るカント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 i need to send my notification. I am not able to get it. 
} 

に従っています。私はそれを行う必要がありますどのようにxamarin.Android で自分のアプリケーションのボタンのクリックにこれを行うために必要

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 the client device notification tray."); 
         })); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Unable to send GCM message:"); 
      Console.Error.WriteLine(e.StackTrace); 
     } 
    } 
    } 
} 

+0

あなたのAndroidアプリからメッセージを送信しようとしていますか? –

+0

はいプッシュ通知 –

+0

クライアントアプリケーションはグローバルトピックに登録していますか?あなたの質問にそのコードを含めることができますか? –

答えて

0

実際に私は質問で与えられたと同じものを使用しました。

 string API_KEY = "APIKEY"; 
     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 the client device notification tray."); 
         })); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Unable to send GCM message:"); 
      Console.Error.WriteLine(e.StackTrace); 
     } 

ご協力いただきありがとうございます。 Brother @ZverevEugene

0

xamarinのクロスプラットフォーム対応のHttpClient使用法の実装が必要なのですか?

お試しください:Consuming a RESTful Web Serviceトピックは少し誤解を招くかもしれませんが、必要なのはHttpClientコードだけです。この例では

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) 
    { 
     ... 
    } 
} 

、クロスプラットフォームMicrosoft HTTP Client Librariesが使用されます。あなたがそれが好きではない場合は、在庫が入手可能な従来のHttpWebRequestを使用することができます。

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(); 
    } 

同期が必要な場合は、デッドロックに陥らないよう十分注意してください。 ConfigureAwait(false)などを使用することを忘れないでください。

P.S.私はあなたがContinueWithを使っているのを見て、それが危険だと気にしません。ここをクリックしてください:ContinueWith is Dangerous, Tooと、主な記事をお見逃しなく:StartNew is Dangerous

+0

私はそれを作ることができません!私に例を挙げてください!私はあなたに喜ぶでしょう! –

+0

@Bakshi私は例を挙げていませんか?今、何が問題になっていますか?実際には –

+0

です。私はそれを使用する方法を作ることができません。通知はトピック/グローバルIMに送信する必要があります。 –

関連する問題