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芯片大多数则是…...
系统设计 --- MongoDB亿级数据查询优化策略
系统设计 --- MongoDB亿级数据查询分表策略 背景Solution --- 分表 背景 使用audit log实现Audi Trail功能 Audit Trail范围: 六个月数据量: 每秒5-7条audi log,共计7千万 – 1亿条数据需要实现全文检索按照时间倒序因为license问题,不能使用ELK只能使用…...
智能在线客服平台:数字化时代企业连接用户的 AI 中枢
随着互联网技术的飞速发展,消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁,不仅优化了客户体验,还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用,并…...
【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)
可以使用Sqliteviz这个网站免费编写sql语句,它能够让用户直接在浏览器内练习SQL的语法,不需要安装任何软件。 链接如下: sqliteviz 注意: 在转写SQL语法时,关键字之间有一个特定的顺序,这个顺序会影响到…...
关于 WASM:1. WASM 基础原理
一、WASM 简介 1.1 WebAssembly 是什么? WebAssembly(WASM) 是一种能在现代浏览器中高效运行的二进制指令格式,它不是传统的编程语言,而是一种 低级字节码格式,可由高级语言(如 C、C、Rust&am…...
Spring数据访问模块设计
前面我们已经完成了IoC和web模块的设计,聪明的码友立马就知道了,该到数据访问模块了,要不就这俩玩个6啊,查库势在必行,至此,它来了。 一、核心设计理念 1、痛点在哪 应用离不开数据(数据库、No…...
力扣-35.搜索插入位置
题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...
通过MicroSip配置自己的freeswitch服务器进行调试记录
之前用docker安装的freeswitch的,启动是正常的, 但用下面的Microsip连接不上 主要原因有可能一下几个 1、通过下面命令可以看 [rootlocalhost default]# docker exec -it freeswitch fs_cli -x "sofia status profile internal"Name …...
Vue3中的computer和watch
computed的写法 在页面中 <div>{{ calcNumber }}</div>script中 写法1 常用 import { computed, ref } from vue; let price ref(100);const priceAdd () > { //函数方法 price 1price.value ; }//计算属性 let calcNumber computed(() > {return ${p…...
【把数组变成一棵树】有序数组秒变平衡BST,原来可以这么优雅!
【把数组变成一棵树】有序数组秒变平衡BST,原来可以这么优雅! 🌱 前言:一棵树的浪漫,从数组开始说起 程序员的世界里,数组是最常见的基本结构之一,几乎每种语言、每种算法都少不了它。可你有没有想过,一组看似“线性排列”的有序数组,竟然可以**“长”成一棵平衡的二…...
解析“道作为序位生成器”的核心原理
解析“道作为序位生成器”的核心原理 以下完整展开道函数的零点调控机制,重点解析"道作为序位生成器"的核心原理与实现框架: 一、道函数的零点调控机制 1. 道作为序位生成器 道在认知坐标系$(x_{\text{物}}, y_{\text{意}}, z_{\text{文}}…...
