I am trying to create an android service for handling Firebase messages (the nativescript-plugin-firebase library does not allow me to utilize data-only messages in the background), but I cannot seem to get the service to function correctly. Specifically, I am getting the following exception when a notification is received (and sometimes when the app is started):
System.err: java.lang.RuntimeException: Unable to instantiate service com.tns.services.CustomFCMService: com.tns.NativeScriptException: Cannot find object id for instance=com.tns.services.CustomFCMService@5a4269d
System.err: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3940)
System.err: at android.app.ActivityThread.access$1500(ActivityThread.java:219)
System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
System.err: at android.os.Handler.dispatchMessage(Handler.java:107)
System.err: at android.os.Looper.loop(Looper.java:214)
System.err: at android.app.ActivityThread.main(ActivityThread.java:7356)
System.err: at java.lang.reflect.Method.invoke(Native Method)
System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
My service is defined in TypeScript as follows:
@JavaProxy('com.tns.services.CustomFCMService')
class CustomFCMService extends (com.google as any).firebase.messaging.FirebaseMessagingService {
constructor() {
super();
}
public init() {
/* Do nothing */
}
public onMessageReceived(message: any) {
this.super.onMessageReceived(message);
/* Handle message receipt */
}
}
I've also added changed the following my webpack file (I can provide the rest of the file if needed):
// Add your custom Activities, Services and other Android app components here.
const appComponents = [
"tns-core-modules/ui/frame",
"tns-core-modules/ui/frame/activity",
resolve(__dirname, 'src/app/core/android/custom-fcm-service') /* My custom service */
];
And lastly, I've added the following to my AndroidManifest.xml
(inside of the application
tag):
<service android:name="com.tns.services.CustomFCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
I've tried changing various things in the AndroidManifest
, service file, and webpack configuration but I cannot seem to get it to work.
Thanks!
I have found a solution to my problem. It appears the erroneous code was inside my custom service:
public init() {
/* Do nothing */
}
Removing that method completely solves the issue I was having above.
It seems there is a default init
method, that has some important implementation details such as registering object ids to instances. I noticed that when I declared that method, not only was I not calling the super
version of it, a call to that method was also added in the constructor of the outputted .java file (which without the init
declaration does not exist). It seems some combination of those two effects causes the exception above.