首页 > 解决方案 > Wear OS 2 上的 Nativescript

问题描述

我在 Wear OS2 上使用 NativeScript。

我刚刚使用了 NS HelloWorld(打字稿非框架)并添加了 Firebase 插件以从通知中获取简单文本并将其显示在文本框中。

一切正常,除非应用程序从后台恢复,在这种情况下 UI 不会呈现文本更改。

调试时,我看到通知有效负载出现在我的打字稿页面实例中,没有错误……但是 UI 没有呈现它。

如果我在手机上运行相同的应用程序,所有工作都按预期工作,也会从后台恢复。相同的代码,没有一行差异。

我想知道 NativeScript 是否支持 Wear OS...

对此有什么想法吗?

谢谢,法比奥。


一些代码...

//主页面.ts

export function navigatingTo(args: EventData) {
     //here Firebase is already init, all right...
    (<Page>args.object).bindingContext = (new ViewModel).init();
}

//视图模型.ts

import { Observable } from "tns-core-modules/data/observable";
import { NotificationService } from "./notification-service";

export class ViewModel extends Observable {

    private _message: string = "Waiting message...";

    init(): ViewModel {
        //this callback is always invoked as expected
        //also when the app is killed and comes back to life
        NotificationService.onMessage = (firebaseMessage: any) => {
            const text = firebaseMessage['text'] || firebaseMessage['data']['text'];

            console.log('text' + text);
            this.message = text; //that's okay, see setter below
        }

        return this;
    }

    get message(): string {
        return this._message;
    }

    set message(value: string) {
        if (this._message !== value) {
            this._message = value;

            //to this point the UI should update... BUT
            //it works only IF the app is in foreground
            //AND was never killed/backgrounded before.
            //(bug on wear-os only)
            this.notifyPropertyChange("message", value);
        }
    }
}

//主页面.xml

<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">

    <StackLayout class="p-20">
        <Label text="{{ message }}" class="h2 text-center" textWrap="true" />
    </StackLayout>
</Page>

标签: firebasepush-notificationnativescriptrenderingwear-os

解决方案


推荐阅读