firebase 推送教程

本文介绍如何在Android项目中配置Firebase Cloud Messaging (FCM),包括获取SHA1指纹、添加google-services.json文件、集成Google服务插件及实现消息接收等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

申请与添加android项目

这个证书建议使用debug的

Sha1这个是啥呢?

是keysotre的sha1呀,好像在eclipse里直接能查这个debug的sha1. Android studio我没找到。

一般来说windows上debug.keystore在:

C:\Users\Administrator\.android\debug.keystore

进入cmd:

keytool -list -alias androiddebugkey-keystore C:\Users\Administrator\.android\debug.keystore

然后输入密码:直接回车

我的机器上是:E7:5A:3C:53:FB:0C:DE:88:7E:15:A6:E9:D3:20:0A:D5:1D:BA:4B:15

填在上页即可!

 

 

复制配置文件

这里直接下载这个google-services.json

 

 

配置这个google-services.json

 

 

 

 

具体使用

1. First, add rules to your root-level build.gradle file, to include the google-services plugin:

buildscript {
   
// ...
    dependencies {
       
// ...
        classpath
'com.google.gms:google-services:3.0.0'
    }
}

2. Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:

apply plugin:'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:10.0.1'
  compile 'com.google.firebase:firebase-messaging:10.0.1'
  // Getting a "Could not find" error? Make sure you have
  // the latest Google Repository in the Android SDK manager
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
3. <service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <actionandroid:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
4. <meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification"/>
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent"/>
5. @Override
publicvoid onMessageReceived(RemoteMessage remoteMessage){
    // ...

    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG,"From: "+ remoteMessage.getFrom());

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

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

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

 

6.服务器发送

在左侧:

然后点击发送,记得选择项目

 

 

 

 

测试通过。记得需要翻墙,翻墙,翻墙!手机和Pc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值