FCM を Firebase コンソールから使う

以前まとめておいたつもりが時間が経つとすぐに理解できない。

FCM と Notification の併用をやめてバックグラウンド受信時にうれしがる

もう、面倒くさいので忘れていても直感的すぐに使いたいので

WEBで提供されている Firebaseコンソール(コンポーザー?)から。

 

送信時の「メッセージ」は必須である

APIから送信する場合は、「データ」のみでバックグランドでしれっと端末に渡すことができるが、

WEB画面から送信する場合は「メッセージ」の入力なしでは送信ボタンがが有効化されず送信できない。

 

送信時にアプリがフォアグラウンドの場合

通知バーは表示されない。

「データ」は、端末内で稼働している Service で受け取る。

「データ」がなければ何も起きない。

 

アプリがバックグラウンドの場合

「メッセージ」と「データ」は通知バーに入る。

「データ」のタイトルは通知バーに表示され、キーと値のペアデータは PendingIntent として通知バーに入る。

 

どう使う?

端末内でプロセスが死んでない限りはフォア/バックグラウンド共に同じ挙動としたい。

フォアグラウンド受信時にも同じ挙動をするように、サービスに処理を追加。

QucikStart のサンプル内に未稼働のコードがある。


/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

quickstart-android/MyFirebaseMessagingService.java at master · firebase/quickstart-android

結局は、起動されるActivityでIntentを受けるような処理を記述しなければならない。 これ書くと見通し悪くなる。きもい。


if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        Object value = getIntent().getExtras().get(key);
        Log.d(TAG, "Key: " + key + " Value: " + value);
    }
}

quickstart-android/MainActivity.java at master · firebase/quickstart-android

直感的にそのまま使うと、単に「アプリ起動を喚起するだけのもの」になってしまいがちだのだが、きもい。


Play Services 12.0.0 で android.permission.READ_PHONE_STATE が要求されている件

Google Play Console にアップすると怒られて受け付けてくれません。

"New permissions added
WARNING
Users that have the APK with version code 19 may need to accept one or more of the android.permission.READ_PHONE_STATE and android.permission.WRITE_EXTERNAL_STORAGE permissions, which may result in them not upgrading to this version of the app."

android.permission.READ_PHONE_STATE てどんな許可内容だったか。

端末のステータスとIDの読み取り
端末の電話機能へのアクセスをアプリに許可します。これにより、電話番号、端末ID、通話中かどうか、通話相手の電話番号をアプリから特定できるようになります。

危険なパーミッションです。

「通話機能の必要ないアプリでも Play Service を利用していれば弾かれる」というような状況のようです。

android.permission.READ_PHONE_STATE added with Play Services 12.0.0 [76024034] - Visible to Public - Issue Tracker

android - Why has the READ_PHONE_STATE permission been added? - Stack Overflow

以下で回避できるようです。できました。


<uses-permission
    android:name="android.permission.READ_PHONE_STATE"
    tools:node="remove" />

なんなんでしょうね。

このような混乱はきっとこの先も増えていきそうです。

ーーー
2018-04-04: 追記
修正されているようです。
Release Notes  |  Google APIs for Android  |  Google Developers


SharedPreferences で putStringSet() が1つしか保存できない。

何ですか、この動きは。

「Preference から Set を取得後、追加して再保存する」

問題なく以下のようなテストも通過します。


@Test fun getStringSet() {

  val key = "key"
  val preferences = context.getSharedPreferences(key, Context.MODE_PRIVATE)
  val beforeData = preferences.getStringSet(key, mutableSetOf())
  val beforeSize = beforeData.size

  beforeData.add(System.currentTimeMillis().toString())
  preferences.edit()
      .putStringSet(key, beforeData)
      .commit()

  val afterData = preferences.getStringSet(key, mutableSetOf())
  val afterSize = afterData.size

  println("$beforeData -> $afterData")
  println("$beforeSize -> $afterSize")
  assertEquals(beforeSize + 1, afterSize)

}


I/System.out: [1520865219872, 1520865358333] -> [1520865219872, 1520865358333]
I/System.out: 1 -> 2

しかし、何回実行してもデータが1つのままで増えていきません。


I/System.out: [1520865219872, 1520865504185] -> [1520865219872, 1520865504185]
I/System.out: 1 -> 2

I/System.out: [1520865219872, 1520865553254] -> [1520865219872, 1520865553254]
I/System.out: 1 -> 2

実際にファイルを確認してみると、


mako:/data/data/com.example.cryptocurrency/shared_prefs # cat key.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <set name="key">
        <string>1520865219872</string>
    </set>
</map>

1つしか保存されてないですね!

クソですね!

なぜなのか?

android - Misbehavior when trying to store a string set using SharedPreferences - Stack Overflow

Android compares the modified HashSet that you are trying to save using SharedPreferences.Editor.putStringSet with the current one stored on the SharedPreference, and both are the same object!!!

A possible solution is to make a copy of the Set returned by the SharedPreferences object

どうやら、同じオブジェクトの比較となるので保存してくれないようです。

読み出し後、すぐコピーすればいいのですね!


val beforeData = preferences.getStringSet(key, mutableSetOf()).toMutableSet()

なんとなく、よくある話のような気がします。


サポートライブラリのバージョンを常に一発で揃える

こんなのでました。

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.1.0, 27.0.2. Examples include com.android.support:animated-vector-drawable:27.1.0 and com.android.support:cardview-v7:27.0.2 less... (⌃F1)
There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion.)

「com.android.support:*」のバージョンは統一しておく必要があるのでしょうが、「利用しているサポートライブラリのバージョンが違う」ということなのでしょうか。

 

依存関係の確認


$ ./gradlew app:dependencies

大量の依存関係の出力から細々と見ていきます。

という感じで古いサードパーティのライブラリなどから、

「うまくバージョンを推移させながら参照できない。」

ということなのでしょう。

 

すばやく特定する

現在の最新バージョン「27.1.0」に揃えます。

行数が多くて面倒なので、grep や sort など使って推移解決できてないものを特定します。


$ ./gradlew app:dependencies | grep com.android.support: | grep -v 27.1.0 | sort | uniq
|    +--- com.android.support:customtabs:27.0.2 (*)
|    \--- com.android.support:cardview-v7:27.0.2 (*)
|    |    +--- com.android.support:cardview-v7:27.0.2
|    |    +--- com.android.support:customtabs:27.0.2

となり

「com.android.support:customtabs」
「com.android.support:cardview-v7」

の2つであることがわかります。

この2つを build.gradle にて追加明示してあげて、sync で赤線消えます。

しかし、これもだるいですね!

 

 

Gradle ResolutionStrategy を使う

この件はサポートライブラリに関してよく遭遇するので、細かく見る方法を覚えておきながら一括な定型にしておきましょう。

force で個別よりグループまとめれば簡単でしょうか。


// build.gradle (project)

// ...

allprojects {

  // ...

  // Force all of the primary support libraries to use the same version.
  configurations.all {
    resolutionStrategy {

      // force "com.android.support:support-annotations:${versions.supportLibrary}"
      // force 'com.google.code.findbugs:jsr305:3.0.0'

      eachDependency { details ->
        if (details.requested.group == 'com.android.support') {
          details.useVersion versions.supportLibrary
        }
      }
    }
  }
}

↓ 基本的なバージョン記述は前回より

あなたの build.gradle バージョン記述、きもいです。

ResolutionStrategy - Gradle DSL Version 4.6

All com.android.support libraries must use the exact same version specification - Stack Overflow


あなたの build.gradle バージョン記述、きもいです。

きもいですよ。

こう書いていたり、


minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion

こう書いてたり、


dependencies {
  implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
  implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
  implementation "com.android.support:design:$rootProject.supportLibraryVersion"

「変数を使ってまとめました!」とか言ってるけど。

きもいですよ。

そもそも、定義の位置がヘボいのですよ。

 

書いてたのは私です。

以下のように書いてました。

root/build.gradle


ext {

  minSdkVersion = 24
  targetSdkVersion = 27
  compileSdkVersion = 27
  buildToolsVersion = '27.0.2'

  supportLibraryVersion = '27.1.0'
  firebaseVersion = '11.8.0'

}

これのせいで、「${rootProject.firebaseVersion」とか「rootProject.ext.***」とかダラダラ書く羽目になるようです。

stackoverflow も今やゴミのだらけでどれが正しいのか分かりづらいです。

 

神はこのように書いていた。

root/build.gradle


buildscript {

  ext.buildConfig = [
      'compileSdk': 27,
      'minSdk': 24,
      'targetSdk': 27
  ]

  ext.versions = [
      'supportLibrary': '27.1.0',
      'kotlin': '1.2.30',
      'okhttp': '3.10.0',
      'retrofit': '2.3.0',
      'kotshi': '1.0.1',
      'dagger': '2.15',
  ]

ブロック buildscript {} 内に map ですっきり記述しています。

module/build.gradle


android {

  compileSdkVersion buildConfig.compileSdk

  defaultConfig {

    minSdkVersion buildConfig.minSdk
    targetSdkVersion buildConfig.targetSdk

    versionCode buildConfig.versionCode
    versionName buildConfig.versionName

  }
}

// ....

dependencies {

  implementation "org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlin}"
  implementation "com.android.support:appcompat-v7:${versions.supportLibrary}"
  implementation "com.android.support:support-v4:${versions.supportLibrary}"
  implementation "com.android.support:design:${versions.supportLibrary}"

  implementation "com.google.firebase:firebase-core:${versions.firebase}"
  implementation "com.google.firebase:firebase-auth:${versions.firebase}"

定形なテンプレートとして使えます。

参照する際は、「rootProject」とか「ext」の単語は不要です。

 

関連の以下も必須だろうと思います。

サポートライブラリのバージョンを揃える

👉 【Gradle Version Catalog】libs.versions.toml キー名の形式 camelCase vs kebab-case hatena-bookmark