2016-05-31 11 views
0

xamarinを使用してAndroidでリモート通知を実装しています。私は携帯電話の登録やGCMを使用して、送信者を通じて通知を送信した後、私は携帯電話に通知が届かないのですWalkthrough - Using Remote Notifications in Xamarin.AndroidxamarinでメソッドOnMessageReceivedを呼び出していません

を使用してPOCをしています

コードに間違いがありますか?または通知を追跡して、モバイルに入っていない理由を知ることはできますか?

MyGcmListenerService.cs

[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })] 
public class MyGcmListenerService : GcmListenerService 
{ 
    public override void OnMessageReceived(string from, Bundle data) 
    { 
     var message = data.GetString("message"); 
     Log.Debug("MyGcmListenerService", "From: " + from); 
     Log.Debug("MyGcmListenerService", "Message: " + message); 
     SendNotification(message); 
    } 
.... 

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yourcompany.LeaveApplication" android:installLocation="auto" android:versionCode="1" android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="15" /> 

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.yourcompany.LeaveApplication.permission.C2D_MESSAGE" /> 
    <permission android:name="com.yourcompany.LeaveApplication.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 
    <application android:label="XamarinLeaveApp" > 
     <receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <category android:name="com.yourcompany.LeaveApplication" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

メッセージ送信者のコード

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 => 
       { 
        var response1 = response; 
        // Console.WriteLine(response); 
        //Console.WriteLine("Message sent: check the client device notification tray."); 
       })); 
    } 
} 

RegistrationIntentService.cs RegistrationIntentService.csファイル内

[Service(Exported = false)] 
class RegistrationIntentService : IntentService 
{ 
    static object locker = new object(); 

    public RegistrationIntentService() : base("RegistrationIntentService") { } 

    protected override void OnHandleIntent(Intent intent) 
    { 
     try 
     { 
      Log.Info("RegistrationIntentService", "Calling InstanceID.GetToken"); 
      lock (locker) 
      { 
       var instanceID = InstanceID.GetInstance(Application.Context); 

       var token = instanceID.GetToken(
        "<project number", GoogleCloudMessaging.InstanceIdScope, null); 

       Log.Info("RegistrationIntentService", "GCM Registration Token: " + token); 
       SendRegistrationToAppServer(token); 
       Subscribe(token); 
      } 
     } 
     catch (Exception e) 
     { 
      Log.Debug("RegistrationIntentService", "Failed to get a registration token"); 
      return; 
     } 
    } 

    void SendRegistrationToAppServer(string token) 
    { 
     // Add custom implementation here as needed. 
    } 

    void Subscribe(string token) 
    { 
     var pubSub = GcmPubSub.GetInstance(Application.Context); 
     pubSub.Subscribe(token, "/topics/global", null); 
    } 
} 
+0

あなたの送信コードの回答がコメントアウトされています。成功した送信を示す応答のメッセージIDを取得していますか? –

+0

これはコメントアウトされたConsole.WriteLineです。 BTWの対応は成功しました.. –

答えて

2

は、あなたの代わりにthisApplication.Contextを使用しています。同等であるべきですが、試してみることはできますか?

また、RegistrationIntentServiceインテントを登録するprotected override void OnCreateメソッドがコード内に表示されません。ここに貼り付けなかったのですか、それとも実装するのを忘れましたか?

HTH

関連する問題