LoginSignup
63
59

More than 5 years have passed since last update.

【Android】既存アプリにFirebaseを入れるときにハマった話

Last updated at Posted at 2016-06-07

【Android】30分で実装するPush通知にてFirebase導入の簡単さと便利さに感激して早速弊社のアプリにも導入しました。
が、しかし、既存のアプリに組み込んでリリース前に動作確認しているときに想定外の不具合が発生して、ドハマりしたので書き残しておきます。

使おうとした機能はFirebase AnalyticsとNotificationです。
(動作確認は基本的にNexus5の6.0.1で実施してます)

既存のplay-servicesライブラリとの競合

結論

  • play-servicesライブラリはgoogle-servicesプラグインで置き換える

(「Firebase Get Started」に書いておいてくれよおおおおおお!!!!)

問題点と解決方法

問題点

既存アプリでplay-servicesを使っている場合、「Get Started」の通りにそのままライブラリとプラグインを追加すると、ANRが発生します。
※アプリ起動後、一度ホーム画面へ行って再度アプリへ戻ったタイミングでANRが発生するので微妙に気付きにくいのがミソです。。。

ライブラリ同士で競合しているとのこと。

解決方法

既にGoogle Analyticsなどを使っていて、「Get Started」の通りに導入すると以下のようになります。

app/build.gradle(対応前)

dependencies {
    // 略

    // google play service
    compile 'com.google.android.gms:play-services:9.0.2'
    compile 'com.google.android.gms:play-services-base:9.0.2'
    compile 'com.google.android.gms:play-services-gcm:9.0.2'

    // ADD Firebase
    compile 'com.google.firebase:firebase-core:9.0.2'
    compile 'com.google.firebase:firebase-messaging:9.0.2'
    compile 'com.google.firebase:firebase-crash:9.0.2'
}

apply plugin: 'com.google.gms.google-services'

以下のようにplay-servicesを削除して、play-service-analyticsを追加すればOKです。(Analyticsを使っていなければ追加は必要ありません)

app/build.gradle(対応後)

dependencies {
    // 略

    // google play service
    compile 'com.google.android.gms:play-services-base:9.0.2'
    compile 'com.google.android.gms:play-services-gcm:9.0.2'
    compile 'com.google.android.gms:play-services-analytics:9.0.2'

    // ADD Firebase
    compile 'com.google.firebase:firebase-core:9.0.2'
    compile 'com.google.firebase:firebase-messaging:9.0.2'
    compile 'com.google.firebase:firebase-crash:9.0.2'
}

apply plugin: 'com.google.gms.google-services'

Notificationアイコンがカスタムできない

結論

  • シンプルかつ輪郭だけで判別できるようなアイコンにしよう

問題点と解決方法

問題点

9.0.2現状、Firebase Notificationではアプリがbackgroundにいるときに表示されるNotificationアイコンはカスタマイズできません
使われるアイコンはアプリのLauncherアイコンなのですが、任意のNotification用のアイコンには変えられないわけです。
更に、アイコン画像はsmallIconにセットされる仕様らしく、Android端末のバージョンによってはアイコンが判別できなくなります。
(手元には4.x系と6の端末しかなかったのですが、5.0以上の端末で判別不可?)

ということで、よくある角丸の四角いアイコンの中にサービス名などを書いているようなアイコンだと全く判別付きません。

対応前後での見え方

角を丸くした四角いアイコン(赤枠外透過あり)を使っていたのですが以下のようになってしまい、どのアプリからの通知だかさっぱりわかりません。。。
notif_icon_sq_sample.png

これを、背景透過の輪郭だけでわかるアイコンにすると
notif_icon_sample.png

なんとなくどのアプリからの通知か分かるようになった気がします。

Launcherアイコンの例

例えば下のスクショだと、2段目のLauncherアイコンならおそらく判別可能ですがそれ以外はどのアプリからの通知だか分からないです。
screen_shot.png

とはいえそう簡単にLauncherアイコンを変えるわけにもいかないと思いますので、早くNotificationアイコンのカスタマイズできるようにして欲しいところです。。。

(おまけ)Google Play servicesをFirebase APIsに寄せていく動き

Google APIs for Androidをみると、以下のような記述があります。

・App Invites (now Firebase Invites) is still available at com.google.android.gms.appinvite, but going forward you should use com.google.firebase:firebase-invites.

・Firebase Cloud Messaging builds on and improves the Google Cloud Messaging API. You can keep using Google Cloud Messaging with com.google.android.gms.gcm, but we recommend upgrading to com.google.firebase:firebase-messaging.

超意訳すると以下の内容になります。

  • App Invites使いたかったらcom.google.firebase:firebase-invites使ってね。com.google.android.gms.appinviteは今んとこ使えるけど使えなくなってもしらないよ。
  • Firebase Cloud Messaging > Google Cloud Messaging APIだよ。まだcom.google.android.gms.gcm使えるけど、com.google.firebase使ってFirebase Cloud Messagingへの移行をオススメするよ。

ということで、I/O 2016でさんざんFirebaseのセッションしてたことも踏まえて、Googleは諸々の機能をFirebaseに移行しようとしているみたいです。
Google教徒のみなさまはこの期にFirebase導入を検討してみてはいかがでしょう。

グーメン( ´-`)†

63
59
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
63
59