Titanium Appcelerator推送通知回调多次调用

时间:2014-04-22 10:57:35

标签: android push-notification titanium

我已经能够注册移动设备并订阅Titanium中的频道。当移动设备收到2个推送通知并且用户点击其中一个时。

回调被调用两次。我怎么知道它被点击了哪个通知,或者我怎么知道推送通知的总数还有待处理?

    var CloudPush = require('ti.cloudpush');

    //CloudPush.singleCallback = true; 

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

    // Enable push notifications for this device
    // Save the device token for subsequent API calls
    function deviceTokenSuccess(e) {
        //CloudPush.enabled = true;

        deviceToken = e.deviceToken;
        subscribeToChannel();
        //sendTestNotification();

    }

    function deviceTokenError(e) {
        //alert('Failed to register for push notifications! ' + e.error);
    }

    // Triggered when the push notifications is in the tray when the app is not running
    CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
        CloudPush.addEventListener('callback', function(evt) {
            var title = JSON.parse(evt.payload);

        });

    });
    // Triggered when the push notifications is in the tray when the app is running
    CloudPush.addEventListener('trayClickFocusedApp', function(evt) {

        CloudPush.addEventListener('callback', function(evt) {
            var title = JSON.parse(evt.payload);
        });

    });



var Cloud = require("ti.cloud");
function subscribeToChannel() {
    // Subscribes the device to the 'test' channel
    // Specify the push type as either 'android' for Android or 'ios' for iOS
    //alert(deviceToken+"ss");
    Titanium.App.Properties.setString("token", deviceToken);
    Cloud.PushNotifications.subscribeToken({
        device_token : deviceToken,
        channel : 'test',
        type : Ti.Platform.name == 'android' ? 'android' : 'ios'
    }, function(e) {
        if (e.success) {
            //alert('Subscribed');
        } else {
            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}

1 个答案:

答案 0 :(得分:3)

callback被调用两次,因为你在里面实现了回调函数:

CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
    CloudPush.addEventListener('callback', function(evt) {
        var title = JSON.parse(evt.payload);

    }); 

CloudPush.addEventListener('trayClickFocusedApp', function(evt) {

    CloudPush.addEventListener('callback', function(evt) {
        var title = JSON.parse(evt.payload);
    });

});

只需实现这样的功能:

CloudPush.addEventListener('callback', function(evt) {
var title = JSON.parse(evt.payload);
});

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

 });

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

});

对于此如何知道点击了哪个通知?

回答:您可以检查回复功能中的推送通知所获得的徽章编号,并根据您可以处理它是否被点击或处于待处理状态。

希望这有帮助。