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

Push does not work on ios

$
0
0

Hello, can you tell me that my code is the error?

I would like clarified that the push works for android, ios but it does not work. In my panel 'acs titanium' the device will not know to register (ios)

Ti.include('common/globals.js');
 
var osname = Ti.Platform.osname;
 
var Cloud = require('ti.cloud');
if (osname == 'android') {
    var CloudPush = require('ti.cloudpush');
}
var deviceToken;
 
//user login on cloud using default credential
function loginUser() {
    Cloud.Users.login({
        login : 'MyLogin',
        password : 'MyPassword'
    }, function(e) {
        if (e.success) {
            var user = e.users[0];
            Ti.API.info("Loggin successfully");
            if (osname == 'android') {
                defaultSubscribe();
            } else {
                getDeviceToken();
            }
        } else {
            Ti.API.info("~~> Error :" + e.message);
        }
    });
}
 
// getting device token
function getDeviceToken() {
 
    if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
        function registerForPush() {
            Ti.Network.registerForPushNotifications({
                success : deviceTokenSuccess,
                error : deviceTokenError,
                callback : receivePush
            });
            // Remove event listener once registered for push notifications
            Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
        };
 
        // Wait for user settings to be registered before registering for push notifications
        Ti.App.iOS.addEventListener('usernotificationsettings', registerForPush);
 
        // Register notification types to use
        Ti.App.iOS.registerUserNotificationSettings({
            types : [Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT, Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND, Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE]
        });
 
    } else {
        // For iOS 7 and earlier
        Ti.Network.registerForPushNotifications({
            // Specifies which notifications to receive
            types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND],
            success : deviceTokenSuccess,
            error : deviceTokenError,
            callback : receivePush
        });
    }
 
    function deviceTokenError(e) {
        alert('Failed to register for push notifications! ' + e.error);
        // Ti.API.info("Error in Get Device Token Function :- " + e.message);
    };
 
    function deviceTokenSuccess(e) {
 
        deviceToken = e.deviceToken;
        Ti.API.info("deviceToken = " + deviceToken);
        registerForPush();
 
    };
 
    function receivePush(e) {
        Ti.API.info("push notification received" + JSON.stringify(e.data.alert));
        Ti.UI.iPhone.appBadge = null;
        Ti.UI.iPhone.appBadge = 0;
        // Titanium.UI.createAlertDialog({
            // title : "Title",
            // message : e.data.alert,
            // buttonNames : ['OK']
        // }).show();
    };
 
 
}
 
// register for push notification on cloud server
function registerForPush() {
    Cloud.PushNotifications.subscribe({
        channel : 'demo_alert',
        type : 'ios',
        device_token : deviceToken
    }, function(e) {
        if (e.success) {
            Ti.API.info('Success :' + ((e.error && e.message) || JSON.stringify(e)));
        } else {
            Ti.API.info('Error:' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}
 
if (osname == 'iphone') {
    loginUser();
} else {
 
    CloudPush.enabled = true;
    CloudPush.showTrayNotificationsWhenFocused = true;
    CloudPush.focusAppOnPush = false;
    Cloud.debug = true;
    CloudPush.retrieveDeviceToken({
        success : function deviceTokenSuccess(e) {
            Ti.API.info('Device Token: ' + e.deviceToken);
            deviceToken = e.deviceToken;
            loginUser();
        },
        error : function deviceTokenError(e) {
            alert('Failed to register for push! ' + e.error);
        }
    });
 
    function defaultSubscribe() {
        Cloud.PushNotifications.subscribe({
            channel : 'demo_alert',
            device_token : deviceToken,
            type : 'gcm'
        }, function(e) {
            if (e.success) {
                Ti.API.info('Subscribed for Push Notification!');
            } else {
                alert('Error:' + ((e.error && e.message) || JSON.stringify(e)));
            }
        });
    }
 
 
    CloudPush.addEventListener('callback', function(evt) {
        Ti.API.info('Notification :' + JSON.stringify(evt));
    });
 
}
 
var win1 = Titanium.UI.createWindow({
    title : 'Tab 1',
    backgroundColor : '#fff',
    url : 'ui/homeScreen.js',
    exitOnClose : true,
    navBarHidden : true,
    orientationModes : [Ti.UI.PORTRAIT]
});
 
Ti.include("ui/page.js");

Viewing all articles
Browse latest Browse all 8068

Trending Articles



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