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

Setting up android push notification

$
0
0

Hey,

I have the following code and am attempting to register both iOS and android phones with ACS. I have iOS working but android registration seems to fail

if ( Titanium.Platform.name == "android" ){ 
    var CloudPush = require("ti.cloudpush");
    //login();
}
 
device_token = Ti.App.Properties.getString("device_token", "empty");
push_list = Ti.App.Properties.getString("pushlist", "empty"); 
 
if (device_token == "empty"){
    myDialog("Register for Notifications?","Click Yes to register for push notifications"); 
    if(Ti.App.Properties.getString("yes2push") == "yes"){
        login();
        getDeviceToken();
        subscribeToIOSServerPush();
 
        Ti.App.Properties.setString("yes2push","no");
    }
}
 
 
function login(){
    Cloud.Users.login({
        login: 'bastien',
        password: 'sweden'
        }, function (e) {
        if (e.success) {
 
            var user = e.users[0];
            //getDeviceToken();
            //subscribeToIOSServerPush();
 
        } else {
            myAlert('error', 'Error:\\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}
 
 
//REGISTER LOCAL PUSH NOTIFICATION HERE
function getDeviceToken(){
    //trace("REGISTERING LOCAL PUSH");
    var user_device_token;
    switch (Ti.Platform.name){
        case "iPhone OS":
 
            Titanium.Network.registerForPushNotifications({
                types: [
                Titanium.Network.NOTIFICATION_TYPE_BADGE,
                Titanium.Network.NOTIFICATION_TYPE_ALERT,
                Titanium.Network.NOTIFICATION_TYPE_SOUND
                ],
                success:function(e)
                {
                    user_device_token = e.deviceToken;
                    Ti.App.Properties.setString("device_token",user_device_token);
                },
                error:function(e)
                {
                    myAlert('error',"Error during registration: "+e.error);
 
                },
                callback:function(e)
                {
                    // called when a push notification is received.
                    myAlert('notice', JSON.stringify(e.data.alert));
                    var thisData = {
                        id : Titanium.Platform.id,
                        device_token : user_device_token,
                        push : 'push notice received',
                        msg : e.data.alert
                    }
                    Ti.App.Properties.setString("latest_buddy_alert",JSON.stringify(thisData));
                    sendToCloud(JSON.stringify(thisData));
            }});
            break;
 
        case "android":
 
            CloudPush.retrieveDeviceToken({
            success: function (e) {
                //myAlert('notice','Device Token: ' + e.deviceToken);
                user_device_token = e.deviceToken
                Ti.App.Properties.setString("device_token",user_device_token);
                myAlert('notice','received token'+user_device_token);
                subscribeToIOSServerPush();
            },
            error: function (e) {
                myAlert('error','Failed to get token for push! ' + e.error);
            }
        });
        break;
    }//ens switch
 
}
 
//REGISTER SERVER PUSH
function subscribeToIOSServerPush(){
    //trace("Registering server push");
    //user_device_token = Ti.App.Properties.getString("device_token");
    //myAlert('notice','attempting server push registration with uuid:' + user_device_token);
    var phoneType;
    var device_token = Ti.App.Properties.getString("device_token");
 
    switch (Ti.Platform.name){
        case "iPhone OS":
            phoneType = "ios";
            break;
        case "android":
            phoneType = "android";
            break;
    }
 
    Cloud.PushNotifications.subscribe({
        channel: 'buddy_notice',
        type: phoneType,
        device_token: device_token
        }, function (e) {
        if (e.success) {
 
            myAlert("notice","Successful server push registration:\n");  //+((e.error && e.message) || JSON.stringify(e)));
            //var myID = Titanium.Network.remoteDeviceUUID;
            //Ti.API.info('successful push registration '+ myID);
            //Ti.App.Properties.setString('uuid', myID);
 
        } else {
 
            myAlert("error", "Server push registration error:\n" + ((e.error && e.message) || JSON.stringify(e)));
            Ti.API.info('failed push registration '+ device_token);
        }
 
    });
}
Consistently getting a 'device token missing error'...

Viewing all articles
Browse latest Browse all 8068

Trending Articles



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