简体   繁体   中英

Cordova/Window.plugins undefined

I have an existing Telerik AppBuilder application that previously had working notifications before a major overhaul was done on the app. The app uses Cordova 3.7.0 and is attempting to implement the localnotifications plugin, whose source code can be found here : https://github.com/katzer/cordova-plugin-local-notifications . I installed it using the instructions specified in Telerik's "Verified Plugins" Documentation. However, it no longer functions. Through various alerts, alert(windows.plugins) and alert(cordova.plugins) is always undefined, as is alert(windows.plugins.notifications) and all permutations thereof. I saw responses to other replies saying that window.plugins would always be undefined and deprecated, but window.plugins.[PLUGIN_NAME] would exist. However, this does not seem to be the case, and this instead breaks the javascript. Below is the code current being used

define(['jQuery', 'base64'], function ($, base64) {

....

var that = this;
that.alert("starting");
document.addEventListener("deviceready", function() {
that.alert(JSON.stringify(window.plugins));
}, false);

....

}

The previously functioning code was

        if (that.hasNotificationPlugin()) {
            that.clearNotifications(function() {
                console.info('Setting notifications');
                // Schedule notifications for each day from the schedule
                $.each(data.DeliveryDaysThisWeek, function (i, day) {
                    var dow = day.DayOfWeek;
                    // Schedule notifications for each store within a day
                    $.each(day.Stores, function (i, store) {
                        // Only schedule the notification if the user 
                        // hasn't disabled notifications for this store
                        if (that.get('notification' + store.StoreId) !== 'false') {
                            var cutoffDate = new Date(store.CutOffDateTime);
                            var cutoffString = $.format.date(cutoffDate, 'h:mm a');

                            // Schedule it 30 minutes before the cutoff time
                            // or using the user defined time
                            var time = parseInt(that.get('notificationTime'));
                            if (isNaN(time)) {
                                that.set('notificationTime', "30");
                                time = 30;
                            }

                            var notifDate = new Date(cutoffDate.getTime() - time * 60 * 1000);
                            // Only schedule it if it's in the future
                            if (notifDate > new Date()) {
                                window.plugin.notification.local.add({
                                    id: (dow * 100000 + store.DeliveryTimes[0].DeliveryTimeId).toString(),
                                    date: notifDate,
                                    message: "The cutoff time is almost up! Place your order by " + cutoffString,
                                    title: store.Store.Restaurant.RestaurantName.trim(),
                                    json: JSON.stringify({StoreId: store.StoreId}),
                                    icon: 'icon'
                                });

that.alert(message) is a shortcut function for navigator.notificaiton.alert(message, false, "COMPANY_NAME") and works fine.

Why

this plugin uses cordova.plugins... or window.plugin... , can you try that? see the line of code responsible for this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM