2016-05-31 3 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

, 당신은 Application.Context 대신 this 사용합니다. 그것은 동급이어야하지만 시도 할 수 있습니까?

또한 코드에 RegistrationIntentService 의도를 등록하는 protected override void OnCreate 메서드가 표시되지 않습니다. 여기에 붙여 넣지 않았습니까, 아니면 그것을 구현하는 것을 잊었습니까?

HTH

관련 문제