Xamarin GCM消息传回“;“未注册”;(沙马林)
我以前曾成功地将GCM消息发送到我的Xamarin应用程序,但由于某些原因,它们现在失败了。POST请求返回NotRegistered。我的代码如下: Android清单:Xamarin GCM消息传回“;“未注册”;(沙马林),xamarin,google-cloud-messaging,xamarin.android,Xamarin,Google Cloud Messaging,Xamarin.android,我以前曾成功地将GCM消息发送到我的Xamarin应用程序,但由于某些原因,它们现在失败了。POST请求返回NotRegistered。我的代码如下: Android清单: <application android:label="MyProject"> <receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:
<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>
注册意图服务(在主活动中创建):
GCM侦听器服务:
[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);
}
}
[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);
}
}
答复如下:
{"multicast_id":8322296108700959972,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[{"error":"NotRegistered"}]}
怎么了?我将非常感谢任何帮助。我用一个相当难看的方法解决了这个问题。似乎令牌/实例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的响应未注册时更新实例ID来完善这个解决方案。我用一个相当难看的方法解决了这个问题。似乎令牌/实例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的响应未注册时更新实例ID来完善此解决方案。您的代码中有没有调用GcmClient.Register()的功能?没有,显然应该有?我应该在哪里补充呢?GcmClient不是某种插件吗?我仔细检查了一下,似乎调用Register()是为GCM注册的一种古老的、不推荐使用的方法。我还没有使用这种新方法:在代码中的任何地方都有对GcmClient.Register()的调用吗?我应该在哪里补充呢?GcmClient不是某种插件吗?我仔细检查了一下,似乎调用Register()是为GCM注册的一种古老的、不推荐使用的方法。我还没有采用新的方法:你救了我的工作,你救了我的工作。