0

i have a { N } app that should trigger notifications.

i'm using the notificationChannel but i keep getting the same error when the app crushed.

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

my code is :

    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 ...");
    }
});

the error coming from this row :

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

why is he telling me that NotificationChannel is not a constructor? what did i missed ?

this is where i got this code and it seems to work for other people.

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

Edit:

i just checked my API Level and its 26 so even with the if statement before the channel line its crushing.

when im looking at my platforms folder in the android manifest i see this :

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

why its 25 ?

Johnny
  • 105
  • 2
  • 14

1 Answers1

1

android.app.NotificationChannel is available only on API Level 26 and above (Android 8.0 - Oreo). If you are using an earlier version, it will throw that error.

You must check the version before you access those apis, something like

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

Update:

You must set your target SDK to a higher version, at least 26. You will not be even able to upload your APK to Google Play if you are targeting a lower version since August 2018.

Manoj
  • 21,753
  • 3
  • 20
  • 41
  • i just checked my API Level ant its 26. – Johnny Dec 10 '18 at 16:17
  • I'm not talking about the API level you are using to build, but the API level the device / emulator belongs to. – Manoj Dec 10 '18 at 16:24
  • my android device version is 8.0. can it be the android platforms getting a wrong api level ? please look at my edit – Johnny Dec 10 '18 at 17:17
  • Did you set you target SDK to 26 or later? – Manoj Dec 10 '18 at 17:21
  • in app_resources my target sdk is 26 and now the platforms updeted to 26 but still i get the error. **android.app.NotificationChannel is not a constructor** – Johnny Dec 10 '18 at 17:44
  • As far I know, API level could be the only possible reason for this error. I'm not able to reproduce the issue on my end, it works with Playground too. – Manoj Dec 10 '18 at 17:49
  • thank you ! i updated the build SDK and its works fine right now. – Johnny Dec 11 '18 at 11:02