簡體   English   中英

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

[英]Titanium Appcelerator push notification callback called more than once

我已經能夠注冊移動設備並訂閱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)));
        }
    });
}

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

});

為此,我如何知道單擊了哪個通知?

:您可以檢查在回調函數中從Push Notification響應中獲得的徽章編號,並以此為基礎來處理它是單擊還是處於掛起狀態。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM