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

时间:2015-08-26 18:52:03

标签: android push-notification titanium google-cloud-messaging channel

我没有更多的头发要撕掉并需要帮助..

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

将推送消息发送到各个设备的工作正常,发送到主题的格式是相同的,除了指定deviceToken,在for / 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);
}

1 个答案:

答案 0 :(得分:2)

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

相关问题