当前位置: 代码迷 >> 综合 >> FCM服务端 - 谷歌云推送 - “errorCode“: “SENDER_ID_MISMATCH“-故障记录
  详细解决方案

FCM服务端 - 谷歌云推送 - “errorCode“: “SENDER_ID_MISMATCH“-故障记录

热度:45   发布时间:2024-03-08 21:04:23.0

前言

2020.10.27 我没解决,记录一些思路,这些问题其他人没有出现唯独我出现了,为什么。

核心代码

main

public class BaoTest {
    private static final String String = null;// 设备的token值public static String token ="AAAARWGhpmw:APA91bE6E7KCs-Tvjh5526LxGL-l1vYySzsnfNN8GhUpt9avG8bsjJEgqPX_txHBgHgfQ2BEt4Ms4aFRZGI7dbRm_pWy7JDqn1mIF9NYBU7WeLrOPt1j3GIll6njvxKkyp7r-q23hlh9";// 渠道名字,也是APP的名字// public static String appName = "VisionC";// 无关内容判别// 主题名字// public static String topic = "China";// 通知消息题目public static String title = "tip";// 通知消息内容public static String body = "body";// 测试内容public static void main(String args[]) throws Exception {
    // 添加tokensList<String> tokens = new LinkedList();tokens.add(token);// 设置Java代理,端口号是代理软件开放的端口,这个很重要。System.setProperty("proxyHost", "localhost");System.setProperty("proxyPort", "8580"); // 1080,8580// String jsonPath = "path/to/serviceAccountKey.json" ;
// String dataUrl = "https://telecomm-773e6e.firebaseio.com/";
// String jsonPath = "D:\\eclipse_workplace_learn\\HelloSpringMVC\\src\\main\\resources\\"
// //+ "baotestserver-firebase-adminsdk-whfsf-311cd00bee" 
// +"visionc-cd27f-firebase-adminsdk-rdqff-26955b119f"
// + ".json" ;// 初始化FirebaseAppif (!FireBaseUtil.initSDK())return;System.out.println("FireBaseUtil init over");FireBaseUtil.pushSingle(token, title, body); // 单推
// 
// 
// FireBaseUtil.registrationTopic(appName, tokens, topic); //设置主题
// 
// FireBaseUtil.sendTopicMes(appName, topic, title, body); //按主题推送
// 
// FireBaseUtil.cancelTopic(appName, tokens, topic); //取消主题
// // 安卓设备推送
// FireBaseUtil.pushSingleToAndroid(appName, token, title, body);
// FireBaseUtil.registrationTopic(appName, tokens, topic); //设置主题
// FireBaseUtil.sendTopicMesToAndroid(appName, topic, title, body);}
}

FireBaseUtil

工具类

package com.bao;import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.annotations.VisibleForTesting;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.UserRecord;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.messaging.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;/*** {@code description: FireBaseUtil Firebase Cloud Message}* Google_FireBase推送工具类** @author zhangkai* @version 1.0* @since 2019/7/5 15:48*/
@Component
public class FireBaseUtil {
    //获取AndroidConfig.Builder对象private static AndroidConfig.Builder androidConfigBuilder = AndroidConfig.builder();//获取AndroidNotification.Builder对象private static AndroidNotification.Builder androidNotifiBuilder = AndroidNotification.builder();public static FirebaseApp firebaseApp;// Retrieve services by passing the defaultApp variable...public static FirebaseAuth defaultAuth ;public static FirebaseDatabase defaultDatabase ;public static boolean initSDK() {
    try {
    //设置环境FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.getApplicationDefault()).setDatabaseUrl("https://visionc-cd27f.firebaseio.com").build();firebaseApp = FirebaseApp.initializeApp(options);// Retrieve services by passing the defaultApp variable...defaultAuth = FirebaseAuth.getInstance(firebaseApp);          //not useddefaultDatabase = FirebaseDatabase.getInstance(firebaseApp);  //not usedSystem.out.println(firebaseApp.getName());  // "[DEFAULT]"} catch (Exception e) {
    System.out.println("SDK init failed");e.printStackTrace();return false;}return true;}/*** 单设备推送* @param token 注册token* @param title 推送题目* @param body 推送内容* @return* @throws IOException* @throws FirebaseMessagingException*/public static void pushSingle( String token, String title, String body) throws IOException, FirebaseMessagingException {
    //获取实例// FirebaseApp firebaseApp = FirebaseApp.getInstance();//实例为空的情况if (firebaseApp == null) {
    System.out.println("实例为空");return;}//构建消息内容//Notification(String title, String body, String imageUrl)Message message = Message.builder().setNotification(new Notification(title, body)).setToken(token).build();//发送后,返回messageIDString response = FirebaseMessaging.getInstance(firebaseApp).send(message);System.out.println("单个设备推送成功 : " + response);}/*** 单设备推送* @param token 注册token* @param title 推送题目* @param body 推送内容* @param map 额外的参数* @return* @throws IOException* @throws FirebaseMessagingException*/public static void pushSingle(String token, String title, String body, Map<String,String> map) throws IOException, FirebaseMessagingException {
    //获取实例FirebaseApp firebaseApp = FirebaseApp.getInstance();//实例为空的情况if (firebaseApp == null) {
    return;}//构建消息内容//Notification(String title, String body, String imageUrl)Message message = Message.builder().setNotification(new Notification(title, body)).putAllData(map).setToken(token).build();//发送后,返回messageID//message.getNotification();String response = FirebaseMessaging.getInstance(firebaseApp).send(message);System.out.println("单个设备推送成功 : " + response);}/*** {@code description: 单设备推送}* @param token 注册token* @param title 推送题目* @param body 推送内容* @param extra 额外的参数* @return* @throws IOException* @throws FirebaseMessagingException* @author zhangkai* @since 2019/10/14 11:24*/public static void pushSingle( String token, String title, String body, String extra) throws IOException, FirebaseMessagingException {
    //获取实例FirebaseApp firebaseApp = FirebaseApp.getInstance();//实例为空的情况if (firebaseApp == null) {
    return;}//构建消息内容//Notification(String title, String body, String imageUrl)Message message = Message.builder().setNotification(new Notification(title, body)).putData("extra",extra).setToken(token).build();//发送后,返回messageIDString response = FirebaseMessaging.getInstance(firebaseApp).send(message);System.out.println("单个设备推送成功 : " + response);}/*** 给设备订阅主题* @param tokens 设备的token,最大1000个* @param topic 要添加的主题* @return* @throws FirebaseMessagingException* @throws IOException*/public static void registrationTopic( List<String> tokens, String topic) throws FirebaseMessagingException, IOException {
    //获取实例FirebaseApp firebaseApp = FirebaseApp.getInstance();//实例不存在的情况if (firebaseApp == null) {
    return;}//订阅,返回主题管理结果对象。TopicManagementResponse response = FirebaseMessaging.getInstance(firebaseApp).subscribeToTopic(tokens, topic);System.out.println("添加设备主题,成功:" + response.getSuccessCount() + ",失败:" + response.getFailureCount());}/*** 取消设备的订阅主题* @param tokens 设备的token,最大1000个* @param topic 取消的主题* @return* @throws FirebaseMessagingException* @throws IOException*/public static void cancelTopic( List<String> tokens, String topic) throws FirebaseMessagingException, IOException {
    //获取实例FirebaseApp firebaseApp = FirebaseApp.getInstance();//实例不存在的情况if (firebaseApp == null) {
    return;}//取消订阅,返回主题管理结果对象。TopicManagementResponse response = FirebaseMessaging.getInstance(firebaseApp).unsubscribeFromTopic(tokens, topic);System.out.println("取消设备主题,成功:" + response.getSuccessCount() + ",失败:" + response.getFailureCount());}/*** 按主题推送* @param topic 主题的名字* @param title 消息题目* @param body 消息体* @return* @throws FirebaseMessagingException* @throws IOException*/public static void sendTopicMes( String topic, String title, String body) throws FirebaseMessagingException, IOException {
    //获取实例FirebaseApp firebaseApp = FirebaseApp.getInstance();//实例不存在的情况if (firebaseApp == null) {
    return;}//构建消息Message message = Message.builder().setNotification(new Notification(title, body)).setTopic(topic).build();//发送后,返回messageIDString response = FirebaseMessaging.getInstance(firebaseApp).send(message);System.out.println("主题推送成功: " + response);}/*** 单条Android设备推送消息(和pushSingle方法几乎没有区别)* @param token 注册token* @param title 推送题目* @param body 推送内容* @throws FirebaseMessagingException*/public static void pushSingleToAndroid( String token, String title, String body) throws FirebaseMessagingException {
    //获取实例FirebaseApp firebaseApp = FirebaseApp.getInstance();//实例为空的情况if (firebaseApp == null) {
    return;}androidConfigBuilder.setRestrictedPackageName("io.telecomm.telecomm");androidNotifiBuilder.setColor("#55BEB7");// 设置消息通知颜色androidNotifiBuilder.setIcon("https://www.shiku.co/images/favicon.png");// 设置消息图标androidNotifiBuilder.setTitle(title);// 设置消息标题androidNotifiBuilder.setBody(body);// 设置消息内容AndroidNotification androidNotification = androidNotifiBuilder.build();androidConfigBuilder.setNotification(androidNotification);AndroidConfig androidConfig = androidConfigBuilder.build();//构建消息Message message = Message.builder().setToken(token).setAndroidConfig(androidConfig).build();//发送后,返回messageIDString response = FirebaseMessaging.getInstance(firebaseApp).send(message);System.out.println("单个安卓设备推送成功 : " + response);}/*** Android按主题推送(和sendTopicMes方法几乎没有区别)* @param topic 主题的名字* @param title 消息题目* @param body 消息体* @return* @throws FirebaseMessagingException* @throws IOException*/public static void sendTopicMesToAndroid( String topic, String title, String body) throws FirebaseMessagingException, IOException {
    //获取实例FirebaseApp firebaseApp = FirebaseApp.getInstance();//实例为空的情况if (firebaseApp == null) {
    return;}androidNotifiBuilder.setColor("#55BEB7");// 设置消息通知颜色androidNotifiBuilder.setIcon("https://www.shiku.co/images/favicon.png");// 设置消息图标androidNotifiBuilder.setTitle(title);// 设置消息标题androidNotifiBuilder.setBody(body);// 设置消息内容AndroidNotification androidNotification = androidNotifiBuilder.build();androidConfigBuilder.setNotification(androidNotification);AndroidConfig androidConfig = androidConfigBuilder.build();//构建消息Message message = Message.builder().setTopic(topic).setAndroidConfig(androidConfig).build();String response = FirebaseMessaging.getInstance(firebaseApp).send(message);System.out.println("安卓主题推送成功: " + response);}
}

接入流程

根据官方文档整理出的服务器接入流程

admin-sdk接入

采用maven接入
完毕后用maven update和download

<dependency><groupId>com.google.firebase</groupId><artifactId>firebase-admin</artifactId><version>6.8.1</version>
</dependency>

service-json接入

-首先,获取项目的json。
通过项目的project setting里的 Service accounts 里的下载选项,获取密匙,json文件,
-其次设置项目的json的环境变量。
window系统可以命令行,其中path为你存放json的绝对路径
set GOOGLE_APPLICATION_CREDENTIALS=PATH
然后官方表示:
完成上述步骤后,应用默认凭据 (ADC) 能够隐式确定您的凭据,如此您便能在非 Google 环境中测试或运行时使用服务帐号凭据。

初始化sdk

FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.getApplicationDefault()).setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/").build();FirebaseApp.initializeApp(options);

获取应用(单个)

因为我测试功能1个就好,有其他需求看官方文档,写的很详细

// Initialize the default app
FirebaseApp defaultApp = FirebaseApp.initializeApp(defaultOptions);System.out.println(defaultApp.getName());  // "[DEFAULT]"// Retrieve services by passing the defaultApp variable...
FirebaseAuth defaultAuth = FirebaseAuth.getInstance(defaultApp);
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance(defaultApp);// ... or use the equivalent shorthand notation
defaultAuth = FirebaseAuth.getInstance();
defaultDatabase = FirebaseDatabase.getInstance();

测试

运行我的main开始测试,报错,结果如下:

[DEFAULT]
FireBaseUtil init over
Exception in thread "main" com.google.firebase.messaging.FirebaseMessagingException: SenderId mismatchat com.google.firebase.messaging.FirebaseMessagingClientImpl.newException(FirebaseMessagingClientImpl.java:306)at com.google.firebase.messaging.FirebaseMessagingClientImpl.createExceptionFromResponse(FirebaseMessagingClientImpl.java:211)at com.google.firebase.messaging.FirebaseMessagingClientImpl.send(FirebaseMessagingClientImpl.java:127)at com.google.firebase.messaging.FirebaseMessaging$1.execute(FirebaseMessaging.java:139)at com.google.firebase.messaging.FirebaseMessaging$1.execute(FirebaseMessaging.java:136)at com.google.firebase.internal.CallableOperation.call(CallableOperation.java:36)at com.google.firebase.messaging.FirebaseMessaging.send(FirebaseMessaging.java:106)at com.google.firebase.messaging.FirebaseMessaging.send(FirebaseMessaging.java:90)at com.bao.FireBaseUtil.pushSingle(FireBaseUtil.java:86)at com.bao.BaoTest.main(BaoTest.java:48)
Caused by: com.google.api.client.http.HttpResponseException: 403 Forbidden
{
    "error": {
    "code": 403,"message": "SenderId mismatch","status": "PERMISSION_DENIED","details": [{
    "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError","errorCode": "SENDER_ID_MISMATCH"}]}
}at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1072)at com.google.firebase.messaging.FirebaseMessagingClientImpl.sendSingleRequest(FirebaseMessagingClientImpl.java:153)at com.google.firebase.messaging.FirebaseMessagingClientImpl.send(FirebaseMessagingClientImpl.java:125)... 7 more

结语

我所有操作严格按着官网文档进行,并且也进行了翻墙处理,所采用token在控制台可以进行发送,我不认为我操作有误。
我的操作指引参考:
http://www.mamicode.com/info-detail-2682997.html
目前钻研了2天,后续有进度我会更新。

  相关解决方案