【问题标题】:Titanium Appcelerator push notification callback called more than once多次调用 Titanium Appcelerator 推送通知回调
【发布时间】:2014-06-06 16:56:30
【问题描述】:

我已经能够在 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)));
        }
    });
}

【问题讨论】:

    标签: android push-notification titanium


    【解决方案1】:

    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)');
    
    });
    

    对于这个我怎么知道点击了哪个通知?

    答案:您可以在回调函数中检查您从推送通知响应中获得的徽章编号,并根据该编号来处理它是被点击还是处于待处理状态。

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回复。如果用户清除托盘图标中的通知并仅运行应用程序,则仍会执行回调。因此,我将回调包装在 trayClickLaunchedApp、trayClickFocusedApp 函数中。但是,如果我们有两个待处理的通知并且用户单击它们,则回调会执行两次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多