我试图实现谷歌消息在我的应用程序和服务器使用Xamarin API。下游消息(服务器到应用程序)工作得很好,但我不能让上游工作。
在服务器端,我使用PHP与XMPP库(JAXL),这样我就可以在GCM服务器上验证并向设备发送消息。我已经注册接收代码:
$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;
...
});
在客户端,我使用GCM api调用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);
}
}
GCM API有两个方法应该在消息发送到服务器的过程中调用,DidSendDataMessage和WillSendDataMessage,但是这些方法没有被调用。
谁能给我一些提示吗?
谢谢!
我发现问题了。首先,您需要设置 googlemessaging的de delegate。配置到你的类(继承自ReceiverDelegate),注意不要从GoogleInstanceID.Config.
之后,您需要从两个库中调用Start()方法,GoogleMessaging。Service and Google.InstanceID。然后魔术就完成了: 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);
}
});
}