Google cloud messaging 向服务器应用发送上游消息

Google cloud messaging 向服务器应用发送上游消息,google-cloud-messaging,jaxl,Google Cloud Messaging,Jaxl,我已经成功地用JAXL将数据从php服务器页面发送到android客户端 我已经仔细阅读了谷歌云信息官方网站的指南。。对于上游,只有以下文件: public void onClick(final View view) { if (view == findViewById(R.id.send)) { new AsyncTask() { @Override protected String doInBackground(Void

我已经成功地用JAXL将数据从php服务器页面发送到android客户端

我已经仔细阅读了谷歌云信息官方网站的指南。。对于上游,只有以下文件:

public void onClick(final View view) {
    if (view == findViewById(R.id.send)) {
        new AsyncTask() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    Bundle data = new Bundle();
                    data.putString("my_message", "Hello World");
                    data.putString("my_action","SAY_HELLO");
                    String id = Integer.toString(msgId.incrementAndGet());
                    gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
                    msg = "Sent message";
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                }
                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {
                mDisplay.append(msg + "\n");
            }
        }.execute(null, null, null);
    } else if (view == findViewById(R.id.clear)) {
        mDisplay.setText("");
    }
}
说:

在应用服务器上接收XMPP消息

When GCM receives an upstream messaging call from a client app, it generates the necessary XMPP stanza for sending the upstream message.
GCM添加“类别”和“发件人”字段,然后发送一个节,如 将以下内容添加到应用程序服务器:


{
“category”:“com.example.yourapp”,//了解哪个应用程序发送了它
“数据”:
{
“你好”:“世界”,
},
“消息id”:“m-123”,
“发件人”:“REGID”
}
但现在我有一些问题,因为上游的文件有限

1-)Android发送JSON数据,带有上游的发送者id。。。但当我注册api时,并没有人问我关于AppServer的问题。发件人Id使用软件包识别我的gmail帐户的应用程序。不是应用服务器。那么gcm在哪里发送来自客户端的数据呢?如何让GCM了解我的应用程序服务器

2-)我的预算有限,我的服务器是共享帐户web服务器。所以我必须使用php。。。但我在文档中读到,“你们的应用服务器应该是持久连接”并没有定期连接和断开连接。。。我可以使用appserver作为php吗?GCM连接Php scrpit,Php scrpit处理数据并响应android客户端

  • 您可能知道,服务器和GCM之间的连接需要发送者id和API密钥。此外,当客户端应用程序想要发送上游消息时,它使用相同的发送者id。因此GCM知道应该向谁发送上游数据

  • 编程语言没有限制,当然可以使用PHP。您只需保持与GCM服务器的持久连接


  • 不要在意某些断开连接的情况,请注意,如果您的服务器关闭,Google将重试发送上游消息,并且没有向GCM发送特定消息的ACK

    以下是我如何管理上游消息

    先得到

    将它放在apache执行目录中

    创建新的php脚本文件

    <?php
    include_once 'jaxl.php';
    
    $client = new JAXL(array(
        'jid' => '/*Write sender ID here*/@gcm.googleapis.com',
        'pass' => 'Write here your GCM apı key',
        'host' => 'gcm-preprod.googleapis.com',
        'port' => 5236,
       'strict' => false,
        'force_tls' => true,
        'log_level' => JAXL_DEBUG,
        'auth_type' => 'PLAIN',
        'protocol' => 'tls',
         'ssl' => TRUE,
        'log_path'=> 'ex.txt'  /*This create text file to comminication between gcm and your server*/
    ));
    
    $client->add_cb('on_message_stanza', function($msg) {
     echo 'now what!!';
     });
    
     $client->add_cb('on_auth_success', function() {
     echo 'it should';
    //Here is for sending downstream msg
      }); 
    
     $client->add_cb('on_error_message',function()
     {
     global $client;
     echo 'error<br/>';
     _info('got on_error_message cb jid'.$client->full_jid->to_string());
     });
    
    $client->start();
    
    ?>
    
    然后,执行上面写的php脚本,然后单击按钮发送上游消息,查看jaxl创建的ex.txt,您将看到应用程序发送的“Hello World”消息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "FCM Token creation logic");
    
        // Get variables reference
        deviceText = (TextView) findViewById(R.id.deviceText);
        editTextEcho = (EditText) findViewById(R.id.editTextEcho);
        buttonUpstreamEcho = (Button) findViewById(R.id.buttonUpstreamEcho);
    
        //Get token from Firebase
        FirebaseMessaging.getInstance().subscribeToTopic("test");
        final String token = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Token: " + token);
        deviceText.setText(token);
    
        //Call the token service to save the token in the database
        tokenService = new TokenService(this, this);
        tokenService.registerTokenInDB(token);
    
        buttonUpstreamEcho.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Echo Upstream message logic");
                String message = editTextEcho.getText().toString();
                Log.d(TAG, "Message: " + message + ", recipient: " + token);
                FirebaseMessaging.getInstance().send(new RemoteMessage.Builder(FCM_PROJECT_SENDER_ID + FCM_SERVER_CONNECTION)
                        .setMessageId(Integer.toString(RANDOM.nextInt()))
                        .addData("message", message)
                        .addData("action", BACKEND_ACTION_ECHO)
                        .build());
                // To send a message to other device through the XMPP Server, you should add the
                // receiverId and change the action name to BACKEND_ACTION_MESSAGE in the data
            }
        });
    
    }
    
    GitHub:

    我正在尝试学习GCM如何识别应用程序服务器。如果php脚本将数据发送到客户端,客户端将另一个数据发送回GCM,GCM知道此脚本发送的数据吗?如果php断开与服务器的连接,会发生什么情况?GCM可以通过url像http协议一样连接到php脚本吗?谁否决了我?谢谢你的回答..但在你回答之前,我成功地在android到php服务器和php服务器到android之间发送了下行和上行消息…现在,我可以开发我的特殊android应用程序,因为解决方案已经解决。我接受你的回答。你能分享你的上行和下行消息代码吗?我正在使用xampp,但无法通过GCM发送上游消息?我使用了和你一样的代码。发送方ID是发送方应用程序的项目号吗?GCM的xmpp将上游消息发送到服务器上的哪个php文件?请告诉我们您是如何处理php文件中的错误的。看我的答案,如何处理我在使用代码时遇到了以下错误。请帮我解决这个问题。谢谢
    无法连接tls://gcm-preprod.googleapis.com:5236 错误号为110,错误str:Connection超时
    您从哪里得到此错误?您的服务器端php脚本文件正在运行吗?您是否在控制台上创建了GCM服务器?他们的网页?似乎您有服务器配置错误我也收到同样的错误无法连接tls://gcm-preprod.googleapis.com:5236 错误号为110,错误str:Connection超时,而此代码段可能会解决此问题,包括提高帖子质量的解释。记住,你是在将来回答读者的问题,而不仅仅是现在提问的人!请在回答中添加解释,并说明适用的限制和假设。
    String msg = "";
                            try {
                                Bundle data = new Bundle();
                                data.putString("my_message", "Hello World");
                                data.putString("my_action", "SAY_HELLO");
                                String id = Integer.toString(incrementAndGet());
                                gcm.send( "/*Write here sender ID*/"+ "@gcm.googleapis.com", id, data);
                                msg = "Sent message";
                            } catch (IOException ex) {
                                msg = "Error :" + ex.getMessage();
                            }
                            Log.d(msg,"-------------");
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "FCM Token creation logic");
    
        // Get variables reference
        deviceText = (TextView) findViewById(R.id.deviceText);
        editTextEcho = (EditText) findViewById(R.id.editTextEcho);
        buttonUpstreamEcho = (Button) findViewById(R.id.buttonUpstreamEcho);
    
        //Get token from Firebase
        FirebaseMessaging.getInstance().subscribeToTopic("test");
        final String token = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Token: " + token);
        deviceText.setText(token);
    
        //Call the token service to save the token in the database
        tokenService = new TokenService(this, this);
        tokenService.registerTokenInDB(token);
    
        buttonUpstreamEcho.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Echo Upstream message logic");
                String message = editTextEcho.getText().toString();
                Log.d(TAG, "Message: " + message + ", recipient: " + token);
                FirebaseMessaging.getInstance().send(new RemoteMessage.Builder(FCM_PROJECT_SENDER_ID + FCM_SERVER_CONNECTION)
                        .setMessageId(Integer.toString(RANDOM.nextInt()))
                        .addData("message", message)
                        .addData("action", BACKEND_ACTION_ECHO)
                        .build());
                // To send a message to other device through the XMPP Server, you should add the
                // receiverId and change the action name to BACKEND_ACTION_MESSAGE in the data
            }
        });
    
    }