2

嗨,我正在使用 ACS 在我的应用程序中实现推送通知。我在 tiapp.xml 中添加了 Ti.cloud 推送,如下所示。

   <module platform="android">ti.cloudpush</module>

我正在使用此代码实现它,但它给出了找不到 Ti.cloudpush 模块的错误。

var HomeWin = Titanium.UI.createWindow({
    backgroundColor : '#f00'
});
var CloudPush = require('ti.cloudpush');
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;

var deviceToken;

var Cloud = require('ti.cloud');
Cloud.debug = true;


var submit = Ti.UI.createButton({
    title : 'Register For Push Notification',
    color:'#000',
    height : 53,
    width : 200,
    top : 100,
});

HomeWin.add(submit);

submit.addEventListener('click', function(e) {
    CloudPush.retrieveDeviceToken({
       success: function deviceTokenSuccess(e) {
                    alert('Device Token: ' + e.deviceToken);
                    deviceToken = e.deviceToken
                    loginDefault();
                },
       error  : function deviceTokenError(e) {
                    alert('Failed to register for push! ' + e.error);
           }
    });
});


function loginDefault(e){
    //Create a Default User in Cloud Console, and login
    Cloud.Users.login({
        login: 'push123',
        password: 'push123'
    }, function (e) {
        if (e.success) {
            alert("login success");
            defaultSubscribe();
        } else {
              alert('Error: ' +((e.error && e.message) || JSON.stringify(e)));
        }
    });
}

function defaultSubscribe(){
    Cloud.PushNotifications.subscribe({
            channel: 'alert',
            device_token: deviceToken,
            type: 'android'
    }, function (e){
            if (e.success) {
               alert('Subscribed for Push Notification!');
            } else{
                alert('Error:' +((e.error && e.message) || JSON.stringify(e)));
            }
    });
}

CloudPush.addEventListener('callback', function (evt) {
    alert(evt);
    alert(evt.payload);
});

CloudPush.addEventListener('trayClickLaunchedApp', function (evt) {
    Ti.API.info('Tray Click Launched App (app was not running)');
    //alert('Tray Click Launched App (app was not running');
});

CloudPush.addEventListener('trayClickFocusedApp', function (evt) {
    Ti.API.info('Tray Click Focused App (app was already running)');
    //alert('Tray Click Focused App (app was already running)');
});
4

2 回答 2

1

即使我添加,我也遇到同样的问题

<module platform="android">ti.cloudpush</module>

我删除了所有项目并使用延期包名称在钛上创建新项目现在它正在工作但我找不到我以前的代码不起作用的原因

于 2013-02-28T08:22:02.147 回答
0

瓦哈布,

如下修改 tiapp.xml 并运行您的代码。我已经复制了你的代码,它运行良好。

<modules>
        <module platform="commonjs" version="2.3.0">ti.cloud</module>
        <module platform="android" version="2.0.7">ti.cloudpush</module>
</modules>
于 2012-11-14T04:12:20.060 回答