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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?