不在xamarin中调用OnMessageReceived方法

时间:2016-05-31 05:57:02

标签: c# xamarin google-cloud-messaging xamarin.android

我正在使用xamarin在Android上实现远程通知。

我正在使用Walkthrough - Using Remote Notifications in Xamarin.Android

进行POC

在注册移动设备并使用GCM通过发件人发送通知后,我无法在移动设备上收到通知。

代码中有错误吗?或者我可以跟踪我的通知以获取详细信息为什么它不是移动设备?

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

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

1 个答案:

答案 0 :(得分:2)

RegistrationIntentService.cs文件中,您使用Application.Context代替this。它应该是等价的,但你可以试试吗?

另外,我在代码中没有看到注册protected override void OnCreate意图的RegistrationIntentService方法。你没有把它贴在这里或者你忘了实施它吗?

HTH