microsoft / cordova-plugin-code-push

Cordova plugin for CodePush
http://appcenter.ms
Other
644 stars 335 forks source link

Force restart after Updated message #640

Closed iecxiv closed 4 years ago

iecxiv commented 4 years ago

Hello, I want to force restart after Updated message. How can I do? I used this code.


   var app = {
    // Application Constructor
    initialize: function () {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener("backbutton", this.onBackKeyDown, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function () {

        /* Invoke sync with the custom options, which enables user interaction.
           For customizing the sync behavior, see SyncOptions in the CodePush documentation. */
        window.codePush.sync(
            function (syncStatus) {
                switch (syncStatus) {
                    // Result (final) statuses
                    case SyncStatus.UPDATE_INSTALLED:
                        app.displayMessage("La aplicación ha sido actualizada. Reiniciela, para ver los cambios. De lo contrario se producirá un error.");
                        break;
                    case SyncStatus.UP_TO_DATE:
                        //app.displayMessage("La aplicación esta actualizada.");
                        break;
                    case SyncStatus.UPDATE_IGNORED:
                        //app.displayMessage("El usuario ha decidido no instalar la actualización.");
                        break;
                    case SyncStatus.ERROR:
                        //app.displayMessage("Un error ocurrió al comprobar actualizaciones.");
                        break;

                    // Intermediate (non final) statuses
                    case SyncStatus.CHECKING_FOR_UPDATE:
                        console.log("Checking for update.");
                        break;
                    case SyncStatus.AWAITING_USER_ACTION:
                        console.log("Alerting user.");
                        break;
                    case SyncStatus.DOWNLOADING_PACKAGE:
                        console.log("Downloading package.");
                        break;
                    case SyncStatus.INSTALLING_UPDATE:
                        console.log("Installing update");
                        break;
                }
            },
            {
                installMode: InstallMode.ON_NEXT_RESTART, updateDialog: false
            },
            function (downloadProgress) {
                console.log("Descargando " + downloadProgress.receivedBytes + " de " + downloadProgress.totalBytes + " bytes.");
            });

        // continue application initialization
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function (id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    // Displays an alert dialog containing a message.
    displayMessage: function (message) {
        navigator.notification.alert(
            message,
            null,
            'Actualización',
            'OK');
    },
    onBackKeyDown: function () {
        if (confirm("¿Desea salir de la aplicación?") == true){
            navigator.app.exitApp();
        }
    }
};

app.initialize();```
iecxiv commented 4 years ago

I want silent update, displaying a message of update aplied and force restart when press ok.

iecxiv commented 4 years ago

Solved

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function () {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener("backbutton", this.onBackKeyDown, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function () {

        /* Invoke sync with the custom options, which enables user interaction.
           For customizing the sync behavior, see SyncOptions in the CodePush documentation. */
        window.codePush.sync(
            function (syncStatus) {
                switch (syncStatus) {
                    // Result (final) statuses
                    case SyncStatus.UPDATE_INSTALLED:
                        app.displayMessage("La aplicación ha sido actualizada. Disfruta de los nuevos contenidos.");
                        app.restart();
                        break;
                    case SyncStatus.UP_TO_DATE:
                        //app.displayMessage("La aplicación esta actualizada.");
                        break;
                    case SyncStatus.UPDATE_IGNORED:
                        //app.displayMessage("El usuario ha decidido no instalar la actualización.");
                        break;
                    case SyncStatus.ERROR:
                        //app.displayMessage("Un error ocurrió al comprobar actualizaciones.");
                        break;

                    // Intermediate (non final) statuses
                    case SyncStatus.CHECKING_FOR_UPDATE:
                        console.log("Checking for update.");
                        break;
                    case SyncStatus.AWAITING_USER_ACTION:
                        console.log("Alerting user.");
                        break;
                    case SyncStatus.DOWNLOADING_PACKAGE:
                        console.log("Downloading package.");
                        break;
                    case SyncStatus.INSTALLING_UPDATE:
                        console.log("Installing update");
                        break;
                }
            },
            {
                installMode: InstallMode.ON_NEXT_RESTART, updateDialog: false
            },
            function (downloadProgress) {
                console.log("Descargando " + downloadProgress.receivedBytes + " de " + downloadProgress.totalBytes + " bytes.");
            });

        // continue application initialization
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function (id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    // Displays an alert dialog containing a message.
    displayMessage: function (message) {
        navigator.notification.alert(
            message,
            null,
            'App actualizada',
            'OK');
    },
    restart: function () {
        codePush.restartApplication();
    },    
    onBackKeyDown: function () {
        if (confirm("¿Desea salir de la aplicación?") == true){
            navigator.app.exitApp();
        }
    }
};

app.initialize();