- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Firestore 制作消息传递应用程序,我想在发送新消息时发送通知。为此,我正在使用 FCM。
现在我声明了 2 个有效载荷:一个通知有效载荷和一个数据有效载荷。当用户发送一条消息时,如果应用程序在后台,收件人会收到 2 条通知,如果应用程序在前台,则收到一条通知。如果我删除通知负载,无论应用程序是在前台还是后台,收件人都会收到一条通知。
我想要发生的是,当应用程序处于后台或前台时,用户只会收到一个通知,而如果应用程序处于与发件人的聊天 Activity 中,则不会收到任何通知。我的理解是,当我同时声明通知和数据有效负载时,这应该是默认行为。
Handle notification messages in a backgrounded app
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.
This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
有人可以帮忙吗?我读过this和 this但它似乎对我不起作用所以也许还有其他我不知道的东西这是我的 list 条目
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
这是我的云函数,包含通知和数据负载
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.notifyNewMessage =
functions.firestore
.document('users/{ID}/contacts/{contactID}/messages/{messageID}')
.onCreate((docSnapshot , context) => {
const message = docSnapshot.data()
const recipientId = message['recipientId']
const senderId = message['sender']
const senderName = message['senderName']
return admin.firestore().doc('users/' + recipientId).get().then(userDoc =>{
const registrationTokens = userDoc.get('registeredToken')
const notificationBody = (message['data_type'] === "TEXT") ?
message['message'] : "You received a new image message."
const payload = {
notification : {
title : senderName + " sent you a message.",
body : notificationBody
},
data : {
TITLE : senderName + " sent you a message.",
BODY : notificationBody,
USER_NAME : senderName,
USER_NUMBER : message['sender'],
CLICK_ACTION : "MessageListActivity",
MESSAGE : message['message'],
}
}
return
admin.messaging().sendToDevice(registrationTokens,payload).then(response
=> {
})
})
})
这是我的消息服务,我现在发送非常基本的通知
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final int NOTIFICATION_MAX_CHARACTERS = 30;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Map<String, String> data = remoteMessage.getData();
if (data.size() > 0) {
sendNotification(data);
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
}
}
private void sendNotification(Map<String, String> data) {
String author = data.get("USER_NAME");
String title = data.get("TITLE");
String body = data.get("BODY");
String id = data.get("USER_NUMBER");
String message = data.get("MESSAGE");
Intent intent = new Intent(this, MessageListActivity.class);
intent.putExtra(Constants.USER_NAME, author);
intent.putExtra(Constants.USER_NUMBER, id);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Create the pending intent to launch the activity
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
if (message.length() > NOTIFICATION_MAX_CHARACTERS) {
message = message.substring(0, NOTIFICATION_MAX_CHARACTERS) +
"\u2026";
}
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_email_black_24dp)
.setContentTitle(String.format(getString(R.string.notification_message),
author))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */,
notificationBuilder.build());
}
}
最佳答案
如果你想避免显示通知,你应该在你的 FirebaseMessagingService
上控制它.您的情况看起来是您的应用程序在特定 Activity 中打开。
如果你想在应用程序不在前台时避免通知,那么你应该从服务中检查 ActivityManager。您可以使用此方法:
public boolean isForeground(String myPackage) {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
return componentInfo.getPackageName().equals(myPackage);
}
同时添加 <uses-permission android:name="android.permission. REAL_GET_TASKS" />
到你的 list 。
以防您只想在特定 Activity 中避免使用它。然后你需要找出一种方法来跟踪该 Activity 的生命周期。使用 SharedPreferences
, 一个 static
变量,你可以在这个 post 中看到多个例子.
关于android - 负载和通知如何在 Firestore 消息传递中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50624486/
在 Web 应用程序架构设计期间,我必须从概念上计算我的服务器之一可以服务多少个当前客户端。然后我可以预算它。 那么,有什么公式可以遵循吗?或者,你如何计算这个?或者,通常,一个 httpd/tomc
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是
我正在使用 Angular 5,我正在尝试在加载 div 的背景图像时获取加载图标。 如果它是一个普通的 img,我对此没有问题,但如果我尝试将它作为背景,它就不起作用。 这里是一些示例代码 app.
我们怎么知道我们的程序在 CPU 上有多少负载? 我尝试使用 htop 找到它。但是 htop 不会给 cpu 负载。它实际上给出了我程序的 cpu 利用率(使用 pid)。 我正在使用 C 编程,L
我们发现从Spark 1.3到当前的Spark 2.0.1以来,从Oracle数据库使用Spark的API加载数据一直很慢。典型的代码在Java中是这样的: Map options =
我有时会收到 mnesia overloaded主要使用时的错误消息 async_dirty查询和 ram_copies表。所以为了了解发生了什么,我想获得更多关于 mnesia 状态的信息,例如每秒
对于通常使用很少 CPU 的程序来说,内核 CPU 非常高。 Linux 机器在状态之间交替。大多数时候,程序使用低 CPU 正常执行。在 CPU“激增”期间,程序使用 100% 可用 CPU 使用高
我正在使用 Raspberry Pi 2 来路由 wifi-eth 连接。因此,从 eth 方面来看,我有一台可以使用 Pi wifi 连接到互联网的计算机。在 Raspberry 上我启动 htop
基本上我有一个网页,其中有一个 iframe 可以从不同的域加载另一个网页。它移动得很慢,我想证明整个页面很慢只是因为 iframe 内的页面。 有什么方法可以测量总页面负载以及总页面负载中有多少%来
我们有一个基于 Spring 的应用程序,它充当使用其他 Rest API 的编排层。我只想测试这个组件的性能,而不测试正在使用的下游 api。 我正在寻找有关如何完成此操作的任何架构建议? 当前的方
我正在学习 hibernate 。为了进行测试,我使用无效 key 调用了 session.load 。当我在调试器(JB Idea)中跨过该行后,没有任何反应 - 我预计会得到 ObjectNotF
我正在开发一个小型的待办事项 PHP 应用程序。我正在使用 jQuery 构建 HTML。其中一个是一个按钮,用于启动一个模式,允许用户编辑该项目。我很好奇加载数据时更好的方法是什么: 1) 在初始加
我尝试在 twitch 播放器中使用 angular 作为覆盖标记。 我将 ng-repear 与(键,值)结合使用。 //player is here 设置是一个全局对象。但是当我尝试加载页面
我即将了解 C 语言中的特定进程如何在特定时间范围内加载 CPU。该进程可能会在运行时切换处理器核心,因此我也需要处理这个问题。 CPU为ARM处理器。 我研究了从标准顶部获取负载的不同方法,perf
这个问题在这里已经有了答案: XMLHttpRequest Origin null is not allowed Access-Control-Allow-Origin for file:/// t
您好,我正在用 Java 开发负载平衡算法。在我的系统中将有一个主节点和 n 个从节点。主节点将接收查询分发给它的从节点。但是在将查询分发到其从节点之一之前,我想测量从节点中的当前负载,以检查特定从节
我正在渲染由大约 50 万个三角形组成的相当重的对象。我使用 opengl 显示列表,在渲染方法中只调用 glCallList。我认为一旦图形基元被编译成显示列表,cpu 的工作就完成了,它只是告诉
我正在尝试加密 Sipdroid,为此我必须在 RTP 数据包获得编码的音频负载后对其进行加密。我在 RTP 数据包类中使用这个函数: public byte[] getPayload() {
我正在尝试解析以下 JSON 负载: { "results":[ [ 298.648132, 280.68692, 356.54
在动画期间 cpu 负载非常高(高达 75%) 是否有优化代码以降低 CPU 负载的方法? 我的代码: ImageView myImageView = (ImageView)findViewById(
我是一名优秀的程序员,十分优秀!