在 Titanium 中接收 GCM 主题推送消息

Receive GCM Topic Push message in Titanium

我没有头发可以撕了,需要帮助..

我想从服务器(通过 GCM)向主题(或频道)发送消息,以便它到达订阅该主题的所有设备。

向各个设备发送推送消息正常,发送到主题的格式相同,除了指定一个deviceToken,在/topics/mytopic

中指定主题

此处描述:

https://developers.google.com/cloud-messaging/downstream https://developers.google.com/cloud-messaging/topic-messaging

在 Titanium 方面(Android 目前但计划是 IOS 太晚了)我将设备注册到频道 "myChannel"。我可以通过返回的成功消息以及在 Appcelerators 控制台中验证这一点。

现在,我用 curl 模拟一个服务器,并向 "myChannel" 发送消息到 GCM。消息响应成功,我收到来自 GCM 的 message_id。

curl -H "Content-Type:application/json" -H "Authorization:key=APIKEY" --data '{"to": "/topics/myChannel","data": {"payload":{ "message": "Hi!" } }}' https://gcm-http.googleapis.com/gcm/send

我的问题是此消息未路由到设备。 我猜在 Titanium 中订阅的频道和我用于 GCM 的频道之间需要进行一些语法转换,但我不知道。

有趣的是,只要我在 curl 中放入 deviceToken,它就可以完美运行。

我是不是漏掉了一些基本的东西?通道是否不存在将同一消息路由到订阅它的多个设备? 非常感谢任何帮助和线索。

我的代码如下所示:

// Require the module
var CloudPush = require('ti.cloudpush');  

// Initialize the module
CloudPush.retrieveDeviceToken({
    success: deviceTokenSuccess,
    error: deviceTokenError
});

CloudPush.addEventListener('callback', receivePush);

function receivePush(evt) {
    alert("Notification received: " + JSON.stringify(evt.payload));
}

function deviceTokenSuccess(e) {
    Alloy.Globals.DeviceToken = e.deviceToken;
    Titanium.API.info(Alloy.Globals.DeviceToken);

    // Subscribe to topic 
    var Cloud = require("ti.cloud");
    var subscribe_data = {
        device_token: Alloy.Globals.DeviceToken,
        channel: Alloy.Globals.topicChannelID,
        type: Ti.Platform.name == 'android' ? 'android' : 'ios'
    };

    Cloud.PushNotifications.subscribeToken(
        subscribe_data, function (e) {
            if (e.success) {
                Titanium.API.info('Subscribed successfully');
            } else {
                Titanium.API.error('Error subscribing');
            }
        });
}

// Something went wrong getting the device token
function deviceTokenError(e) {
    Titanium.API.error('Failed ' + e.error);
}

GCM 不了解频道。这是 Arrow 跟踪并转化为订阅令牌的东西。因此,您需要通过 Arrow API 或 Appcelerator Dashboard 发送推送。

http://docs.appcelerator.com/platform/latest/#!/guide/Sending_and_Scheduling_Push_Notifications

http://docs.appcelerator.com/arrowdb/latest/#!/api/PushNotifications