NativeScript Android Background Service - nativescript

I am developing NativeScript android application. I am new to NativeScript. I want to run background service, which is checking service periodically and give notifications to user even though app not opened on device. I followed instructions from Nativescript site
https://www.nativescript.org/blog/using-android-background-services-in-nativescript
But when i call setupAlarm function, i received error can't instantiate class com.tns.notifications.NotificationIntentService; no empty constructor
please help me. i give full exception details below
The application crashed because of an uncaught exception. You can look at "stackTrace" or "nativeException" for more detailed information about the exception.
An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to instantiate service com.tns.notifications.NotificationIntentService: java.lang.InstantiationException: can't instantiate class com.tns.notifications.NotificationIntentService; no empty constructor
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2543)
at android.app.ActivityThread.access$1800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class com.tns.notifications.NotificationIntentService; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2540)
... 10 more

To solve the no empty constructor issue, you can use nativescript-android-utils like this:
Install:
npm install nativescript-android-utils --save
In you code:
com.pip3r4o.android.app.IntentService.extend("com.mypackage.MyClass", {
onHandleIntent: function(){}
});
Now, com.mypackage.MyClass will have an empty constructor.

i found working nativescript android background service project from here
https://github.com/NativeScript/sample-android-background-services

The latest release of Nativescript allows you to use workers for multithreading applications. I believe it's what you should be using. In the tutorial you provided they also mention using web workers in the future.
What’s next
In the upcoming releases we will be rolling out support for the Web
Workers feature in NativeScript, allowing you to execute code in a
background thread, all through Angular/JavaScript.
Hope it helps.

Related

Unable to start activity ProxyBillingActivity, NullPointerException by getIntentSender()

We are using the latest Google Play billing client com.android.billingclient:billing:4.1.0 as described in https://developer.android.com/google/play/billing/integrate
Sometimes we get this non-reproducible exception in the crash logs, mostly Android 10 devices.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.android.billingclient.api.ProxyBillingActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.IntentSender android.app.PendingIntent.getIntentSender()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3654)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8154)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.IntentSender android.app.PendingIntent.getIntentSender()' on a null object reference
at com.android.billingclient.api.ProxyBillingActivity.onCreate(com.android.billingclient:billing##4.1.0:12)
at android.app.Activity.performCreate(Activity.java:7963)
at android.app.Activity.performCreate(Activity.java:7952)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3629)
... 11 more
Any ideas how to fix/catch?

Calling js method onDestroy failed

I am trying to publish my app, but, Google play Pre-launch report return this Error: View not added to this instance. View: t(73) CurrentParent: t(69) ExpectedParent: t(66). This error was happens multiple times. I did add onDestroy on each of my components.
The app just reproduce a web page from Wordpress. (Client requirement).
I use WebView component to achieve that.
After I apply my changes, the amount error reduced to 1. Now, only happen in Galaxy S9.
Well, at this point I don’t know where can I fix that.
Here the error:
FATAL EXCEPTION: main
Process: org.fcoe.iplan, PID: 26666
java.lang.RuntimeException: Unable to destroy activity {org.fcoe.iplan/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onDestroy failed
Error: View not added to this instance. View: t(73) CurrentParent: t(69) ExpectedParent: t(66)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4603)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4621)
at android.app.ActivityThread.-wrap5(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1757)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: com.tns.NativeScriptException: Calling js method onDestroy failed
Error: View not added to this instance. View: t(73) CurrentParent: t(69) ExpectedParent: t(66)
at com.tns.Runtime.callJSMethodNative(Native Method)
at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1286)
at com.tns.Runtime.callJSMethodImpl(Runtime.java:1173)
at com.tns.Runtime.callJSMethod(Runtime.java:1160)
at com.tns.Runtime.callJSMethod(Runtime.java:1138)
at com.tns.Runtime.callJSMethod(Runtime.java:1134)
at com.tns.NativeScriptActivity.onDestroy(NativeScriptActivity.java:39)
at android.app.Activity.performDestroy(Activity.java:7462)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1255)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnDestroy(MonitoringInstrumentation.java:1)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4590)
... 9 more
My coworker suggest to add StackLayout tag around <page-router-outlet></page-router-outlet> in app.component.html.
I did his suggestion and all works fine in Google Play tests.
Here the code looks like:
<StackLayout>
<page-router-outlet></page-router-outlet>
</StackLayout>

Xamarin Prism Unable to navigate to LoginPageViewModel

I am trying to make my Xamarin Project use MVVM with Prism and DryIoc.
I mostly want to use AutoRegistration like below:
[AutoRegisterForNavigation]
...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//Pages
containerRegistry.RegisterForNavigation<NavigationPage>();
//Services
containerRegistry.RegisterSingleton<ILocalDatabase, LocalDatabase>();
containerRegistry.RegisterSingleton<IUserProfileDataStore, UserProfileDataStore>();
containerRegistry.RegisterSingleton<IApplicationSettings, ApplicationSettings>();
containerRegistry.RegisterSingleton<ILogger, Logger>();
containerRegistry.RegisterSingleton<IApiService, ApiService>();
containerRegistry.RegisterSingleton<IUserSession, UserSession>();
containerRegistry.Register<IBrowser, BrowserImplementation>();
containerRegistry.Register<IConnectivity, ConnectivityImplementation();
containerRegistry.Register<IFileSystem, FileSystemImplementation>();
containerRegistry.Register<ICoreServices, CoreServices>();
}
I have also tried Manual Registration:
containerRegistry.RegisterForNavigation<LoginPage, LoginPageViewModel>();
Neither works, It hits the Login Page code behind then breaks with the following error:
Exception - High: Prism.Ioc.ContainerResolutionException:
An unexpected error occurred while resolving 'AppetiteApp.ViewModels.LoginPageViewModel' --->
DryIoc.ContainerException: code: UnableToResolveUnknownService; message: Unable to resolve
Resolution root AppetiteApp.ViewModels.LoginPageViewModel
with passed arguments [value(Prism.Navigation.ErrorReportingNavigationService)]
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'
I've also tried using a Linker file setting it's build action to "linkdescription"
As for my Login Page here is the declaration
public LoginPageViewModel(ICoreServices coreServices)
: base(coreServices)
The constructor of LoginPageViewModel requires the ICoreServices argument which is registered.
The error message says that the LoginPageViewModel itself is unknown to the IoC - it means the type LoginPageViewModel is not directly registered and not found through dynamic registrations or unknown service resolvers.
I am not a user of the Xamarin Prism so I am not sure about its mechanism for registering the view models.
Btw, this part of error
Resolution root AppetiteApp.ViewModels.LoginPageViewModel
with passed arguments [value(Prism.Navigation.ErrorReportingNavigationService)]
basically means the view-model was resolved via the foollowing call resolver.Resolve(typeof(LoginPageViewModel), args: new[] { errorReportingNavigationService })
Hope it will help you or someone knowledgeable in Xamarin to track the error cause.
So Once I investigated Inside of ICoreServices I commented out each of the dependencies then discovered that IUserSession was the once causing problems then I dug into that and discovered that the dependences for IAppInfo and IVersionTracking were missing in the App.Xaml.cs registr tyepes so I added that and then it worked!
containerRegistry.Register<IAppInfo, AppInfoImplementation>();
containerRegistry.Register<IVersionTracking, VersionTrackingImplementation>();

when exit and enter the app again using a MasterDetailPage, it crashes after a certain amount of times

The first page of my app is a Contentpage where you login or MasterDetailPage if you are already logged in. Even when the app runs fine, if I exit tha app and enter a certain amount of times (sometimes is 2, sometimes even 10), the app crashes and I get the next error message. Java.Lang.IllegalArgumentException: and this appears in crashlytics:
Shutting down VM
FATAL EXCEPTION: main
Fatal Exception: > java.lang.IllegalArgumentException: No view found for id 0x249 (unknown) for fragment FragmentContainer{bbc6fff #0 id=0x249}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1413)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
This only happens when I am in the masterdetailpage, in the login page functions very good
The error was that I make a transition between 2 pages in the code too fast, so one of them didn't finish loading and the other was starting. That was the source of my error. Now that I fixed it I can see it in my emulator android 8.1, but have problems with 7.0 if I know why I will put it in the post.
EDIT:
The error in 7.1 is only when I am trying to installed it again in my phone with debug. The error is the next:
Android.View.WindowManager BadTokenException: Unable to add window token android.os.BinderProxy is not valid; is your activity running?
if you use the app normally after the installation, it works good!

NativeScript error - java.lang.OutOfMemoryError

I'm creating a NativeScript app and testing it on an Android emulator. Unfortunately I can't replicate this, but occasionally I get the error below.
The app follows simple "parent-child" template. Going from the Parent to a Child; back to the Parent and then to a different Child - and repeating multiple times. Each Child displays text and an image.
I'm not sure if this is unique to Android (vs iOS) or an emulator (vs physical device).
An uncaught Exception occurred on "main" thread.
java.lang.OutOfMemoryError: Failed to allocate a 18925068 byte allocation with 11023464 free bytes and 10MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:831)
at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
at android.graphics.Bitmap.createBitmap(Bitmap.java:739)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615)
at org.nativescript.widgets.image.Fetcher.scaleAndRotateBitmap(Fetcher.java:499)
at org.nativescript.widgets.image.Fetcher.decodeSampledBitmapFromFile(Fetcher.java:474)
at org.nativescript.widgets.image.Fetcher.processBitmap(Fetcher.java:263)
at org.nativescript.widgets.image.Worker.loadImage(Worker.java:119)
at org.nativescript.widgets.ImageView.loadImage(ImageView.java:197)
at org.nativescript.widgets.ImageView.onAttachedToWindow(ImageView.java:58)
at android.view.View.dispatchAttachedToWindow(View.java:14514)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewGroup.addViewInner(ViewGroup.java:4348)
at android.view.ViewGroup.addView(ViewGroup.java:4145)
at android.view.ViewGroup.addView(ViewGroup.java:4086)
at android.view.ViewGroup.addView(ViewGroup.java:4059)
at android.view.ViewOverlay$OverlayViewGroup.add(ViewOverlay.java:190)
at android.view.ViewGroupOverlay.add(ViewGroupOverlay.java:63)
at android.transition.Visibility.onDisappear(Visibility.java:423)
at android.transition.Visibility.createAnimator(Visibility.java:247)
at android.transition.Transition.createAnimators(Transition.java:732)
at android.transition.TransitionSet.createAnimators(TransitionSet.java:405)
at android.transition.Transition.playTransition(Transition.java:1777)
at android.transition.TransitionManager$MultiListener.onPreDraw(TransitionManager.java:306)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Use decodedWidth / decodedHeight on Image component. Also set the largeHeap property on your application tag of your AndroidManifest.xml
<application
android:largeHeap="true" ....

Resources