GCM多次推送相同的消息(PushSharp)

时间:2014-09-17 13:12:57

标签: c# xamarin.android google-cloud-messaging pushsharp

问题:

我正在使用Xamarin开发一个使用PushSharp的Android应用程序。我正在使用GCM向客户端发送消息,以便在应用程序打开时我可以更新某些内容。 GCM似乎多次向同一设备发送相同的消息。


的logcat:

Thread started:  #12
09-17 08:40:34.307 I/PushSharp-GCM(20855): GCM Message Received!
09-17 08:40:34.317 V/GCMBaseIntentService(20855): Releasing Wakelock
09-17 08:40:34.327 V/UpdateSignalReceiver(20855): Message Receieved: *****
Thread finished:  #12
The thread 'Unknown' (0xc) has exited with code 0 (0x0).
09-17 08:40:34.787 V/PushHandlerBroadcastReceiver(20855): OnReceive: com.google.android.c2dm.intent.RECEIVE
09-17 08:40:34.787 V/PushHandlerBroadcastReceiver(20855): GCM IntentService Class: rAMP_TabletV1.x5.GCMIntentService
09-17 08:40:34.787 V/GCMBaseIntentService(20855): Acquiring wakelock
Thread started:  #13
09-17 08:40:34.807 I/PushSharp-GCM(20855): GCM Message Received!
09-17 08:40:34.817 V/GCMBaseIntentService(20855): Releasing Wakelock
09-17 08:40:34.817 V/UpdateSignalReceiver(20855): Message Receieved: *****
Thread finished:  #13
The thread 'Unknown' (0xd) has exited with code 0 (0x0).
09-17 08:40:35.817 V/PushHandlerBroadcastReceiver(20855): OnReceive: com.google.android.c2dm.intent.RECEIVE
09-17 08:40:35.817 V/PushHandlerBroadcastReceiver(20855): GCM IntentService Class: rAMP_TabletV1.x5.GCMIntentService
09-17 08:40:35.817 V/GCMBaseIntentService(20855): Acquiring wakelock
Thread started:  #14
09-17 08:40:35.857 I/PushSharp-GCM(20855): GCM Message Received!
09-17 08:40:35.857 V/GCMBaseIntentService(20855): Releasing Wakelock
09-17 08:40:35.867 V/UpdateSignalReceiver(20855): Message Receieved: *****
Thread finished:  #14
The thread 'Unknown' (0xe) has exited with code 0 (0x0).
09-17 08:40:36.277 V/PushHandlerBroadcastReceiver(20855): OnReceive: com.google.android.c2dm.intent.RECEIVE
09-17 08:40:36.277 V/PushHandlerBroadcastReceiver(20855): GCM IntentService Class: rAMP_TabletV1.x5.GCMIntentService
09-17 08:40:36.277 V/GCMBaseIntentService(20855): Acquiring wakelock
Thread started:  #15
09-17 08:40:36.327 I/PushSharp-GCM(20855): GCM Message Received!
09-17 08:40:36.327 V/GCMBaseIntentService(20855): Releasing Wakelock
09-17 08:40:36.337 V/UpdateSignalReceiver(20855): Message Receieved: *****
Thread finished:  #15
The thread 'Unknown' (0xf) has exited with code 0 (0x0).
09-17 08:40:36.717 V/PushHandlerBroadcastReceiver(20855): OnReceive: com.google.android.c2dm.intent.RECEIVE
09-17 08:40:36.717 V/PushHandlerBroadcastReceiver(20855): GCM IntentService Class: rAMP_TabletV1.x5.GCMIntentService
09-17 08:40:36.717 V/GCMBaseIntentService(20855): Acquiring wakelock
Thread started:  #16
09-17 08:40:36.747 I/PushSharp-GCM(20855): GCM Message Received!
09-17 08:40:36.757 V/GCMBaseIntentService(20855): Releasing Wakelock
09-17 08:40:36.757 V/UpdateSignalReceiver(20855): Message Receieved: *****

发送消息:

每个设备都已注册,注册ID存储在数据库中。我检查版本号和以前的注册ID,以确保没有重复项,并确保我有正确的注册ID。 (我已仔细检查以确保我的数据库中没有重复项)

从WCF服务调用更新,它将消息发送到所有已注册的设备。该方法仅被调用一次,AllRegisteredDevices是发送消息的设备ID的不同列表。

foreach (var deviceId in AllRegisteredDevices)
{
    var webRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
    webRequest.Method = "post";
    webRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
    webRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
    webRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
    var postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
        + value + "&registration_id=" + deviceId + "";
    Byte[] bytes = Encoding.UTF8.GetBytes(postData);
    webRequest.ContentLength = bytes.Length;
    var dataStream = webRequest.GetRequestStream();
    dataStream.Write(bytes, 0, bytes.Length);
    dataStream.Close();
    var webResponse = webRequest.GetResponse();
    dataStream = webResponse.GetResponseStream();
    var streamReader = new StreamReader(dataStream);
    var responseFromServer = streamReader.ReadToEnd();
    streamReader.Close();
    dataStream.Close();
    webResponse.Close();
} // end loop

收到消息:

我有一个自定义广播接收器来处理消息,具体取决于“消息”的值。 PushService将它传递给接收者。

推送服务

protected override void OnMessage(Context context, Intent intent)
{
     Log.Info(PushHandlerBroadcastReceiver.TAG, "GCM Message Received!");

     string message = intent.Extras.GetString("message");
     var theIntent = new Intent(UpdateAction);
     theIntent.PutExtra("message", message);
     SendOrderedBroadcast(theIntent, null);
} // end OnMessage

UpdateSignalReceiver

[BroadcastReceiver]
[IntentFilter(new string[]{PushHandlerService.UpdateAction}, Priority = (int)IntentFilterPriority.HighPriority)]
public class UpdateSignalReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {       
        MyActivity TheActivity = ((MyActivity )context);
        string message = intent.Extras.GetString("message") ?? "";
        Log.Verbose("UpdateSignalReceiver", "Message Receieved: " + message);
        if (message == "foo")
        {
            TheActivity.DoSomething();
        } // end if
        else if (message == "bar")
        {
            TheActivity.SomethingElse();
        } // end else if
        else
        {
            TheActivity.CatchAllMethod();
        } // end else
        InvokeAbortBroadcast();
    } // end on receieve
} // end UpdateSignalReceiver

环境

  • 三星Galaxy Tab 3
  • 仅限出厂安装的应用(没有其他应用使用相同的GCM)

研究:


问题:

当我只发送一次时,为什么我会反复收到相同的消息?

1 个答案:

答案 0 :(得分:0)

我决定采用限制措施。虽然这在实现上并不完美,但总体思路如下:

  • 所有消息都有唯一ID(Guid)
  • 广播接收器有一个静态的Guids列表,其中包含我们收到和处理的最后20个消息ID
  • 只处理列表中未包含id的邮件,因此我们知道这是该设备的新消息。
  • 将列表限制为20个ID,使其不会失控。 (20似乎绰绰有余,因为重复消息通常有3个,有时5个迭代)

[BroadcastReceiver]
[IntentFilter(new string[]{PushHandlerService.UpdateAction}, Priority = (int)IntentFilterPriority.HighPriority)]
public class UpdateSignalReceiver : BroadcastReceiver
{
    private static List<Guid> _Last20MessageIds;

    public override void OnReceive(Context context, Intent intent)
    {       
        Guid MessageId;
        // Pull the MessageId from the intent
        String MessageIdString = intent.Extras.GetString("message_id" ?? Guid.Empty.ToString());
        Guid.TryParse(MessageIdString, out MessageId);


        if (_Last20MessageIds == null)
        {
            _Last20MessageIds = new List<Guid>();
        }

        // Make sure we didn't already receive this Message, then do work
        if (MessageId != null && MessageId != Guid.Empty && ! _Last20MessageIds.Contains(MessageId))
        {
            DoSomeWorkWithIntent(intent);


            // Add the guid to the message id list
            _Last20MessageIds.Insert(0, MessageId);

            // Trim the list to the most recent 20
            _Last20MessageIds= _Last20MessageIds.Take(20).ToList();
        }
        InvokeAbortBroadcast();
    } // end on receive
} // end UpdateSignalReceiver