我想安裝一個基本的Xamarin應用程序與Android的推送通知。我跟着演示here,似乎一切順利。應用程序日誌顯示使用下面的代碼接收到註冊令牌:Xamarin谷歌雲消息傳遞不工作
using System;
using Android.App;
using Android.Content;
using Android.Util;
using Android.Gms.Gcm;
using Android.Gms.Gcm.Iid;
namespace HawkProject.Droid {
[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 (this);
var token = instanceID.GetToken (
"1015213323461", 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");
Log.Debug("RegistrationIntentService", e.Message);
return;
}
}
void SendRegistrationToAppServer (string token)
{
// Add custom implementation here as needed.
}
void Subscribe (string token)
{
var pubSub = GcmPubSub.GetInstance(this);
pubSub.Subscribe(token, "/topics/hawkglobal", null);
}
}
}
應用程序日誌返回:
[RegistrationIntentService] Calling InstanceID.GetToken
[RegistrationIntentService] GCM Registration Token: dz8yxsF61tc:APA91bFpHBPsJuGUN2Yh5Hv9CyuAgyIUP4qthhNE79rJUOvLH__ildlVOaP9Q-X1lBpDk69BDRbnzDl4kdEdJBjyudwiVfRZnVR4trQwcM8JZYIEqMNFiXAy61RSjD2MWnC2kmNLj1Ev
我的清單如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.gcs.hawkproject" android:versionCode="1" android:versionName="start">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.gcs.hawkproject.permission.C2D_MESSAGE" />
<permission android:name="com.gcs.hawkproject.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<application android:label="Hawk" android:icon="@drawable/log"></application>
<application android:label="RemoteNotifications" android:icon="@drawable/Icon">
<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.gcs.hawkproject" />
</intent-filter>
</receiver>
<service
android:name="com.gcs.hawkproject.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.gcs.hawkproject.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
</application>
</manifest>
一切似乎進展順利,但是當我嘗試發送消息時,我得到以下結果:
POST:
{
"data" : { "message" : "This is a test" },
"to" : "dz8yxsF61tc:APA91bFpHBPsJuGUN2Yh5Hv9CyuAgyIUP4qthhNE79rJUOvLH__ildlVOaP9Q-X1lBpDk69BDRbnzDl4kdEdJBjyudwiVfRZnVR4trQwcM8JZYIEqMNFiXAy61RSjD2MWnC2kmNLj1Ev"
}
響應
{
"multicast_id":5139152955408512939,
"success":0,"failure":1,"canonical_ids":0,
"results": [{"error":"NotRegistered"}]
}
有什麼想法?我對Xamarin和GCM都很陌生,所以我在幾個小時內摸索得失敗。
我有演示中的其他代碼,以及我可以發佈如有必要。
在此先感謝。
遇到相同問題。你解決了嗎? –
你可能想看看這個線程:http://stackoverflow.com/questions/34513614/google-cloud-message-not-registered-failure-and-unsubscribe-best-practices –