我正在创建将 GCM 与 Xamarin 一起使用的 android 应用程序。我遵循了 Xamarin 团队的这个指南,然后是谷歌团队的这个指南
在我实现了 Gcm 的 Xamarin 版本之后,我的 BroadcaseReceiver.OnReceive 什么也没收到。这是清单和广播接收器
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto"
package="com.pushtest.droid"
android:versionCode="1"
android:versionName="1">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="com.pushtest.droid.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.pushtest.droid.permission.C2D_MESSAGE" />
<application android:label="push droid"/>
</manifest>
广播接收器:
[BroadcastReceiver(Permission = "com.google.android.c2dm.permission.SEND")]
[IntentFilter(new[] {"com.google.android.c2dm.intent.RECEIVE"}, Categories = new[] {"com.pushtest.droid"})]
[IntentFilter(new[] {"com.google.android.c2dm.intent.REGISTRATION"}, Categories = new[] {"com.pushtest.droid"})]
[IntentFilter(new[] {"com.google.android.gcm.intent.RETRY"}, Categories = new[] {"com.pushtest.droid"})]
public class MyGcmBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
System.Diagnostics.Debug.WriteLine("MyGcmBroadcastReceiver.OnReceive");
if (intent.Action == "com.google.android.c2dm.intent.REGISTRATION")
{
var registrationId = intent.GetStringExtra("registration_id");
var error = intent.GetStringExtra("error");
var unregistered = intent.GetStringExtra("unregistered");
System.Diagnostics.Debug.WriteLine("!!!! Registration status |R: {0}|E: {1}|U: {2}", registrationId, error, unregistered);
}
else if (intent.Action == "com.google.android.c2dm.intent.RECEIVE")
{
System.Diagnostics.Debug.WriteLine("!!!! Push Received: |TBD|");
}
}
}
当我开始意图时,我什么也没得到 - 没有错误,没有调试信息,没有结果:
private void RegisterForGcm(Context context)
{
const string SenderId = "PROJECT_ID_FROM_GOOGLE_CONSOLE";
var appExtra = PendingIntent.GetBroadcast(context, 0, new Intent(), 0);
var intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.PutExtra("app", appExtra);
intent.PutExtra("sender", SenderId);
context.StartService(intent);
}
然后我以为谷歌的指南终于得到了错误=“SERVICE_NOT_AVAILABLE”的注册响应:
没有额外的字符串(没有registraion_id)。调试信息: !!!!注册状态 |R: |E: SERVICE_NOT_AVAILABLE|U:
这是我的清单和广播接收器(意图开始是相同的):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto"
package="com.pushtest.droid"
android:versionCode="1"
android:versionName="1">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="com.pushtest.droid.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.pushtest.droid.permission.C2D_MESSAGE" />
<application android:label="push droid">
<receiver android:name="com.pushtest.droid.MyGcmBroadcastReceiver"
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" />
<action android:name="com.google.android.gcm.intent.RETRY" />
<category android:name="com.pushtest.droid" />
</intent-filter>
</receiver>
</application>
</manifest>
和 BroadcastReceiver (没有任何属性)
public class MyGcmBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
System.Diagnostics.Debug.WriteLine("MyGcmBroadcastReceiver.OnReceive");
if (intent.Action == "com.google.android.c2dm.intent.REGISTRATION")
{
var registrationId = intent.GetStringExtra("registration_id");
var error = intent.GetStringExtra("error");
var unregistered = intent.GetStringExtra("unregistered");
System.Diagnostics.Debug.WriteLine("!!!! Registration status |R: {0}|E: {1}|U: {2}", registrationId, error, unregistered);
}
else if (intent.Action == "com.google.android.c2dm.intent.RECEIVE")
{
System.Diagnostics.Debug.WriteLine("!!!! Push Received: |TBD|");
}
}
}
此处的测试示例: https ://dl.dropboxusercontent.com/u/19503836/pushtest-issue.zip
我在我的 Android 2.3.6 设备上尝试此代码非常感谢任何帮助。
蒂亚!