我有一个应该触发通知的 { 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 ?