2014-04-22 45 views
0

我已經能夠註冊移動設備並訂閱Titanium中的頻道。當移動設備收到2個推送通知並且用戶點擊其中一個通知時。Titanium Appcelerator推送通知回叫不止一次

回調被稱爲兩次。我如何知道它被點擊的通知或我如何知道推送通知的總數正在等待?

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

回答

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

}); 

對於此如何知道它被點擊的通知?

:您可以檢查您在從推送通知響應回調函數內獲取並根據您可以處理它是否被點擊或掛起狀態的證件號碼。

希望這會有所幫助。

+0

感謝您的回覆。如果用戶清除托盤圖標中的通知並僅運行應用程序,則回調仍將執行。因此,我將回調包裝在trayClickLaunchedApp,trayClickFocusedApp函數中。但是,如果我們有兩個通知未決並且用戶點擊它們,那麼兩個回調都會執行兩次。 – Vidya