android - 应用程序运行 android 时未收到 Cloud Firestore 通知
问题描述
我有一个向用户发送通知的云 Firestore 功能。
[安卓]
它工作得很好,但只有当应用程序处于后台(暂停或终止)时我才会收到通知。
这是我的功能的简化版本:
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) =>
{
const receiver_user_id = context.params.receiver_user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to :' , receiver_user_id);
if (!data.after.val())
{
console.log('A notification has been deleted :' , notification_id);
return null;
}
const DeviceToken = admin.database().ref(`/Users/${receiver_user_id}/device_token`).once('value');
return DeviceToken.then(result =>
{
const token_id = result.val();
const payload =
{
notification:
{
title: "New Chat Request",
body: `You have a new chat Request`,
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response =>
{
console.log('This was a notification feature.');
});
});
});
我没有任何特定的 java 代码,因为我现在只需要显示通知。有没有人有想法以简单的方式归档这个问题?
解决方案
FCM 通知在您的应用程序处于后台时由系统处理,并显示在 Android 的系统托盘中。如果您想在应用程序处于活动状态时处理通知,则必须通过覆盖onMessageReceived
来编写代码。
另见:
推荐阅读
- regex - 如何将正则表达式转换为在 Bash 脚本中工作?
- json - 我可以根据列表中的值输出布尔值吗?
- php - HTTP 413:请求太大
- bash - Bash文件不按ubuntu中的目录运行
- node.js - 运行nodemon时在一个依赖的commonjs下运行时选择commonjs文件
- docker - 容器突然被杀死并警告“在杀死垫片后清理”
- javascript - MQTT.js(mosqutio 代理) - 我多次收到相同的消息
- python - AttributeError:模块“dlib”没有属性“shape_predictor”
- macos - 你能解释为什么错误“权限被拒绝”是鱼卸载的常见问题吗?
- python - 在子目录中运行脚本时从上一级目录导入模块