用例:Android设备无法直接联系Ruby On Rails服务器(它位于防火墙后面),我无法让Internet服务器直接接收请求。我需要有内部Rails服务器来检索来自Android设备的消息,而不是池化策略,我已经看到GCM + XMPP非常适合。
但是,我找不到任何关于如何在Ruby / Rails中编写这种场景的信息。
我怎么能不仅可以从我的Ruby On Rails服务器发送推送通知,还可以接收Upstream Messages from GCM (Google云消息传递)连接我的Ruby On Rails服务器到谷歌CCS服务器?
感谢任何帮助。
记录(或到目前为止我所做的......)
17/12/15:
试图使用this post as a starting point。我在google console上创建了一个项目,并添加了一个服务器API密钥,同时启用了API Google Cloud Messaging for Android
。我已经测试了with this code(基于this blog post),到目前为止,它执行时没有错误:
require 'stella_gcm_xmpp'
id = '[project_number]@gcm.googleapis.com'
password = [API_KEY]
gcm = StellaGcmXmpp.new(id, password, true, true)
gcm.connect
gcm.callback
上游消息测试
客户端(为了完整起见,问题范围之外):
$ ionic start gcm-test
$ ionic platform add android
$ ionic plugin install cordova-plugin-chrome-apps-gcm
$ ionic run android
open Chrome-Dev-Tools console:
> chrome.gcm.register( ['2195xxxxx718'], function(regId) { console.log('regId:' + regId); } )
> regId:APA91bG_5QIpVrBvuooVp7xO...KiVt3ozcf2HKIkHq_42UAPAU4w
> chrome.gcm.send( {destinationId: '2195xxxxx718@gcm.googleapis.com', messageId: '111', timeToLive: 10, data: {my: 'my message'} }, function(messageId) { console.log(messageId); } )
> 8
服务器端
检查Ruby(irb)控制台上的上游消息的接收:
D, [2015-12-18T10:09:05.664007 #4019] DEBUG -- : RECEIVED:
<message from='devices@gcm.googleapis.com' to='2195xxxxx718@gcm.googleapis.com' type='normal'><gcm xmlns='google:mobile:data'>{"data":{"my":"my message"},"time_to_live":86400,"from":"APA91bG_5QIpVrBvuooVp7xOos_EYzA4XNH0CeGzVudbJXxW4avE4NpZO84Q3mC2I-FKAGMTfFdGumSGmkUYViZVwp5gbbC38NDS4GWyaIsABJfhZd3J5KMJBLKgah6lC4LwkbLHKiVt3ozcf2HKIkHq_42UAPAU4w","message_id":"8","category":"com.ionicframework.gcmtest908063"}</gcm></message>
D, [2015-12-18T10:09:05.665623 #4019] DEBUG -- : PROCESSING:
<message from='devices@gcm.googleapis.com' to='2195xxxxx718@gcm.googleapis.com' type='normal' xmlns='jabber:client'><gcm xmlns='google:mobile:data'>{"data":{"my":"my message"},"time_to_live":86400,"from":"APA91bG_5QIpVrBvuooVp7xOos_EYzA4XNH0CeGzVudbJXxW4avE4NpZO84Q3mC2I-FKAGMTfFdGumSGmkUYViZVwp5gbbC38NDS4GWyaIsABJfhZd3J5KMJBLKgah6lC4LwkbLHKiVt3ozcf2HKIkHq_42UAPAU4w","message_id":"8","category":"com.ionicframework.gcmtest908063"}</gcm></message> (Jabber::Message)
D, [2015-12-18T10:09:05.665760 #4019] DEBUG -- : TRYING stanzacbs...
D, [2015-12-18T10:09:05.665859 #4019] DEBUG -- : TRYING message/iq/presence/cbs...
[2015-12-18 10:09:05] GCM send Failed id: 8 error:
*The 'GCM send Failed id: 8 error:' occurs not because of an error, but because message-type is empty*
下游留言测试(超出问题范围,为了完整起见)
客户端
chrome.gcm.onMessage.addListener(function(msg) { console.log('msg: ' + JSON.stringify(msg)) } )
服务器端
gcm.send 'APA91bG_5QIpVrBvuooVp7x...kHq_42UAPAU4w', '999', { msg: 'teste' }
客户端
msg: {"data":{"msg":"teste"}}
答案 0 :(得分:0)
CCS(GCM的XMPP服务器)将位于您的Android客户端和您的ruby服务器之间。一旦您的ruby服务器可以建立到CCS的XMPP连接,那么从客户端到服务器传递消息就没有什么特别之处,反之亦然。 ruby服务器负责向CCS发送消息和从CCS接收消息。