arleyandrada / PushClient

FREE and Open Source - Titanium PushClient Module
Other
44 stars 17 forks source link

Need more example for custom server #172

Open kerberosargos opened 7 years ago

kerberosargos commented 7 years ago

I have custom server for push message. I am using your module with codes as below. But mostly I am getting error SERVICE_NOT_AVAILABLE and I waiting get error about 20 seconds. I would like to store device ID but I can not understand to use that without PushClient.EVENT_SUCCESS

Please help me how to get error quickly without waiting about 20 seconds or how to use your module correctly

`var $PushClient = require('br.com.arlsoft.pushclient');

var $PushClientRegisterOptions = {
    GCMSenderId : 'xxxxxxx',
    APNTypes : [$PushClient.NOTIFICATION_TYPE_BADGE, $PushClient.NOTIFICATION_TYPE_ALERT, $PushClient.NOTIFICATION_TYPE_SOUND]
};

if (OS_IOS) {
    var issue17030 = Titanium.Network.registerForPushNotifications;
    var issue17030iOS8 = Titanium.App.iOS.registerUserNotificationSettings;
}

if (OS_IOS) {

    var $PushClientIosAcceptAction = $PushClient.createAction({
        identifier : 'ACCEPT_IDENTIFIER',
        title : 'Accept',
        activationMode : $PushClient.NOTIFICATION_ACTIVATION_MODE_FOREGROUND,
        destructive : false,
        authenticationRequired : true
    });

    var $PushClientIosRejectAction = $PushClient.createAction({
        identifier : 'REJECT_IDENTIFIER',
        title : 'Reject',
        activationMode : $PushClient.NOTIFICATION_ACTIVATION_MODE_BACKGROUND,
        destructive : true,
        authenticationRequired : false
    });

    var $PushClientIosDownloadContent = $PushClient.createCategory({
        identifier : 'APPROVE_CONTENT',
        actionsForMinimalContext : [$PushClientIosAcceptAction, $PushClientIosRejectAction],
        actionsForDefaultContext : [$PushClientIosAcceptAction, $PushClientIosRejectAction]
    });

    $PushClientRegisterOptions.Categories = [$PushClientIosDownloadContent];
}

var $PushClientEventSuccess = function($Event) {

    if (!$Event) {
        Titanium.API.error('$PushClientEventSuccess -> Invalid success');
        return;
    }

    $VarsGlobal.DeviceToken = $Event.registrationId;

    var $RegisterDeviceRemoteServerParams = {
        os : OS_ANDROID === true ? 'android' : 'ios',
        language : Titanium.Locale.currentLanguage,
        country : Titanium.Locale.currentCountry,
        version : Titanium.App.version,
        deviceMacAdress : Titanium.Platform.macaddress,
        deviceUserName : Titanium.Platform.username,
        token : $VarsGlobal.DeviceToken
    };

    var $Xhr = Titanium.Network.createHTTPClient({

        onload : function() {
            $Xhr = null;
        },

        onerror : function() {
            Titanium.API.error("$PushClientEventSuccess -> Token can not sent to our backend");
        }
    });

    $Xhr.open("POST", "http://www.myserver.com/pages/php/register-push-notification-devices.php");
    $Xhr.send($RegisterDeviceRemoteServerParams);

};

var $PushClientEventError = function($Event) {

    /*************************************
     error explain about not getting device token
     https://eladnava.com/google-cloud-messaging-extremely-unreliable/
     *************************************/

    Titanium.API.error('$PushClientEventError -> ' + JSON.stringify($Event));
    if (!$Event) {
        Titanium.API.error('$PushClientEventError -> Invalid error');
        return;
    }

    alert(L('TextError_PushError') +  $Event.error);

    switch ($Event.code) {
    case $PushClient.ERROR_SENDER_ID:
        Titanium.API.error('$PushClientEventError -> Undefined GCMSenderId');
        //> Only for Google Cloud Messaging (Android)
        break;
    case $PushClient.ERROR_PLAY_SERVICES:
        Titanium.API.error('$PushClientEventError -> Google Play Services not available: ' + $Event.error);
        //> Only for Google Cloud Messaging (Android)
        break;
    case $PushClient.ERROR_NOT_SUPPORTED:
        Titanium.API.error('$PushClientEventError -> Not supported error: ' + $Event.error);

        /*****************************************
         Possible error messages for iOS
         - "Unable to run with iOS Simulator"
         - "Unable to run with iOS in DEBUG mode"
         - "Unable to run with iOS DEV profile due to Titanium Mobile issue
         - #17030"
         Possible error messages for Android
         - "This device is not supported"
         *******************************************/

        break;
    case $PushClient.ERROR_REGISTER:
        Titanium.API.error('$PushClientEventError -> Unable to register this device: ' + $Event.error);
        break;
    case $PushClient.ERROR_UNREGISTER:
        Titanium.API.error('$PushClientEventError -> Unable to unregister this device: ' + $Event.error);
        break;
    default:
        Titanium.API.error('$PushClientEventError -> Unknown error: ' + JSON.stringify($Event));
    }
};

var $PushClientEventCallback = function($Event) {
    //< $FncsGlobal.$FncTiApiInfo('$PushClientEventCallback -> ' + JSON.stringify($Event));

    if (!$Event) {
        Titanium.API.error('$PushClientEventCallback -> Invalid callback');
    } else if ($Event.mode == $PushClient.MODE_FOREGROUND) {
        if (OS_ANDROID) {
            $PushClient.showLocalNotification($Event.data);
            //> Force to show local notification
        }

        //< $FncsGlobal.$FncTiApiInfo('$PushClientEventCallback -> Callback in Foreground: ' + JSON.stringify($Event.data));
        $FncManipuatePushNotificationOnReceive(JSON.stringify($Event.data));
        $FncCancelNotifications(1000);
        //> Push data received with app in foreground

    } else if ($Event.mode == $PushClient.MODE_CLICK) {
        //< $FncsGlobal.$FncTiApiInfo('$PushClientEventCallback -> Callback from Click: ' + JSON.stringify($Event.data));
        //> Push data received when user clicks in notification message
        $FncManipuatePushNotificationOnReceive(JSON.stringify($Event.data));
        $FncCancelNotifications(0);
    } else if ($Event.mode == $PushClient.MODE_BACKGROUND) {
        //> Requires set remote-notification UIBackgroundModes in tiapp.xml
        $PushClient.endBackgroundHandler($Event.data.handlerId);
        //> Put the application back to sleep before any UI interations
        //< $FncsGlobal.$FncTiApiInfo('$PushClientEventCallback -> Callback from Silent: ' + JSON.stringify($Event.data));
        //> Push data received with app in background
        $FncManipuatePushNotificationOnReceive(JSON.stringify($Event.data));
        $FncCancelNotifications(1000);
    } else if ($Event.mode == $PushClient.MODE_ACTION) {
        // $FncsGlobal.$FncTiApiInfo('$PushClientEventCallback -> Callback from Action: ' + $Event.category + ', ' + $Event.identifier + ', ' + JSON.stringify($Event.data));
        //> Push data received when user choose an action from notification message
        $FncCancelNotifications(0);
    } else {
        // $FncsGlobal.$FncTiApiInfo('$PushClientEventCallback -> Callback: ' + JSON.stringify($Event.data));
        $FncCancelNotifications(0);
    }

};

$PushClient.addEventListener($PushClient.EVENT_SUCCESS, $PushClientEventSuccess);
$PushClient.addEventListener($PushClient.EVENT_ERROR, $PushClientEventError);
$PushClient.addEventListener($PushClient.EVENT_CALLBACK, $PushClientEventCallback);
$PushClient.registerPush($PushClientRegisterOptions);`