NativeScript / push-plugin

Contains the source code for the Push Plugin.
Apache License 2.0
123 stars 48 forks source link

_this.updateMessage is not a function #235

Closed lumayara closed 5 years ago

lumayara commented 5 years ago

I sent a message from Firebase and right away my emulator took me to an error:

An uncaught Exception occurred on "main" thread.
com.tns.NativeScriptException: 
Calling js method success failed

TypeError: _this.updateMessage is not a function
File: "file:///data/data/org.nativescript.LCGIncConnect/files/app/main.js, line: 17, column: 14

StackTrace: 
    Frame: function:'notificationCallbackAndroid', file:'file:///data/data/org.nativescript.LCGIncConnect/files/app/main.js', line: 17, column: 15

    at com.tns.Runtime.callJSMethodNative(Native Method)
    at com.tns.Runtime.access$2300(Runtime.java:34)
    at com.tns.Runtime$3.run(Runtime.java:1112)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

In my main.ts I have:

import * as pushPlugin from "nativescript-push-notifications";
//other code
var pushSettings = {
    senderID: "4394239484298 //Im just putting a random ID, it is not the one I am using", 
    notificationCallbackAndroid: (stringifiedData: String, fcmNotification: any) => {
        const notificationBody = fcmNotification && fcmNotification.getBody();
        this.updateMessage("New message!\n" + notificationBody + "\n" + stringifiedData);
    }
};
pushPlugin.register(pushSettings, (token: String) => {
    alert("Device registered. Access token: " + token);;
}, function() { });

//other code

Is there anything I am doind wrong?

fthuin commented 5 years ago

Yes, you did not define updateMessage() inside your class, the code inside the notificationCallback is just an example taken from the demo of this repo. You have to write your own callback logic with your own methods. Take a look at the demo code if you want to reuse the updateMessage function.

lini commented 5 years ago

You can also try replacing this.updateMessage( with alert( or console.log( in your code if you just want to see the notification body.

lumayara commented 5 years ago

Thank you, @fthuin and @lini ! Now it works :)