さてさて、急きょ、AndroidにFCMを実装しています。
FCMとはFirebase Cloud Messagingの略です。Googleが最近力を入れている、Firebaseの製品群の一つで、手っ取り早く言うと、アプリに送信するPush通知ですね。
今までは、GCM(Google Cloud Messagingの略)というものを使っていましたが、ターゲットAPIが27以降に変更してから、Android8以降の機種でGCMを受信すると、
java.lang.RuntimeException: Unable to start receiver com.google.android.gcm.GCMBroadcastReceiver:
 java.lang.IllegalStateException: Not allowed to start service Intent
 { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=hogehoge.com cmp=hogehoge.com/.GCMIntentService (has extras) }: app is in background uid
というエラーで、アプリを立ち上げていない時、再起動後などにGCMがクラッシュするようになってしまったのです。
回避する方法がどうもなさそうで、FCM実装したほうが早いかという結論になりました。どのみち、GCMは2019年4月に廃止が決まっているのです…。(>_<)
というわけで、今回はそのFCMの一番最初の実装方法を書いておきます。Firebaseのコントロールパネルから、Androidアプリで簡単なメッセージを受信する、というところがゴールです。
結構簡単ですが、Googleさんのドキュメントにあるやり方ではわかりにくいところもあったので、その点を補足しながらやります。
①Firebaseのアカウントを作っておきます。
②メッセージを受信するAndroidアプリを作っておきます。ここでは、FCMdemoという名前のアプリです。
③Android StudioのTooles→Firebase→Cloud Messaging のSet up Firebase Cloud Messagingをクリックします。
もうここまで来たら、できたも同然!!!
①と②を、指示通りにクリックします。流れでできると思います。

そうすると、gradleのファイルの変更とか、google-services.jsonのダウンロードとかの面倒な作業を、このAndroid StudioのFirebase Assistantがやってくれます!!なんて便利なんだ!!!.゚ヽ(*´∀`)ノ゚
アプリは、次のように実装します。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="smart.location.admin.fcmdemo">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
MainActivity
package smart.location.admin.fcmdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
build.gradle(Module: app)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.1.1'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}
build.gradle(Project: FCMdemo)
apply plugin: 'com.android.application'
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "smart.location.admin.fcmdemo"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
とりあえずのゴールとしては、Firebaseのコントロールパネルからメッセージを送信するというところだったので、Googleのチュートリアルにあるような、トークンを取得してどうこう…という手続きは不要です!
アプリをインストールしたら、早速メッセージを送ってみましょう。
①Firebaseにログインします。
②FCMdemo(アプリの名前)をクリック→Cloud Messagingをクリック
③新規メッセージを送信、で次のような画面になりますので、メッセージを入力します。赤丸で囲った、アプリ名を指定しないと送信できませんので、(わかりにくかった)ご注意を。

④アプリをバックグラウンドにしておき、送信します。
すると、Notificationを受信できますね!

すばらしい!.゚ヽ(*´∀`)ノ゚
スマホ単体の個体識別トークンがなくても、アプリをインストールしているユーザーにメッセージが送れるというすばらしさ!!
もちろん、普通は個スマホへのメッセージングに使うでしょうから、これだけではダメだと思いますが、FCMのなんたるかがつかめると思います。⊂(^-^)⊃
