dongqiancui9194
2015-08-30 22:23 浏览 44
已采纳

上游消息 - iOS GCM

I'm trying to implement Google Messaging in my app and server using Xamarin API. The downstream messages (server to app) is working great, but i can't get the upstream working.

At server side i'm using PHP with XMPP library (JAXL), with that i can auth at GCM server and send messages to devices. I've registered to receive messages with this code:

$XMPPClient->add_cb("on__message", function($stanza){ 

        echo "new message";
        $data = json_decode(html_entity_decode($stanza->childrens[0] -> text), true);
        $messageType = $data['message_type'];
        $messageId = $data['message_id']; //message id which was sent by us
        $gcmKey = $data['from']; //gcm key;
        ...
        });

At client, i'm using the GCM api call SendMessage:

    public class SendClass : ReceiverDelegate
{
    public void SendMessage(string Message)
    {
        InstanceId.SharedInstance.Start(Google.InstanceID.Config.DefaultConfig);

        Service.SharedInstance.SendMessage(new NSDictionary("key", "value"), @"SenderID@gcm.googleapis.com", "Message");
    }

    public override void DidSendDataMessage(string messageID)
    {
        base.DidSendDataMessage(messageID);
    }

    public override void WillSendDataMessage(string messageID, NSError error)
    {
        base.WillSendDataMessage(messageID, error);
    }
}

The GCM API have two methods that should be called when the message are in the process of sending to server, DidSendDataMessage and WillSendDataMessage, but those methods are not called.

Can someone give me some tips here ?

Thanks !

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • 已采纳
    dongtaochan0777 dongtaochan0777 2015-09-01 15:43

    I've found the problems. First you need to set de delegate of GoogleMessanging.Config to your class (that inherits from ReceiverDelegate), attention not from GoogleInstanceID.Config.

    After that, you need to call the method Start() from the two libraries,GoogleMessaging.Service and Google.InstanceID. Then the magic is done:

            private void StartService()
        {
            NSError ConfigError;
            Google.Core.Context.SharedInstance.Configure(out ConfigError);
            GCMSenderID = Google.Core.Context.SharedInstance.Configuration.GcmSenderID;
    
            SendC = new SendClass();
            Google.GoogleCloudMessaging.Config Conf = Google.GoogleCloudMessaging.Config.DefaultConfig;
            Conf.ReceiverDelegate = SendC;
    
            Service.SharedInstance.Start(Conf);
            Service.SharedInstance.Connect(delegate (NSError error)
            {
                if (error == null)
                {
                    GetToken();
                }
            });
        }
    
        private void GetToken()
        {
            InstanceId.SharedInstance.Start(Google.InstanceID.Config.DefaultConfig);
            InstanceId.SharedInstance.Token(GCMSenderID, Constants.ScopeGCM, new NSDictionary(Constants.RegisterAPNSOption, DevToken,
                Constants.APNSServerTypeSandboxOption, 1), delegate (string Token, NSError error)
                {
                    if (Token != null)
                    {
                        OnTokenReceived(Token);
                    }
                });
        }
    
    点赞 评论 复制链接分享

相关推荐