0

My app have a background service (Android) which look periodicly for new data. If there are new data a notification is started. By tapping the notification a little native code piece is running and this will bring back to front the Nativescript app.

Snippet from the Kotlin class to bring back the Nativescript app:

val launchIntent: Intent? = packageManager.getLaunchIntentForPackage(applicationContext.packageName)
launchIntent?.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_SINGLE_TOP)
launchIntent?.setAction(ACTION_NOTIFICATION_AVAILABLE)
startActivity(launchIntent)

In the AppComponent class my listener for a AndroidApplication.activityResumedEvent will listen:

export class AppComponent implements OnInit {
    constructor(
        private router: Router,
        private ngZone: NgZone,
    ) {}

    ngOnInit(): void {
        android.on(AndroidApplication.activityResumedEvent, (args) => this.ngZone.run(() => {
            let intent = args.activity.getIntent();

            if (intent.getAction() === com.sample.app.NotificationTapReceiver.ACTION_NOTIFICATION_AVAILABLE) {
                this.router.navigate(['/notificationList']);
            }
        }));
    }
}

Most of the times this code works as aspected. But in my opinion we have three diffrent situations:

  • App is in foreground and running when a notification is incomming and the use tap on it. => all works fine
  • The app is not running only the background service. If the user tap the notification the app will fully start and move to the notification list. => all works fine
  • Problem: The App is running but not in the foreground. By tapping the notification the app resume (without restart) to the front and navigate to the notification list BUT the user interface are freezed. By tapping the elements on the screen, the inside code are running (consoloe.log(...)) but no action on the UI. Maybee the UI are not freezed only the routing/navigation don't work.

I find the question Nativescript - How to bring app to foreground when notification is pressed but in my case there are no difference between android emulator and (not connected) real android device.

Tests with the flags to start the activity will change the behavior but not in the right way

# FLAG_ACTIVITY_SINGLE_TOP might be the best solution but the described error above is very annoying
launchIntent?.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_SINGLE_TOP)

# even the app is in foreground after a tap on the notification the app is fully restarts
launchIntent?.addFlags(FLAG_ACTIVITY_NEW_TASK)

Have anyone an idea where the problem is or which flags i have to set to prevent the current state and the fully working UI (with router/navigation)? Or do i have to modify the nativescript/angular code the better handle the resume event?

Not the best solution but when i was able to trim the flags to get the in the foreground running app still running and in the other two states (app dead or app in the background) a fully restart is initiated, at the moment this will be fine too.

Update: It look like, that the UI not freezed. I added a RadListView and i can scroll to the list, still the routing/navigate don't work. Even i use the this.routerExtensions.back() nor i use the this.router.navigate(['notificationDetail']).

TeHa
  • 171
  • 8
  • Can you setup a sample Github repo? – Manoj Apr 28 '20 at 10:26
  • @Manoj Oh, i will try but it will take a little bit of time. At the moment i think, it is a problem with the routing/navigate because the flags interact with the histroy. For your information look at my short update. – TeHa Apr 28 '20 at 12:51

0 Answers0