首页 > 解决方案 > 从 nativescript-plugin-firebase 通知消息打开应用程序时,我的应用程序在 Android 后退按钮点击时崩溃

问题描述

我正在开发一个接收 [nativescript-plugin-firebase] 通知的 nativescript-angular 应用程序。

该项目是最新的。package.json 包含以下几行:
"tns-android": {
"version": "5.4.0"
},
"tns-core-modules": "~5.4.0",
"@angular/core": " ~8.0.0"
"nativescript-plugin-firebase": "^9.0.1"

通知很好,但在以下情况下点击通知消息时应用程序崩溃:

我在 Android Emulator 和具有 Android 7、8 和 9 的设备上遇到了同样的错误。

屏幕错误在这里

这是 src\app\app.component.ts 中的通知代码:

ngOnInit(): void {

    /// *** Firebase Cloud Messaging ***
    firebase.addOnMessageReceivedCallback(
        (message) => {
            const that = this;  
            let contentId: string = "";

            if (message.foreground) {
                /** If the app is already open, show a dialog message. **/
                confirm({
                    title: message.title,
                    message: message.body,
                    okButtonText: "Open",
                    cancelButtonText: "Cancel"
                }).then(function (result) {
                    // result argument is boolean
                    if (result) {
                        if (message.data.contentId) {
                            contentId = message.data.contentId;

                            if (parseInt(contentId) > 0) {
                                that.routerExtensions.navigate(["/car-detail/" + contentId], { clearHistory: false });
                            }
                        }
                    }
                    // console.log("Dialog result: " + result);
                });
            }
            else {
                /** Else if the message arrived when the app is in the background, this code is executed when the user taps on the notification. **/
                if (message.data.contentId) {
                    contentId = message.data.contentId;

                    if (parseInt(contentId) > 0) {
                        this.routerExtensions.navigate(["/car-detail/" + contentId], { clearHistory: false });
                    }
                }
            }
        }
    )

    if (this.authenticationService.FirebaseToken == undefined || this.authenticationService.FirebaseToken == null) {
        firebase.init({
            // Optionally pass in properties for database, authentication and cloud messaging, see their respective docs.
            onPushTokenReceivedCallback: function (token: any) {
                console.log("Firebase push token: " + token);
            }
            , showNotificationsWhenInForeground: true
        }).then(
            () => {
                console.log("firebase.init done");
            },
            error => {
                console.log(`firebase.init error: ${error}`);
            }
        );

        firebase.getCurrentPushToken().then((token: string) => {
            // may be null if not known yet
            console.log(`Current push token: ${token}`);
            this.authenticationService.FirebaseToken = token;
        });
    }

    // *** / Firebase Cloud Messaging ***
}  

标签: nativescriptnativescript-angularnativescript-plugin

解决方案


推荐阅读