nativescript - 从 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"
通知很好,但在以下情况下点击通知消息时应用程序崩溃:
- 应用程序已打开(在前台)
- 设备已关闭
- 然后点击锁屏通知消息
- 应用程序崩溃
并显示:
An uncaught Exception occurred on 'main' thread. java.lang.RuntimeException: Unable to destroy activity
我在 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 ***
}
解决方案
推荐阅读
- javascript - 在 iOS 设备上输出 JavaScript 事件内容
- git - Git:切换分支
- zabbix - Zabbix:文本文件的最后一行
- javascript - 如何在用户单击单选按钮之前更新 useState 变量?
- pandas - 数据框单元格中的求和列表
- java - 方法返回值在 Eclipse 调试器中不可见
- regex - 正则表达式选择所有在这个或那个之前
- javascript - 在 React 和 material-ui 中制作 popper 或 div 高度“100%”
- python-2.7 - 带有 lxml 构建器的 SSS 版本
- vue.js - 使用 BaseDropdown 组件时如何向父组件发出下拉值