Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Push Notifications weird problem

$
0
0

Hello, i am developing an android app which contains push notifications. The app works properly with the push notif. service but there is a small problem. When app opens, it logins to acs, retrieves device token and then it subscribes to a channel. But if i close the app and then reopen it, the function CloudPush.retrieveDeviceToken() cant run. Here is the code

alloy.js

var com = require('common');
com.loginToACS();
common.js
var Cloud = require('ti.cloud');
exports.loginToACS = function() {
    if (Titanium.Network.networkType == Titanium.Network.NETWORK_NONE) {
        alert(L('alert_nonetwork'));        
    } else {
        Cloud.debug = true;
        // Login to Titanium Cloud
        Cloud.Users.login({
            login : 'myLogin',
            password : 'myPass'
        }, function(e) {
            if (e.success) {
                Ti.API.info('Login success!');
                registerForACSNotifications();
            } else {
                alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
            }
        });
    }
};
 
// Retrieve device token and subscribe to channel in success
var CloudPush = require('ti.cloudpush');
var deviceToken = null;
function registerForACSNotifications() {
    try {
        CloudPush.retrieveDeviceToken({
            success : function deviceTokenSuccess(e) {
                Cloud.PushNotifications.subscribe({
                    channel : 'myChannel',
                    type : 'gcm',
                    device_token : e.deviceToken
                }, function(e) {
                    if (e.success) {
                        Ti.API.info('Success! ' + ((e.error && e.message) || JSON.stringify(e)));
                    } else {
                        Ti.API.info('Error:\\n ' + ((e.error && e.message) || JSON.stringify(e)));
                    }
                });
            },
            error : function deviceTokenError(e) {
                Ti.API.info('Failed to register for push! ' + e.error);
            }
        });
        CloudPush.enabled = true;
        CloudPush.addEventListener('callback', function(evt) {
            Ti.API.info("evt.payload: " + evt.payload);
        });
    } catch(x) {
        alert(L('alert_acsregerror'));
    }
}
When the app reopens, it goes to catch(x) { alert(L('alert_acsregerror')); } Where is the problem? Should i get deviceToken once and then save it locally and use it for ever? The app must get device token every time it opens?

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>