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

上游消息 - 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 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);
                    }
                });
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 echarts图表制作
  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?