AndroidでPush通知を受け取るためのメモです。
Firebaseコンソールでプロジェクトを作成していきます。
実装手順
「プロジェクトを作成」をクリックします。
プロジェクト名を指定します。
アナリティクスの指定は省略して進みます。
プロジェクトが作成されました。
Cloud Messagingをクリックします。
Androidアプリを追加します。
Android StudioでEmpty Activityのプロジェクトを作成します。ここで指定したPackage nameは後ほどFirebaseに登録します。
SHA-1の取得。Firebaseのデバッグ用証明書キーをAndroid Studioで取得します。画面右のGradleタブを引き出して、app > Tasks > singningReportを右クリックして「Run」をクリックします。
BuildペインにSHA1を含む各キーストアが表示されます。
AndroidアプリへのFirebaseの追加
設定ファイルのダウンロード
設定ファイルをコピー(cmd+C)し、プロジェクトのappフォルダーをフォーカスした状態でペースト(cmd+V)します。
Firebase SDKをAndroidプロジェクトに追加します。
プロジェクトレベルのbuild.gradleを修正します。dependenciesに
classpath 'com.google.gms:google-services:4.2.0'
を追加します。buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.0' // Add this line classpath 'com.google.gms:google-services:4.2.0' } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
アプリレベルのbuild.gradleを修正します。dependenciesに
implementation 'com.google.firebase:firebase-messaging:17.6.0'
を追加します。使用するライブラリは 使用可能なライブラリの一覧から「Cloud Messaging」の項目を確認します。 また、チュートリアルに書かれているapply plugin: 'com.google.gms.google-services'
も末尾に追加します。これがないと例外が発生して動作しません(ここでハマりました)。apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion "29.0.1" defaultConfig { applicationId "com.yuppejp.hellofcm" minSdkVersion 26 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' // add SDKs for desired Firebase products implementation 'com.google.firebase:firebase-messaging:17.6.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' } // Add the following line to the bottom of the file: apply plugin: 'com.google.gms.google-services'
修正が終わったらAndroid Studio画面右上の「Sync now」をクリックします。
これで、Firebase の設定が完了しました。 [f:id:yuppejp:20190908164307p:plain:alt=設定完了]
FirebaseMessagingServiceを継承したMy FirebaseMessagingServiceクラスを作成し、AndroidManifest.xmlにサービスとして登録します。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yuppejp.hellofcm"> <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> <!-- add ここから--> <service android:name=".MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <!-- add ここまで--> </application> </manifest>
これで完成です
Androidアプリを実行するとPush通知が受け取れるようになります。
アプリはバックグランドにまわっていたり、端末がスリープ状態でも通知を受け取ることができました。ただし、アプリをKillすると通知を受け取ることはできなくなります。
例外が出たら
この例外が出る場合は、プロジェクトの設定が不十分な場合に発生します。
Default FirebaseApp is not initialized in this process Make sure to call FirebaseApp.initializeApp(Context) first. cloud messaging
自分の場合は、アプリレベルのbuild.gradleの設定で、 apply plugin: 'com.google.gms.google-services'
の設定が抜けていました。これは公式のチュートリアルに書かれていますので、参考にしてください。
公式ドキュメント
参考にしたサイト
ありがとうございました。