1

我尝试在JobScheduler

在设置组件值的第一行,我收到此错误:

ERROR TypeError: Cannot read property 'MyJobService' of undefined

这两个文件都在同一个文件夹中,并且昨天都正常工作。

  • 为了确定,我清理了平台文件夹,因为我将一些图片拖到drawble文件夹中app_Resources,我不得不再次构建项目,也许有些东西已经改变了。但它没有帮助。

什么会导致这个问题?我错过了什么吗?

JobScheduler.js:

function scheduleJob(context) {
        var component = new android.content.ComponentName(context, com.tns.notifications.MyJobService.class);
        const builder = new android.app.job.JobInfo.Builder(1, component);

        builder.setPeriodic(15 * 60 * 1000);
        builder.setOverrideDeadline(0);


        const jobScheduler = context.getSystemService(android.content.Context.JOB_SCHEDULER_SERVICE);
        console.log("Job Scheduled: " + jobScheduler.schedule(builder.build()));

}

module.exports.scheduleJob = scheduleJob;

MyJobService.js:

android.app.job.JobService.extend("com.tns.notifications.MyJobService", {
    onStartJob: function(params) {       
        console.log("Job execution ...");            

        var utils = require("utils/utils");
        var context = utils.ad.getApplicationContext();

            var builder = new android.app.Notification.Builder(context);
            console.log("setting notification head and body")

                builder.setContentTitle("notification triggered ")
                .setAutoCancel(true)
                .setColor(android.R.color.holo_purple)//getResources().getColor(R.color.colorAccent))
                .setContentText("body)
                .setVibrate([100, 200, 100])
                .setSmallIcon(android.R.drawable.btn_star_big_on);


            var mainIntent = new android.content.Intent(context, com.tns.NativeScriptActivity.class); 

            var mNotificationManager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

            const channelId = "my_channel_01";
            const name = "Channel name";
            const description = "Channel description";
            const importance = android.app.NotificationManager.IMPORTANCE_LOW;
            if (android.os.Build.VERSION.SDK_INT >= 26) {
                console.log("api level is good",android.os.Build.VERSION.SDK_INT)
            }
            const mChannel = new android.app.NotificationChannel(channelId, name,importance);


            mChannel.setDescription(description);
            mChannel.enableLights(true);

            mChannel.enableVibration(true);
            mNotificationManager.createNotificationChannel(mChannel);
            builder.setChannelId(channelId);
            mNotificationManager.notify(1, builder.build());

            return false;
        },

        onStopJob: function() {
            console.log("Stopping job ...");

        }

    });
4

0 回答 0