nativescript-local-notifications: Cannot read property 'registerCallbacks' of undefined

Hello,

I am using NativeScript with Angular and Typescript. When I load the component which implements LocalNotifications.addOnMessageReceivedCallback or attempt to schedule a notification, I get a console error:

ERROR TypeError: Cannot read property ‘registerCallbacks’ of undefined NovenaScheduleComponent_Host.ngfactory.js? [sm]:1

My component: novena-schedule.component.ts

My template: novena-schedule.component.html

My whole project is here

Any help would be greatly appreciated. I am fairly new to all this so any I would be grateful for any requests for clarification, logs, etc.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 2
  • Comments: 16 (3 by maintainers)

Most upvoted comments

I ran into a similar issue with a fresh install in a previously established project. I’m using a local build. Running tns platform clean followed by tns build android resolved the problem.

Maybe just update the docs to note that a platform clean is necessary to get it working.

That’s required for every plugin that uses native libs. Not sure what the best approach would be. Perhaps an automatic clean after adding the plugin would help.

I finally got it working by updating Nativescript to latest.

For me worked after removing paltforms.

Thanks for taking time to reply and the suggestions, @Danziger . I have tried rebuilding the AAR but that doesn’t seem to do anything.

However, I have done some more digging and found that it works when running the tns commands together with a keystore option. I have no idea why that would matter.

Not working: tns run android

Working: tns run android --key-store-path <keystore_path> --key-store-password <keystore_pass>

hi @jnorkus

to be honest, this problem has not gone away for me entirely with my above fix.

The strange thing is, I am able to compile and run my app no problem on one machine, but on my other 2 machines (by machines i mean dev machines, not mobile devices) i am continuing to encounter this error despite applying my fix mentioned above. The only way to stop the error is to remove all uses of LocalNotifications from my code (i can still import it, but any time I call it, I get an error, as you are experiencing).

All of my machines use a cloud build to run the app. There is no difference between them that I can see; I reverted to the exact same commit on all 3, and only 1 can run without this exception.

Since you are experiencing the same thing, I do believe there is some problem and i am reopening.

Sorry @jnorkus, a bit busy this week with work and an event I’m attending here in Barcelona. I will try to take a look probably on Friday afternoon.

I have had this issue of namespaces being empty, but that has only happened to me while working on updates on the plugin, not while using a release version, and the reason was I forgot to add public to those methods.

You could try to clone the repo, run the Gradle build command, which should generate a file native-src/android/app/build/outputs/aar/app-release.aar, copy that to src/platforms/android/app-release.aar and then run publish/pack.sh, which will generate a file publish/package/nativescript-local-notifications-X.Y.Z.tgz that you should install into your project. I doubt this will fix the issue, but just in case you want to give it a try before I take a look and try to reproduce it.

I am facing the same issue but I’m not even using the addOnMessageReceivedCallback.

Android only. To reproduce:

  1. tns create local-notifications-test --template angular
  2. cd local-notifications-test
  3. tns plugin add nativescript-local-notifications
  4. Add the following code to the app.component.ts:
import { LocalNotifications } from 'nativescript-local-notifications';
...
constructor() {
    console.log(LocalNotifications !== null);
}
  1. tns run android

Result: TypeError: Cannot read property ‘registerCallbacks’ of undefined

update: fixed!

It had to do with the addOnMessageReceivedCallback being in the wrong component; I removed it from novena-schedule.component.ts and placed it in app.component.ts and it worked.

Thanks again for your response @Danziger and thanks for the plugin @EddyVerbruggen

Can you please try removing var LocalNotifications = require("nativescript-local-notifications"); from novena-schedule.component.ts and adding it to main.ts and main.aot.ts?