titanium studio push notifications closing the app

2019-09-01 01:58发布

问题:

I am trying to show push notifications in android app, developed by titanium studio, my getting alert with unfortunately app is closed after this my is clossing, my code is

try {

    CloudPush.retrieveDeviceToken({
        success : function deviceTokenSuccess(e) {
            deviceID = e.deviceToken;
            Ti.API.info(deviceID);
            Ti.App.Properties.setString('deviceid', '' + deviceID);
        },
        error : function deviceTokenError(e) {
            alert('Failed to register for push! ' + e.error);
        }
    });

} catch(e) {
    alert('Error :', e);
}

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

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


// Process incoming push notifications
CloudPush.addEventListener('callback', function(evt) {
    //alert(evt.payload);
    var alertNotification = Titanium.UI.createAlertDialog({
                title : 'app',
                message : evt.data.alert,
                cancel : 1,
                buttonNames : ['OK']
            });
            alertNotification.show();

});
// Triggered when the push notifications is in the tray when the app is not running
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
    Ti.API.info('Tray Click Launched App (app was not running)');
});
// Triggered when the push notifications is in the tray when the app is running
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
    Ti.API.info('Tray Click Focused App (app was already running)');
}); 

what was the wrong I am doing will any one suggest me, how to resolve this isssue

thanks in advace

回答1:

Try changing your code as follows

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

    CloudPush.retrieveDeviceToken({
        success : function deviceTokenSuccess(e) {
            deviceID = e.deviceToken;
            CloudPush.enabled = true;
            Ti.API.info(deviceID);
            Ti.App.Properties.setString('deviceid', '' + deviceID);
        },
        error : function deviceTokenError(e) {
            alert('Failed to register for push! ' + e.error);
        }
    });

} catch(e) {
    alert('Error :' + e);
}

// Process incoming push notifications
CloudPush.addEventListener('callback', function(evt) {
    //alert(evt.payload);
    var alertNotification = Titanium.UI.createAlertDialog({
                title : 'app',
                message : evt.data.alert,
                cancel : 1,
                buttonNames : ['OK']
            });
            alertNotification.show();

});
// Triggered when the push notifications is in the tray when the app is not running
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
    Ti.API.info('Tray Click Launched App (app was not running)');
});
// Triggered when the push notifications is in the tray when the app is running
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
    Ti.API.info('Tray Click Focused App (app was already running)');
}); 

I have removed the callback methods from your code. And also please make sure that you're using the latest CloudPush module. Please check the TiApp.xml, if you have selected '*' as module version, change it to latest number (3.2.3 for me)