【问题标题】:NativeScript android NotificationChannel errorNativeScript android NotificationChannel 错误
【发布时间】:2018-12-10 12:41:30
【问题描述】:

我有一个应该触发通知的 { N } 应用程序。

我正在使用 notificationChannel,但是当应用崩溃时我一直收到同样的错误。

"System.err: TypeError: android.NotificationChannel is not a constructor"

我的代码是:

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

        // Do something useful here, fetch data and show notification for example
        var utils = require("utils/utils");
        var context = utils.ad.getApplicationContext();

        // var res=GeofenceService.mainFunction()
        //     console.log("res",res)
            var builder = new android.app.Notification.Builder(context);
            builder.setContentTitle("Scheduled Notification")
            .setAutoCancel(true)
            .setColor(android.R.color.holo_purple)//getResources().getColor(R.color.colorAccent))
            .setContentText("This notification has been triggered by Notification Service")
            .setVibrate([100, 200, 100])
            .setSmallIcon(android.R.drawable.btn_star_big_on);


        // will open main NativeScript activity when the notification is pressed
        var mainIntent = new android.content.Intent(context, com.tns.NativeScriptActivity.class); 

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

        // The id of the channel.
        const channelId = "my_channel_01";
        // The user-visible name of the channel.
        const name = "Channel name";
        // The user-visible description of the channel.
        const description = "Channel description";
        const importance = android.app.NotificationManager.IMPORTANCE_LOW;
        const mChannel = new android.app.NotificationChannel(channelId, name,importance);
        // Configure the notification channel.
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        // Sets the notification light color for notifications posted to this
        // channel, if the device supports this feature.
        mChannel.setLightColor(android.graphics.Color.RED);
        mChannel.enableVibration(true);
        mNotificationManager.createNotificationChannel(mChannel);

        builder.setChannelId(channelId);

        mNotificationManager.notify(1, builder.build());

        return false;
    },

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

来自该行的错误:

const mChannel = new android.app.NotificationChannel(channelId, name,importance);

他为什么告诉我 NotificationChannel 不是构造函数? 我错过了什么?

这是我获得此代码的地方,它似乎对其他人有用。

https://github.com/NativeScript/sample-android-background-services

编辑:

我刚刚检查了我的 API 级别及其 26,因此即使在通道行之前使用 if 语句,它也会崩溃。

当我在 android 清单中查看我的平台文件夹时,我看到了这个:

  <uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="25"/>

为什么是 25 ?

【问题讨论】:

    标签: android nativescript notification-channel


    【解决方案1】:

    android.app.NotificationChannel 仅适用于 API 级别 26 及更高版本(Android 8.0 - Oreo)。如果您使用的是早期版本,它将引发该错误。

    您必须在访问这些 api 之前检查版本,例如

    if (android.os.Build.VERSION.SDK_INT >= 26) {
     const mChannel = new android.app.NotificationChannel(channelId, name,importance);
    }
    

    更新:

    您必须将目标 SDK 设置为更高版本,至少 26。如果您的目标是较低版本 since August 2018,您甚至无法将 APK 上传到 Google Play。

    【讨论】:

    • 我刚刚检查了我的 API 级别,它是 26。
    • 我说的不是你用来构建的API级别,而是设备/模拟器所属的API级别。
    • 我的安卓设备版本是8.0。会不会是 android 平台获得了错误的 api 级别?请看我的编辑
    • 您是否将目标 SDK 设置为 26 或更高版本?
    • 在 app_resources 我的目标 sdk 是 26,现在平台更新到 26,但我仍然得到错误。 android.app.NotificationChannel 不是构造函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多