titanium studio push notifications closing the app

ε祈祈猫儿з 提交于 2019-12-12 01:51:57

问题


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)



来源:https://stackoverflow.com/questions/23083077/titanium-studio-push-notifications-closing-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!