1

我正在创建将 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 设备上尝试此代码非常感谢任何帮助。

蒂亚!

4

2 回答 2

1

您尚未创建 Service [IntentService ] 类。正在启动服务但尚未创建该服务。BroadCastReceiver 将如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace hellomultiscreen
{

    public class MyGCMBroadcastReceiver : BroadcastReceiver
    {

        const string TAG = "PushHandlerBroadcastReceiver";
        public override void OnReceive(Context context, Intent intent)
        {

            MyIntentService.RunIntentInService(context, intent);
            SetResult(Result.Ok, null, null);
            Toast.MakeText(context, "Received Message!", ToastLength.Short).Show();
        }
    }
}

意图服务如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using Android.Preferences;
using String = System.String;
namespace hellomultiscreen
{

    [Service]
    public class MyIntentService : IntentService
    {
        static PowerManager.WakeLock sWakeLock;
        static object LOCK = new object();

       public static void RunIntentInService(Context context, Intent intent)
        {
            lock (LOCK)
            {
                if (sWakeLock == null)
                {
                    // This is called from BroadcastReceiver, there is no init.
                    var pm = PowerManager.FromContext(context);
                    sWakeLock = pm.NewWakeLock(
                    WakeLockFlags.Partial, "My WakeLock Tag");
                }
            }

            sWakeLock.Acquire();
            intent.SetClass(context, typeof(MyIntentService));
            context.StartService(intent);
        }

        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                Context context = this.ApplicationContext;
                string action = intent.Action;

                if (action.Equals("com.google.android.c2dm.intent.REGISTRATION"))
                {
                   HandleRegistration( context,intent);
                }
                else if (action.Equals("com.google.android.c2dm.intent.RECEIVE"))
                {
                   HandleMessage(intent);
                }
            }
            finally
            {
                lock (LOCK)
                {
                    //Sanity check for null as this is a public method
                    if (sWakeLock != null)
                        sWakeLock.Release();
                }
            }
        }
        private void HandleRegistration(Context context, Intent intent)
        {
            String registration = intent.GetStringExtra("registration_id");
            if (intent.GetStringExtra("error") != null)
            {
                // Registration failed, should try again later.
            }
            else if (intent.GetStringExtra("unregistered") != null)
            {
                // unregistration done, new messages from the authorized sender will be rejected
            }
            else if (registration != null)
            {

                string score = "Successful Login To Bullseye Account";
                long[] vibraPattern = { 0, 500, 250, 500 };

                Bundle valuesForActivity = new Bundle();
                valuesForActivity.PutString("score", score);

                // Create the PendingIntent with the back stack             
                // When the user clicks the notification, SecondActivity will start up.
                Intent resultIntent = new Intent(this, typeof(NotificationDetails));
                resultIntent.PutExtras(valuesForActivity); // Pass some values to SecondActivity.

                TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
                stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(NotificationDetails)));
                stackBuilder.AddNextIntent(resultIntent);

                PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

                // Build the notification
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .SetAutoCancel(true) // dismiss the notification from the notification area when the user clicks on it
                    .SetContentIntent(resultPendingIntent) // start up this activity when the user clicks the intent.
                    .SetContentTitle(score) // Set the title
                    .SetNumber(1) // Display the count in the Content Info
                    .SetSmallIcon(Resource.Drawable.alert) // This is the icon to display
                    .SetVibrate(vibraPattern)

                    .SetContentText(score); // the message to display.

                // Finally publish the notification
                NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                notificationManager.Notify(1, builder.Build());

            }
        }
        private void HandleMessage(Intent intent)
        {
            try
            {
                string score = intent.GetStringExtra("message");
                long[] vibraPattern = { 0, 500, 250, 500 };

                Bundle valuesForActivity = new Bundle();
                valuesForActivity.PutString("score", score);

                // Create the PendingIntent with the back stack             
                // When the user clicks the notification, SecondActivity will start up.
                Intent resultIntent = new Intent(this, typeof(NotificationDetails));
                resultIntent.PutExtras(valuesForActivity); // Pass some values to SecondActivity.

                TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
                stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(NotificationDetails)));
                stackBuilder.AddNextIntent(resultIntent);

                PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

                // Build the notification
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .SetAutoCancel(true) // dismiss the notification from the notification area when the user clicks on it
                    .SetContentIntent(resultPendingIntent) // start up this activity when the user clicks the intent.
                    .SetContentTitle(score) // Set the title
                    .SetNumber(1) // Display the count in the Content Info
                    .SetSmallIcon(Resource.Drawable.alert) // This is the icon to display
                    .SetContentText(score); // the message to display.

                // Finally publish the notification
                NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                notificationManager.Notify(1, builder.Build());

                //
            }
            catch 
            {

            }
        }
    }
}
于 2013-12-09T13:12:55.560 回答
0

我从不费心尝试构建我们自己的推送技术,因为PushSharp运行良好。他有带有工作代码的 monodroid 和 monotouch 示例。我们一直在工作中使用它,除了一些已修复的旧错误之外,该库一直很可靠。

我查看了我的应用程序生成的清单,其中有一些你没有的东西。如果你坚持建立自己的服务,至少看看他的代码,看看他是怎么做的。

链接到 GitHub 项目

于 2013-10-16T18:08:59.043 回答