1. 程式人生 > >firebase 推送教程

firebase 推送教程

申請與新增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 pluginline 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. <serviceandroid:name=".MyFirebaseMessagingService"><intent-filter><actionandroid:name="com.google.firebase.MESSAGING_EVENT"/></intent-filter></service>
4. <meta-dataandroid: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-dataandroid:name="com.google.firebase.messaging.default_notification_color"android:resource="@color/colorAccent"/>
5. @Overridepublicvoid onMessageReceived(RemoteMessage remoteMessage){// ...// TODO(developer): Handle FCM messages here.// Not getting messages here? See why this may be: https://goo.gl/39bRNJLog.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