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

上游消息 - 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 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测