EddyVerbruggen / nativescript-local-notifications

:mailbox: NativeScript plugin to easily schedule local notifications
MIT License
162 stars 57 forks source link

Cannot read property 'registerCallbacks' of undefined #108

Open rchisholm opened 6 years ago

rchisholm commented 6 years ago

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.

rchisholm commented 6 years ago

FYI I tried removing platform/android and rebuilding, got same error. App throws an exception when opening this component or scheduling a notification.

Danziger commented 6 years ago

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?

rchisholm commented 6 years ago

Thanks for your response @Danziger

I tried your recommendation and it didn't work; now the app throws an exception immediately when launching; not a console error, but an exception which prevents the app from launching, with a similar message:

TypeError:Cannot read property 'registerCallbacks' of undefined File: "file:///data/data/com.marianfathers-SaintStanislausPapczynski/files/app/tns_modules/nativescript-local-notifications/local-notifications.js, line: 8, column: 58

rchisholm commented 6 years ago

i should note that i misspoke when i originally posted this; the console error I receive is:

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

the original post for this issue has been edited to reflect this.

rchisholm commented 6 years ago

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

jnorkus commented 6 years ago

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);
    }
  5. tns run android

Result: TypeError: Cannot read property 'registerCallbacks' of undefined

rchisholm commented 6 years ago

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.

jnorkus commented 6 years ago

@EddyVerbruggen @Danziger any insight?

jnorkus commented 6 years ago

For some reason com.telerik.notifications namespace seems to be empty. Issues with native source build?

Reverting to 2.0.4 fixes the issue. However, I'm getting the undefined errors elsewhere. Seems to be the same problem.

Danziger commented 6 years ago

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.

jnorkus commented 6 years ago

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>

jnorkus commented 6 years ago

I finally got it working by updating Nativescript to latest.

mpochron commented 6 years ago

For me worked after removing paltforms.

rchisholm commented 6 years ago

this now is fixed when choosing the "clean build" option, or choosing the "clean cloud workspace" option in Sidekick. I believe it was related to some cloud-side cache which had bad data; similar to this issue

kdanieladams commented 5 years ago

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.

EddyVerbruggen commented 5 years ago

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.