GCM消息传递返回" NotRegistered" (Xamarin)

时间:2016-05-30 12:34:40

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

之前我已成功将GCM消息发送到我的Xamarin应用程序,但由于某种原因,他们现在失败了。 POST请求返回NotRegistered。我的代码如下:

Android Manifest:

<application android:label="MyProject">
  <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.example.MyProject" />
    </intent-filter>
  </receiver>
</application>

实例ID侦听器服务:

[Service(Exported = false), IntentFilter(new[] { "com.google.android.gms.iid.InstanceID" })]
class InstanceIdListenerService : InstanceIDListenerService
{
    public override void OnTokenRefresh()
    {
        var intent = new Intent(this, typeof(RegistrationIntentService));
        StartService(intent);
    }
}

注册意向服务(这是在主要活动中创建的):

static object locker = new object();

protected override void OnHandleIntent(Intent intent)
{
    lock (locker)
    {
        var instanceID = InstanceID.GetInstance(this);
        var token = instanceID.GetToken(
            "123456789123",
            GoogleCloudMessaging.InstanceIdScope, 
            null);
        SendRegistrationToAppServer(token); // just a HTTP request
    }
}

GCM侦听器服务:

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

POST回复:

{"multicast_id":8322296108700959972,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[{"error":"NotRegistered"}]}

出了什么问题?我非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

我用一个相当丑陋的黑客解决了这个问题。似乎令牌/实例ID已过期,因此我添加了:

var instanceID = InstanceID.GetInstance(this);

// update the instance ID
instanceID.DeleteInstanceID();
instanceID = InstanceID.GetInstance(this);

var token = instanceID.GetToken(
    "123456789123", 
    GoogleCloudMessaging.InstanceIdScope, 
    null);

我可以通过仅在GCM的响应为NotRegistered时更新实例ID来优化此解决方案。