当活动处于后台或不在后台时,Firebase 通知不起作用

2024-04-17

我的编码与 firebase 示例代码提供的相同。当 Activity 处于前台状态或打开时,它可以正常工作。但是,当活动关闭或处于后台状态时,它就无法正常工作。问题是这样的...

  1. 不显示应用程序的应用程序图标。

  2. 默认情况下以应用程序名称作为通知标题

当应用程序处于后台状态或未在设备上打开时,会发生时间通知,LogCat 将显示以下详细信息:

09-16 15:55:56.056 12113-12113/com.testdemofb D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
09-16 15:55:56.089 12113-12113/com.testdemofb D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
09-16 15:55:56.176 12113-12113/com.testdemofb I/FA: App measurement is starting up, version: 9452
09-16 15:55:56.177 12113-12113/com.testdemofb I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
09-16 15:55:56.286 12113-12113/com.testdemofb I/FirebaseInitProvider: FirebaseApp initialization successful
09-16 15:55:56.518 12113-12147/com.testdemofb I/FA: Tag Manager is not found and thus will not be used

如果有任何解决方案,请帮助我

谢谢...

MyFirebaseMessagingService.java

package com.testdemofb;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        sendNotification(remoteMessage.getData().get("title"));
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        Log.d(TAG, "Message Notification BodyLocalize: " + remoteMessage.getNotification().getBodyLocalizationKey());
        Log.d(TAG, "Message Notification Title: " + remoteMessage.getNotification().getTitle());
        Log.d(TAG, "Message Notification TitleLocalize: " + remoteMessage.getNotification().getTitleLocalizationKey());
        sendNotification(remoteMessage.getNotification().getBody());
    }


}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(bm)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Title")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

}
}

AndroidManifest.xml 文件像这样放在之间应用 tag...

    <service android:name=".MyFirebaseMessagingService"
        android:process="remote">
        <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>

FCM 使用两种类型的消息,即。通知和数据。了解更多相关信息here https://firebase.google.com/docs/cloud-messaging/concept-options。我认为您正在发送通知。您需要发送数据消息。

像这样的东西。

{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
 "Nick" : "Mario",
 "body" : "great match!",
 "Room" : "PortugalVSDenmark"
},
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当活动处于后台或不在后台时,Firebase 通知不起作用 的相关文章

  • Android自定义控件命名空间问题

    我一直在为 Android 开发自定义控件 尽管我尝试按照建议进行操作here https stackoverflow com questions 4495511 how to pass custom component parameter
  • Google Play 商店中基于服务的 Android 应用程序

    我正在开发一个应用程序 该应用程序仅包含一些服务 没有任何活动 即没有 UI 基本上 当用户在他 她的设备上安装应用程序时 我希望有 2 到 3 个服务在后台运行 对此我有几个疑问 应用程序安装后我的服务将如何启动 我的BroadcastR
  • Android向后兼容技术

    我现在在开发基于最新 API 15 ICS 的 15 项活动 Android 应用程序方面取得了进展 现在我发现应用程序的主要功能主义者即使支持 android v4 也不向后兼容 例如 1 fragment事务动画 2 将StringSe
  • ReferenceError:找不到变量:需要

    我在加载时遇到问题node modules到我的网页之一 我已经安装了 npm node js 并且我想使用require 函数在我的网站上初始化 Firebase 我不知道为什么 但它抛出引用错误 ReferenceError 找不到变量
  • Google App Engine - 节点:找不到模块“firebase-admin”

    第一次在这里部署 GAE 应用程序 我尝试遵循本教程 https firebase googleblog com 2016 08 sending notifications Between android html https fireba
  • Android 5.0 Lollipop 中屏幕固定关闭时如何收到通知?

    我有一个在后台运行的应用程序 并在手机上发生特定事件时启动活动 我发现在 Android 5 0 中 当用户使用另一个应用程序打开屏幕固定时 startActivity intent 调用将被完全忽略 我的应用程序不知道该活动尚未启动 因此
  • 游标索引越界异常

    打开后出现光标索引越界错误 数据库 请任何人告诉我如何打开现有数据库 sqllite Android 我想在数据库上触发一个选择查询 检索一些信息 public void getPatient SQLiteDatabase db Strin
  • FLAG_ACTIVITY_REORDER_TO_FRONT 被忽略

    我有一个包含项目列表的 FragmentActivity 当应用程序处于后台时 可以推送该项目列表 发生这种情况时 我想创建一个状态栏通知并提醒用户更新 当用户单击通知时 活动应重新排序到前面并显示在屏幕上 同时在列表底部显示新项目 所以我
  • Android 8.1 中 Activity 自行旋转并恢复正常

    我的应用程序在所有 Android 版本上运行良好 但我注意到在 Android 8 1 0 Oreo 中 当我将屏幕从纵向活动转到横向活动时 以及当我按后退按钮时 它会显示异常行为 屏幕自动从横向旋转并恢复正常 看起来 Activity
  • android:进程和进程名称

    我试图理解android process属性 Ref says http developer android com guide topics manifest application element html proc 如果分配给该属性的
  • 更改 Android 中的媒体音量?

    我可以更改媒体音量吗 如何 到目前为止我用过这个 setVolumeControlStream AudioManager STREAM MUSIC 但有一个搜索栏并且想要更改媒体音量 而不是铃声音量 那么有人可以告诉我如何更改媒体音量onC
  • Android:BATTERY_STATUS_DISCHARGING 和 BATTERY_STATUS_NOT_CHARGING 之间的区别

    我想知道这两个标志之间的区别 BatteryManager BATTERY STATUS DISCHARGING And BatteryManager BATTERY STATUS NOT CHARGING 我开发了一个使用这两个标志的应用
  • 如何知道用户是否在 Android 应用程序中输入了错误的密码(锁定屏幕)

    我正在开发一个 Android 应用程序 如果用户在 Android 锁定屏幕中输入错误的密码 则必须完成其中一项活动 例如 如果用户输入错误的密码 则会发送电子邮件 我将不胜感激任何帮助 提前致谢 Kshitij 锁屏在完全沙箱环境中运行
  • 如何在Android网格视图中设置单元格大小?

    我正在尝试为应用程序制作一个带有大图标的网格视图 但我找不到任何有关修改 Android 上网格布局上的单元格大小的教程 有人可以给我一个例子或相关链接吗 Thanks 就像另一个一样适配器视图 http developer android
  • 使用 UPI url 调用 PSP 应用程序

    我正在尝试创建一个商家应用程序 它将根据 NPCI 的指南生成一个 url 此 url 将作为意图共享 并且 PSP 应用程序 任何注册的银行应用程序 应该能够侦听该 url 并被调用 我已经形成了这样的网址 upi pay pa icic
  • 按名称获取 ArrayList

    这是正确的获取方式吗ArrayList
  • 如何通过 AppCompatActivity 使用 YouTube Android 播放器 API

    为了在我的应用程序中播放视频 我决定扩展 YouTube Android Player API 但问题是我的菜单消失了 因为我没有从 AppCompatActivity 扩展 问题是 如何使用 YouTube Android Player
  • 使用 RecyclerView.Adapter 在 onBindViewHolder() 内设置 onItemClickListener

    我有一个自定义对象 学生班 public class Student private String name private String age public String getName return name public void
  • 如何修复超出最大调用堆栈大小

    有一个 MERN Firebase 应用程序并收到此错误和一堆 atdeepExtend deepCopy ts 71 RangeError Maximum call stack size exceeded getApps as apps
  • 在android中使用BaseActivity的不同活动中的通用标头

    我想编写一次代码并在不同的活动中使用 我创建了一个Base Activity class为了那个原因 此外 不同活动中所有布局的标题都是相同的 我在以下人员的帮助下做到了这一点

随机推荐

  • 从 data.frame 中的现有变量创建几个新的派生变量

    在 R 中 我有一个 data frame 其中有几个变量 这些变量是多年来每月测量的 我想得出每个变量的月平均值 使用所有年份 理想情况下 这些新变量将全部放在一个新的 data frame 中 继承 ID 下面我只是将新变量添加到 da
  • MultipartEntityBuilder 和 setCharset for UTF-8 发送空内容

    我需要将 unicode 字符提交到表单 以将我的应用程序本地化到使用非拉丁字母的国家 地区 关于新的 MultiPartEntityBuiler 的文档很少 我只找到了另一篇建议使用 setCharset 的帖子 如果我不使用 Entit
  • 从 mongoose 'toJSON' 支持中删除一个属性

    我正在使用 mongoose 的 toJSON 支持 如下所示 userSchema set toJSON getters true virtuals true minimize false 现在 在猫鼬对象的 toJSON 方法调用返回的
  • 使用 Office.js 在桌面 Excel 上呈现数据,但在 Chrome Office 365 上,它会给出错误“处理请求时出错”。

    我们正在使用办公js在 Excel 上呈现数据的库 有超过2000行效果很好桌面Excel 但当同样API用于Chrome 办公室 365它给出错误消息 有一个错误处理请求 请参阅随附的屏幕截图 随着数据变大 Chrome Office E
  • 如何获取hive中的数据库用户名和密码

    正在编写jdbc程序来连接hive数据库 我希望在连接 url 中提供用户名和密码 我不知道如何使用 hive QL 获取用户名和密码 有人可以帮我吗 Exception in thread main java sql SQLNonTran
  • 如何向 CPBarPlot 条形图添加标签?

    我对 Core Plot 完全陌生 并且有一个可用的条形图 但视觉效果对我来说有点无用 除非我知道每个条形中代表哪个对象 我看到有一个名为 fieldIdentifiers 的方法 但不知道如何实现它 也找不到任何文档 如果这甚至是正确的方
  • 禁用 ActionBar RTL 方向

    Android 4 2 引入了 RTL BiDi 支持 要开始使用它 我只需按照说明进行操作 清单文件中元素的 android supportsRtl 属性并将其设置为 true 但随后我的 ActionBar 徽标也将方向更改为右侧 徽标
  • 未使用 QueryString id 参数

    我有一个非常基本的 ASP Net MVC 项目 我想在我的控制器操作之一上使用 id 参数名称 从我读过的所有内容来看 这应该不是问题 但由于某种原因 使用 id 参数名称无法获取从查询字符串中提取的值 但如果我将其更改为任何其他不同的名
  • 获取第n行文本输出

    我有一个每次生成两行作为输出的脚本 我真的只对第二行感兴趣 此外 我只对第二行一对 之间出现的文本感兴趣 此外 在散列之间还使用另一个分隔符 A 如果我还可以分解以 A 分隔的文本的每个部分 那就太好了 请注意 A 是 SOH 特殊字符 可
  • PowerShell,用另一种文化格式化值

    PowerShell 中有没有一种简单的方法可以在另一个语言环境中格式化数字等 我目前正在编写一些函数来简化我的 SVG 生成和 SVG 使用 作为小数点分隔符 而 PowerShell 遵循我的区域设置 de DE 将浮点数转换为字符串时
  • 检索 Windows 10 电源模式滑块的状态

    是否有任何 API 可以检索 Windows 10 电源模式滑块的状态 我在看Windows System Power名称空间 https learn microsoft com en us uwp api windows system p
  • GitHub Actions 工作流程错误:运行命令超时!即使剧本做得很好

    每次提交到存储库时 我想在远程服务器上部署 Nuxt js 应用程序 这是我的deploy yml name Deployment on push branches master jobs deploy name Deploy runs o
  • 分配多个 JsonProperty?

    我正在尝试创建一个包含来自 Facebook 和 Twitter 的信息的单个数据类 但在我来自 Twitter 的 JSON 回复中我需要id str我从 Facebook 得到id 我需要将这两个放入id 细绳 现在我知道我可以使用 J
  • WooCommerce:如何在价格和促销价格之前添加文本?

    我已成功地让文本显示在价格之前和促销价格之前 但文本被视为价格的一部分 而不是与其分开 Placing ins before content Betty s price color 000 font size 14px 在我的自定义 CSS
  • Flutter 嵌套行 MainAxisAlignment

    我想做这个 但这是我实际得到的 这是我的代码 Row itemTransaction BuildContext context Transaction transaction This is the function that will b
  • 如何配置 android 测试目录?

    Android gradle 构建强制我的 AndroidTests 位于 src androidTests 中 如何将其更改为我选择的另一个目录 这是一些背景 我正在从 eclipse 迁移一个项目 根据构建文档 当我将其添加到我的 gr
  • 重新启动 Vaadin 应用程序时抛出 java.io.NotSerializedException

    当我重新启动 Tomcat 时 出现以下错误 2014 10 01 15 49 47 055 WARN com vaadin event ListenerMethod localhost startStop 2 Error in seria
  • 如何判断一个对象是否是 Mockito 模拟对象?

    是否可以在代码中判断给定对象是否是 Mockito 模拟对象 我想这样做的原因是在使用模拟时返回不同的错误消息 这将用于向其他开发人员建议他们应该使用预先准备的模拟 该模拟已经设置为以有用的方式应答呼叫 而不是自己创建模拟 目前我拥有的最好
  • 《How To Make a Tile-Based Game with Cocos2D 2.X》用cocos2d V3制作本教程

    我有一个小问题 在本教程中如何使用 Cocos2D 2 X 制作基于图块的游戏 http www raywenderlich com 29458 how to make a tile based game with cocos2d 2 x使
  • 当活动处于后台或不在后台时,Firebase 通知不起作用

    我的编码与 firebase 示例代码提供的相同 当 Activity 处于前台状态或打开时 它可以正常工作 但是 当活动关闭或处于后台状态时 它就无法正常工作 问题是这样的 不显示应用程序的应用程序图标 默认情况下以应用程序名称作为通知标