简体   繁体   中英

Titanium Appcelerator push notification callback called more than once

I have been able to register a mobile device and subscribe to a channel in Titanium . When the mobile device receives 2 push notifications and the user clicks on one of them .

The callback is called twice . How do i know for which notification it was clicked or how do i Know the total number of push notification is pending?

    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 is called twice because you are implementing callback function inside :

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

    }); 

and

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

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

});

Just implement the function like this :

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

});

For this How do I know for which notification it was clicked ?

Answer : You can check the badge number that you get in response from Push Notification inside callback function and based on that you can handle whether it is clicked or in pending state.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM