位置情報とジオフェンス
ジオフェンスは、一部の Braze パッケージでのみ利用できます。利用するには、サポートチケットを作成するか、Braze カスタマーサクセスマネージャーにご相談ください。
Android のジオフェンスをサポートするには、以下の条件を満たす必要があります。
- 統合はバックグラウンドプッシュ通知に対応している必要があります。
- Braze のジオフェンスまたは位置情報収集機能が有効になっている必要があります。
ステップ1: build.gradle を更新する
android-sdk-location
をアプリレベルbuild.gradle
に追加します。また、Google Play Services セットアップガイドを使用して、Google Play Services の位置情報パッケージを追加します。
1
2
3
4
dependencies {
implementation "com.braze:android-sdk-location:+"
implementation "com.google.android.gms:play-services-location:${PLAY_SERVICES_VERSION}"
}
ステップ2: マニフェストを更新する
AndroidManifest.xml
にブート、精度の高い位置情報、バックグラウンド位置情報の権限を追加します。
1
2
3
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
バックグラウンド位置情報アクセス権限は Android 10 で追加されたもので、Android 10 以降のすべてのデバイスでは、アプリがバックグラウンドで動作している間ジオフェンスが機能するために必要です。
AndroidManifest.xml
のapplication
エレメントに Braze ブートレシーバーを追加します。
1
2
3
4
5
<receiver android:name="com.braze.BrazeBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
ステップ3: Braze の位置情報収集機能を有効にする
まだ Braze の位置情報収集機能を有効にしていない場合は、com_braze_enable_location_collection
を含むようにbraze.xml
ファイルを更新し、その値がtrue
に設定されていることを確認します。
1
<bool name="com_braze_enable_location_collection">true</bool>
Braze Android SDK バージョン3.6.0から、Braze の位置情報収集機能はデフォルトで無効になっています。
Braze のジオフェンスは、Braze の位置情報収集機能が有効になっている場合に有効になります。デフォルトの位置情報収集機能をオプトアウトしながらも、ジオフェンスを使用したい場合は、com_braze_enable_location_collection
の値とは別に、braze.xml
のキーcom_braze_geofences_enabled
の値をtrue
に設定することで、選択的に有効にすることができます。
1
<bool name="com_braze_geofences_enabled">true</bool>
ステップ4: エンドユーザーから位置情報の許可を得る
Android M 以降のバージョンでは、位置情報を収集したりジオフェンスを登録したりする前に、エンドユーザーに位置情報の許可を求める必要があります。
ユーザーがアプリに位置情報の許可を付与したときに Braze に通知するために、以下の呼び出しを追加します。
1
Braze.getInstance(context).requestLocationInitialization();
1
Braze.getInstance(context).requestLocationInitialization()
これにより、SDK は Braze サーバーにジオフェンスを要求し、ジオフェンスの追跡を初期化します。
実装例については、RuntimePermissionUtils.java
を参照してください。
```java public class RuntimePermissionUtils { private static final String TAG = BrazeLogger.getBrazeLogTag(RuntimePermissionUtils.class); public static final int DROIDBOY_PERMISSION_LOCATION = 40;
public static void handleOnRequestPermissionsResult(Context context, int requestCode, int[] grantResults) { switch (requestCode) { case DROIDBOY_PERMISSION_LOCATION: // In Android Q, we require both FINE and BACKGROUND location permissions. Both // are requested simultaneously. if (areAllPermissionsGranted(grantResults)) { Log.i(TAG, “Required location permissions granted.”); Toast.makeText(context, “Required location permissions granted.”, Toast.LENGTH_SHORT).show(); Braze.getInstance(context).requestLocationInitialization(); } else { Log.i(TAG, “Required location permissions NOT granted.”); Toast.makeText(context, “Required location permissions NOT granted.”, Toast.LENGTH_SHORT).show(); } break; default: break; } }
private static boolean areAllPermissionsGranted(int[] grantResults) { for (int grantResult : grantResults) { if (grantResult != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } } ```
```kotlin object RuntimePermissionUtils { private val TAG = BrazeLogger.getBrazeLogTag(RuntimePermissionUtils::class.java!!) val DROIDBOY_PERMISSION_LOCATION = 40
fun handleOnRequestPermissionsResult(context:Context, requestCode:Int, grantResults:IntArray) { when (requestCode) { DROIDBOY_PERMISSION_LOCATION -> // In Android Q, we require both FINE and BACKGROUND location permissions. Both // are requested simultaneously. if (areAllPermissionsGranted(grantResults)) { Log.i(TAG, “Required location permissions granted.”) Toast.makeText(context, “Required location permissions granted.”, Toast.LENGTH_SHORT).show() Braze.getInstance(context).requestLocationInitialization() } else { Log.i(TAG, “Required location permissions NOT granted.”) Toast.makeText(context, “Required location permissions NOT granted.”, Toast.LENGTH_SHORT).show() } else -> { } } }
private fun areAllPermissionsGranted(grantResults:IntArray):Boolean { for (grantResult in grantResults) { if (grantResult != PackageManager.PERMISSION_GRANTED) { return false } } return true } } ```
先のサンプルコードの使用は、以下の方法で行います。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
boolean hasAllPermissions = PermissionUtils.hasPermission(getApplicationContext(), Manifest.permission.ACCESS_BACKGROUND_LOCATION)
&& PermissionUtils.hasPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);
if (!hasAllPermissions) {
// Request both BACKGROUND and FINE location permissions
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION},
RuntimePermissionUtils.DROIDBOY_PERMISSION_LOCATION);
}
} else {
if (!PermissionUtils.hasPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)) {
// Request only FINE location permission
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
RuntimePermissionUtils.DROIDBOY_PERMISSION_LOCATION);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val hasAllPermissions = PermissionUtils.hasPermission(applicationContext, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
&& PermissionUtils.hasPermission(applicationContext, Manifest.permission.ACCESS_FINE_LOCATION)
if (!hasAllPermissions) {
// Request both BACKGROUND and FINE location permissions
requestPermissions(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION),
RuntimePermissionUtils.DROIDBOY_PERMISSION_LOCATION)
}
} else {
if (!PermissionUtils.hasPermission(applicationContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
// Request only FINE location permission
requestPermissions(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
RuntimePermissionUtils.DROIDBOY_PERMISSION_LOCATION)
}
}
}
ステップ5: ダッシュボードでジオフェンスを有効にする
Android では、1つのアプリに保存できるジオフェンスは100個までとなっています。Braze の位置情報製品は、利用可能であれば最大20のジオフェンススロットを使用します。アプリ内の他のジオフェンス関連機能への偶発的または不要な中断を防ぐため、位置情報ジオフェンスはダッシュボード上で個々のアプリに対して有効にする必要があります。
Braze 位置情報製品が正しく動作するには、アプリが利用可能なジオフェンススポットをすべて使用していないことを確認する必要があります。
ロケーションページからジオフェンスを有効にする
設定ページからジオフェンスを有効にする
ステップ6: ジオフェンスの更新を手動でリクエストする (オプション)
デフォルトでは、Braze は自動的にデバイスの位置情報を取得し、その取得した位置情報に基づいてジオフェンスを要求します。しかし、代わりに近接する Braze ジオフェンスを取得するために使用される GPS 座標を手動で指定することもできます。手動で Braze ジオフェンスをリクエストするには、自動 Braze ジオフェンスリクエストを無効にし、リクエスト用に GPS 座標を指定する必要があります。
パート1: 自動ジオフェンスリクエストを無効にする
自動 Braze ジオフェンスリクエストは、com_braze_automatic_geofence_requests_enabled
をfalse
に設定することで、braze.xml
ファイルで無効にすることができます。
1
<bool name="com_braze_automatic_geofence_requests_enabled">false</bool>
これはさらに、ランタイム時に以下の方法で行うことができます。
1
2
3
BrazeConfig.Builder brazeConfigBuilder = new BrazeConfig.Builder()
.setAutomaticGeofenceRequestsEnabled(false);
Braze.configure(getApplicationContext(), brazeConfigBuilder.build());
1
2
3
val brazeConfigBuilder = BrazeConfig.Builder()
.setAutomaticGeofenceRequestsEnabled(false)
Braze.configure(applicationContext, brazeConfigBuilder.build())
パート2: GPS 座標で Braze のジオフェンスを手動でリクエストする
Braze のジオフェンスは、requestGeofences()
メソッドを使用して手動でリクエストします。
1
Braze.getInstance(getApplicationContext()).requestGeofences(latitude, longitude);
1
Braze.getInstance(applicationContext).requestGeofences(33.078947, -116.601356)
ジオフェンスは、SDK により自動的に、またはこのメソッドにより手動で、セッションごとに一度だけリクエストできます。
プッシュ同期
Braze では、バックグラウンドプッシュを使用してジオフェンスをデバイスに同期します。ほとんどの場合、この機能はアプリ側でのさらなる統合を必要としないため、コードの変更を伴いません。
しかし、アプリケーションが停止している場合にバックグラウンドプッシュを受信すると、バックグラウンドで起動し、そのApplication.onCreate()
メソッドが呼び出されることに注意してください。カスタムのApplication.onCreate()
実装がある場合は、自動サーバーコールやバックグラウンドプッシュでトリガーしないアクションを延期する必要があります。