Android Firebase (FCM)推送接入
官方文档:
向后台应用发送测试消息 | Firebase Cloud Messaging
1、根级(项目级)Gradlegradle的dependencies中添加:
dependencies {...// Add the dependency for the Google services Gradle pluginclasspath 'com.google.gms:google-services:4.3.10'}
2、模块(应用级)Gradle 中添加 Google 服务插件:
plugins {id 'com.android.application'// Add the Google services Gradle pluginid 'com.google.gms.google-services'...
}
或者
apply {plugin 'com.android.application'// Add the Google services Gradle pluginplugin "com.google.gms.google-services"
}
添加 Firebase Cloud Messaging Android 库的依赖项:(按官方的应该也是可以的)
implementation(platform("com.google.firebase:firebase-bom:32.3.1"))implementation("com.google.firebase:firebase-analytics-ktx")
// implementation("com.google.firebase:firebase-auth-ktx")implementation("com.google.firebase:firebase-firestore-ktx")implementation("com.google.firebase:firebase-messaging")// Firebase Cloud Messaging (Kotlin)implementation("com.google.firebase:firebase-messaging-ktx")
3、消息接收
AndroidManifest.xml中添加,接受类
<serviceandroid:name=".BillionFirebaseMessagingService"android:exported="true"><intent-filter><action android:name="com.google.firebase.MESSAGING_EVENT" /></intent-filter></service>
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;import com.game.base.sdk.BaseSDK;
import com.game.base.sdk.Events;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.Gson;import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;import java.util.Locale;
import java.util.Map;
import java.util.jar.JarException;
import java.lang.Boolean;import game.billion.block.blast.bbb.MainActivity;
import game.billion.block.blast.bbb.R;
import kotlin.Pair;public class BillionFirebaseMessagingService extends FirebaseMessagingService {private String channelID = "game.puzzle.block.billion.pro.notification";private String channelName = "messageName";int notifyID = 101010;//监控令牌的生成@Overridepublic void onNewToken(@NonNull String token) {super.onNewToken(token);Log.d("BillionFirebaseMessagingService", "onNewToken:"+token);}//监控推送的消息@Overridepublic void onMessageReceived(@NonNull RemoteMessage mge) {super.onMessageReceived(mge);Log.d("BillionFirebaseMessagingService", "From:"+mge.getFrom());Log.d("BillionFirebaseMessagingService", "data:"+mge.getData());Map<String,String> dt = mge.getData();String ti =dt.get("title"); //标题(多语言),JSON字符串try {if (ti != null && !ti.isEmpty()){JSONObject json =new JSONObject(ti);String language = Locale.getDefault().getLanguage();if (language=="pt"){//获取巴西语String resTitle = json.getString("pt");if (resTitle != null && !resTitle.isEmpty()){//显示通知addNotification(resTitle);}}else {String resTitle = json.getString("en");if (resTitle != null && !resTitle.isEmpty()){//显示通知addNotification(resTitle);}}}} catch (JSONException e) {}String mgeId = mge.getMessageId();String intent =dt.get("intent");String strId =dt.get("sid");Pair<String, String>[] params = new Pair[3];params[0] = new Pair<>("message_id", mgeId);params[1] = new Pair<>("type", intent);params[2] = new Pair<>("sid", strId);Events.push("100410", params);}//添加提示private void addNotification(String resTitle ){if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);NotificationChannel nc=newNotificationChannel(channelID, channelName);if (nc!=null){if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {managerCompat.createNotificationChannel(nc);}}Intent nfIntent = new Intent(this, MainActivity.class);PendingIntent pendingIntent ;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {pendingIntent=PendingIntent.getActivity(this, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE);} else {pendingIntent=PendingIntent.getActivity(this, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);}NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelID);builder.setContentIntent(pendingIntent); // 设置PendingIntentbuilder.setSmallIcon(R.drawable.notification); // 设置状态栏内的小图标builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);builder.setStyle(new NotificationCompat.BigTextStyle());builder.setContentTitle(getString(R.string.app_name));builder.setContentText(resTitle);builder.setWhen(System.currentTimeMillis());builder.setAutoCancel(true);managerCompat.notify(notifyID, builder.build());}}private NotificationChannel newNotificationChannel(String cId, String cName){if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel channel =new NotificationChannel(cId, channelName, NotificationManager.IMPORTANCE_HIGH);channel.setSound(null, null);channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);return channel;}return null;}
}
4、测试(google-services.json 记得放应用级中)
a、获取tokn
//在firebase中检查google版本是否有问题GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this).addOnCompleteListener(new OnCompleteListener<Void>() {@Overridepublic void onComplete(@NonNull Task<Void> task) {if (task.isSuccessful()) {Log.d("Firebase", "onComplete: Play services OKAY");//firebase 推送 (FCM)获取token(FCM令牌)FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {@Overridepublic void onComplete(@NonNull Task<String> task) {if (!task.isSuccessful()){Log.w("Firebase", "Fetching FCM registration token failed", task.getException());return;}// Get new FCM registration tokenString ss= task.getResult();Log.d("Firebase", "onComplete:token getResult "+ss);}});} else {// Show the user some UI explaining that the needed version// of Play services Could not be installed and the app can't run.}}});
b、用tokn就能对固定手机发送消息
相关文章:

Android Firebase (FCM)推送接入
官方文档: 向后台应用发送测试消息 | Firebase Cloud Messaging 1、根级(项目级)Gradlegradle的dependencies中添加: dependencies {...// Add the dependency for the Google services Gradle pluginclasspath com.google.gm…...

Neo4j恢复
主要记录windows环境下从备份文件中恢复Neo4j, Linux环境同理 备份在上一篇中有介绍,参考: Neo4j备份-CSDN博客 误删数据 为了模拟误删除场景,我们查询Person,并模拟误操作将其进行删除; match(p:Person) return …...
ZZULIOJ 1114: 逆序
题目描述 输入n(1<n<10)和n个整数,逆序输出这n个整数。 输入 输入n(1<n<10),然后输入n个整数。 输出 逆序输出这n个整数,每个整数占4列,右对齐。 样例输入 Copy …...

Linux前后端项目部署
目录 1.jdk&tomcat安装 配置并且测试jdk安装 修改tomcat 配置文件 登入tomcat 发布 安装mysql 导入sql数据 发布项目war包 redis安装 nginx安装 配置nginx域名映射 部署前端项目 centos 7的服务安装 安装jdk 安装tomcat 安装Mysql 安装redis 安装nginx 前后…...

GPT-4与DALL·E 3:跨界融合,开启绘画与文本的新纪元
在人工智能的发展浪潮中,MidTool(https://www.aimidtool.com/)的GPT-4与DALLE 3的集成代表了一个跨越式的进步。这一集成不仅仅是技术的结合,更是艺术与文字的完美融合,它为创意产业带来了革命性的变革。本文将探讨GPT…...
聊聊PowerJob的Alarmable
序 本文主要研究一下PowerJob的Alarmable Alarmable tech/powerjob/server/extension/Alarmable.java public interface Alarmable {void onFailed(Alarm alarm, List<UserInfoDO> targetUserList); }Alarmable接口定义了onFailed方法,其入参为alarm及tar…...

系列三十五、获取Excel中的总记录数
一、获取Excel中的总记录数 1.1、概述 使用EasyExcel开发进行文件上传时,通常会碰到一个问题,那就是Excel中的记录数太多,使用传统的方案进行文件上传,很容易就超时了,这时可以通过对用户上传的Excel中的数量进行限制…...

VMware workstation安装debian-12.1.0虚拟机并配置网络
VMware workstation安装debian-12.1.0虚拟机并配置网络 Debian 是一个完全自由的操作系统!Debian 有一个由普罗大众组成的社区!该文档适用于在VMware workstation平台安装debian-12.1.0虚拟机。 1.安装准备 1.1安装平台 Windows 11 1.2软件信息 软…...

centos下系统全局检测工具dstat使用
目录 一:没有需要安装 二:dstat命令参数 三、监测界面各参数含义(部分) 四、dstat的高级用法 一:没有需要安装 yum install dstat 二:dstat命令参数 有默认选项,执行dstat命令不加任何参数…...
无人机群ros通信
单架无人机与地面站通信 在一个局域网内获取无人机的机载电脑ip 通过地面站ssh到机载电脑,实现通信 多架无人机与地面站通信 在ROS基础上,配置主机和从机,实现主机和从机的话题联通 配置hosts 在主机和从机的/etc/hosts文件中,…...

LeetCode刷题:142. 环形链表 II
题目: 是否独立解决:否,参考了解题思路解决问题,思考了用快慢指针,栈,统计链表数量定位尾巴节点(因为是环形链表所以是死循环,链表数量用while循环统计不出来)都没解决 解…...

Laravel 使用rdkafka_laravel详细教程(实操避坑)
一、选择rdkafka 首先要看版本兼容问题,我的是Laravel5.6,PHP是7.3.13,所以需要下载兼容此的rdkafka,去 Packagist 搜索 kafka ,我用的是 enqueue/rdkafka选择里面0.10.5版本, 二、安装rdkafka 在 Larav…...
439 - Knight Moves (UVA)
题目链接如下: Online Judge UVA439 骑士的移动 - 锦依卫议事堂 - 洛谷博客 这里有好几个特别厉害的解法...先存着慢慢看。 我的代码如下: #include <iostream> #include <deque> #include <string> // #define debugstruct node{…...

数据结构(c)冒泡排序
本文除了最下面的代码是我写的,其余是网上抄写的。 冒泡排序 什么是冒泡排序? 冒泡排序(Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交…...

并发编程之并发容器
目录 并发容器 CopyOnWriteArrayList 应用场景 常用方法 读多写少场景使用CopyOnWriteArrayList举例 CopyOnWriteArrayList原理 CopyOnWriteArrayList 的缺陷 扩展迭代器fail-fast与fail-safe机制 ConcurrentHashMap 应用场景 常用方法 并发场景下线程安全举例 Con…...

K8s---存储卷(动态pv和pvc)
当我要发布pvc可以生成pv,还可以共享服务器上直接生成挂载目录。pvc直接绑定pv。 动态pv需要两个组件 1、卷插件:k8s本生支持的动态pv创建不包括nfs,需要声明和安装一个外部插件 Provisioner: 存储分配器。动态创建pv,然后根据pvc的请求自动…...
JS判断对象是否为空对象的几种方法
通过JSON自带的stringify()方法判断 function isEmptyObj(obj) {return JSON.stringify(obj) {} } console.log(对象是否为空:, isEmptyObj({}))for in 循环判断 function isEmptyObj(obj) {for(let item in obj) {return true}return false } console.log(对…...
算法通关村第十五关—用4KB内存寻找重复元素(青铜)
用4KB内存寻找重复元素 用4KB内存寻找重复元素 题目要求:给定一个数组,包含从1到N的整数,N最大为32000,数组可能还有重复值,且N的取值不定,若只有4KB的内存可用,该如何打印数组中所有重复元素。…...
【PHP】判断字符串是否是有效的base64编码
目录 1.检测长度: 2.检查字符集: 3.判断是否能正常解码 在PHP中,判断一个字符串是否是有效的Base64编码,可以通过以下几种方法: 1.检测长度: Base64编码的字符串长度必须是4的倍数(对于标准…...
鼎盛合|测量精度SOC芯片开发中的技术问题整理
SOC芯片近几年的发展势头迅猛,许多行业中俱可见其身影。SOC芯片并不是传统意义上的芯片,它是一个由多种功能集成的一个芯片。SOC芯片自身在出厂时便带有部分程序,是为了方便设计开发而针对某些行业设计的特定功能。如芯海的SOC芯片大多数则是…...

使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式
一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明:假设每台服务器已…...

1.3 VSCode安装与环境配置
进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件,然后打开终端,进入下载文件夹,键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...

ardupilot 开发环境eclipse 中import 缺少C++
目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...

自然语言处理——循环神经网络
自然语言处理——循环神经网络 循环神经网络应用到基于机器学习的自然语言处理任务序列到类别同步的序列到序列模式异步的序列到序列模式 参数学习和长程依赖问题基于门控的循环神经网络门控循环单元(GRU)长短期记忆神经网络(LSTM)…...

GC1808高性能24位立体声音频ADC芯片解析
1. 芯片概述 GC1808是一款24位立体声音频模数转换器(ADC),支持8kHz~96kHz采样率,集成Δ-Σ调制器、数字抗混叠滤波器和高通滤波器,适用于高保真音频采集场景。 2. 核心特性 高精度:24位分辨率,…...

C++:多态机制详解
目录 一. 多态的概念 1.静态多态(编译时多态) 二.动态多态的定义及实现 1.多态的构成条件 2.虚函数 3.虚函数的重写/覆盖 4.虚函数重写的一些其他问题 1).协变 2).析构函数的重写 5.override 和 final关键字 1&#…...

从“安全密码”到测试体系:Gitee Test 赋能关键领域软件质量保障
关键领域软件测试的"安全密码":Gitee Test如何破解行业痛点 在数字化浪潮席卷全球的今天,软件系统已成为国家关键领域的"神经中枢"。从国防军工到能源电力,从金融交易到交通管控,这些关乎国计民生的关键领域…...

【Linux】Linux安装并配置RabbitMQ
目录 1. 安装 Erlang 2. 安装 RabbitMQ 2.1.添加 RabbitMQ 仓库 2.2.安装 RabbitMQ 3.配置 3.1.启动和管理服务 4. 访问管理界面 5.安装问题 6.修改密码 7.修改端口 7.1.找到文件 7.2.修改文件 1. 安装 Erlang 由于 RabbitMQ 是用 Erlang 编写的,需要先安…...

Win系统权限提升篇UAC绕过DLL劫持未引号路径可控服务全检项目
应用场景: 1、常规某个机器被钓鱼后门攻击后,我们需要做更高权限操作或权限维持等。 2、内网域中某个机器被钓鱼后门攻击后,我们需要对后续内网域做安全测试。 #Win10&11-BypassUAC自动提权-MSF&UACME 为了远程执行目标的exe或者b…...
计算机系统结构复习-名词解释2
1.定向:在某条指令产生计算结果之前,其他指令并不真正立即需要该计算结果,如果能够将该计算结果从其产生的地方直接送到其他指令中需要它的地方,那么就可以避免停顿。 2.多级存储层次:由若干个采用不同实现技术的存储…...