从nativescript-plugin-firebase通知消息打开应用后,我的应用在Android后退按钮点击时崩溃了

时间:2019-06-26 15:59:19

标签: nativescript nativescript-angular nativescript-plugin

我正在开发一个接收[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”

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

  • 该应用已打开(在前台)
  • 设备已关闭
  • 然后点击锁定屏幕通知消息
  • 应用程序崩溃
  • 并显示:

    An uncaught Exception occurred on 'main' thread.
    java.lang.RuntimeException: Unable to destroy activity
    

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

Screen error here

这是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 ***
}  

0 个答案:

没有答案