Repro - Mobile Analytics for growth
English
リソース
Growth Hack Journal
アカウント登録 管理画面に戻る
  • 動作環境
  • 管理画面ガイド
  • 開発ガイド
    • アカウント作成
    • iOS/Android SDK
      • 導入
      • セッション・ライフサイクル
      • ユーザーID
      • デバイスID
      • ユーザープロフィール
      • イベントトラッキング
      • プッシュ通知
        • APNs証明書の設定 (iOS)
        • FCMの設定 (Android)
        • iOS
        • Android
          • プッシュ通知の設定
          • Firebase SDKを導入する
          • AndroidManifest.xml ファイルを編集する
          • Registration IDをReproに送信
          • オプション:プッシュ通知受信時の動作をカスタマイズする
        • Unity
        • Cordova
        • Monaca
        • Cocos2d-x
        • React Native
        • Flutter
      • ニュースフィード
      • アプリ内メッセージ
      • UXオプティマイザー(ベータ版)
      • WebView
      • オプトアウト機能
      • Adjustで取得したアトリビューションデータをReproにセットする
      • AppsFlyerで取得したアトリビューションデータをReproにセットする
      • ログレベル
      • 検証方法
    • Web
    • オーディエンスAPI
    • オーディエンスインポート(β)
    • プッシュAPI
    • ユーザープロフィールAPI
    • ユーザープロフィールバルクインポートAPI
  • リリースノート
  • FAQ
java

プッシュ通知(Android)¶

プッシュ通知の設定¶

FCMの設定 (Android) を参照し、設定してください。

Firebase SDKを導入する¶

FCMの設定 (Android) でダウンロードした google-services.json をアプリモジュールのディレクトリにコピーし、プロジェクトのbuild.gradleに以下を追加してください。

buildscript {
    ...
    dependencies {
        ...
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

allprojects {
    ...
    repositories {
        ...
        google()
    }
}

アプリのbuild.gradleに以下を追加してください。

dependencies {
    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
}

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

警告

firebase-messagingのバージョン17.1.0以上を指定してください。17.1.0未満は、Repro SDKでRegistration IDを取得できません。

注釈

Gradle 3.4 未満のバージョンの場合は 'implementation' が利用できません。 'compile'を利用してください。

AndroidManifest.xml ファイルを編集する¶

Receiverの登録¶

以下のXML中の "YOUR_PACKAGE_NAME" をアプリケーションのパッケージ名に置き換え、AndroidManifest.xmlの <application> タグの中に追加してください。

<receiver
    android:name="io.repro.android.ReproReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="YOUR_PACKAGE_NAME" />
    </intent-filter>
</receiver>

通知チャンネルの設定¶

Android Oにて、ユーザーが通知を管理できるように通知チャンネルが導入されました。

Repro SDKで利用する通知チャンネルの設定を行うには、「ID」「名前」「ユーザー向けの説明」「badgeの表示有無」を指定する必要があります。 以下のXML中の value, resource を設定したい値に書き換え、AndroidManifest.xmlの <application> タグの中に追加してください。 IDと名前の指定は必須、その他は省略可能です。「ユーザー向けの説明」省略時は空文字列、「badgeの表示有無」省略時はbadgeの表示有りとなります。

警告

  • アプリのtargetSDKVersionが26以上かつAndroid O以降の機種で動作させる場合、IDと名前が指定されていなければSDKはReproからのプッシュ通知を表示いたしませんのでご注意ください。
  • ChannelId に指定する値は、整数や小数ではなく test ID のような文字列を指定してください。文字列以外の値を指定するとチャンネルIDの指定がうまく動作せず、プッシュ通知が表示されません。
<meta-data
    android:name="io.repro.android.PushNotification.ChannelId"
    android:value="YOUR_CHANNEL_ID">
</meta-data>

<meta-data
    android:name="io.repro.android.PushNotification.ChannelName"
    android:resource="@string/YOUR_CHANNEL_NAME">
</meta-data>

<meta-data
    android:name="io.repro.android.PushNotification.ChannelDescription"
    android:resource="@string/YOUR_CHANNEL_DESCRIPTION">
</meta-data>

<meta-data
    android:name="io.repro.android.PushNotification.ShowBadge"
    android:value="true">
</meta-data>

指定されたIDの通知チャンネルが存在しない場合はAndroidManifest.xmlの指定を基にSDKが自動で作成し、指定されたIDの通知チャンネルが存在する場合は既存のものを利用します。

既存のものを利用する場合、SDKは「名前」と「ユーザー向けの説明」を更新します。

また、アプリのtargetSDKVersionが25以下の場合、あるいは、Android O未満の機種の場合は、SDKは上記の通知チャンネルに関する設定を無視します。

注釈

通知チャンネルはAndroid Oで導入されたAndroidの標準機能です。通知チャンネルの詳細は こちら をご覧ください。

アイコンと背景色のカスタマイズ¶

Android5.0以降の機種で、通知エリアに表示されるアイコンとその背景色をカスタマイズする場合は、以下のXMLをAndroidManifest.xmlの <application> タグの中に追加してください。Android5.0未満の機種では以下の設定は無視され、通知エリアにはアプリケーションのアイコンおよびシステムデフォルトの背景色が使用されます。

<meta-data
    android:name="io.repro.android.PushNotification.SmallIcon"
    android:resource="@drawable/YOUR_ICON_ID">
</meta-data>

<meta-data
    android:name="io.repro.android.PushNotification.AccentColor"
    android:resource="@color/YOUR_COLOR_ID">
</meta-data>

Registration IDをReproに送信¶

Reproの setup() を呼び出した直後に、 Repro.enablePushNotification を呼び出してください。

 import io.repro.android.Repro;

 public class MyApplication extends Application {

     @Override
     public void onCreate() {
         super.onCreate();

         ...

         Repro.setup(this, "YOUR_APP_TOKEN");
         Repro.enablePushNotification();

         ...
     }
 }

FirebaseMessagingService を継承したクラスの onNewToken にて Repro.setPushRegistrationID を呼び出してください。

 import io.repro.android.Repro;

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
     ...
     @Override
     public void onNewToken(String token) {
         Repro.setPushRegistrationID(token);
     }
     ...
 }

以下をAndroidManifest.xmlの <application> タグの中に追加してください。

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

注釈

AndroidManifest.xmlに複数のFirebaseMessagingServiceを追加しても、その内の一つのFirebaseMessagingServiceしか動作しません。

このため、既にFirebaseMessagingServiceを継承したクラスを実装している場合は、そのクラスの onNewToken にて Repro.setPushRegistrationID を呼び出すようにし、新たにFirebaseMessagingServiceを継承したクラスを作成しないでくだだい。

上記の実装が終わったら、 プッシュ通知を作成する をご覧ください。

オプション:プッシュ通知受信時の動作をカスタマイズする¶

プッシュ通知受信時の動作をカスタマイズしたい場合は、FirebaseMessagingServiceのonMessageReceivedメソッドを実装してください。

FirebaseMessagingServiceのonMessageReceivedを実装する¶

onMessageReceived をオーバーライドして、以下の3つのメソッドを呼び出してください。

  • Repro.applicationShouldHandlePushNotification を呼び出して、受信したメッセージをアプリケーションで処理すべきか判定してください。このメソッドが false を返す場合、そのメッセージはSDKで処理されます。
  • Repro.isAlreadyReceivedPushNotification を呼び出して、受信済みのメッセージか判定してください。アプリケーションのアンインストール・再インストールを繰り返した場合、重複したメッセージが届く場合があります。
  • Repro.markPushNotificationReceived を呼び出して、受信済みのメッセージであることを記録してください。

注釈

スタンダード形式の通知を受信するためには ReproReceiver が必要です。AndroidManifest.xmlに登録されている ReproReceiver を削除しないでください。

@Override
public void onMessageReceived(RemoteMessage message) {
    Map<String, String> data = message.getData();

    // check whether the application should handle this push notification.
    if (!Repro.applicationShouldHandlePushNotification(this, data)) {
        Log.d(TAG, "Ignore push notification: it will be handled by SDK: " + data.toString());
        return;
    }

    // check whether this push notification is already received.
    if (Repro.isAlreadyReceivedPushNotification(this, data)) {
        Log.d(TAG, "Ignore push notification: it is already received: " + data.toString());
        return;
    }

    // mark this push notification as "received".
    Log.d(TAG, "Mark push notification as received: " + data.toString());
    Repro.markPushNotificationReceived(this, data);

    // Implement procedures unique to the application here
    ...
}

開封された通知をトラッキングする¶

スタンダード形式のメッセージの場合はSDKが自動で開封をトラッキングします。JSON形式のメッセージの場合は、アプリケーションから Repro.trackNotificationOpened を呼び出してトラッキングする必要があります。

以下では、JSON形式のメッセージからActivityを起動する通知を作成し、起動されたActivity側で Repro.trackNotificationOpened を呼び出すサンプルを示します。

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MessagingService";
    private static final String CHANNEL_ID = "custom-firebase-messaging-service-channel";

    private final Random mRandom = new Random();

    @Override
    public void onNewToken(String token) {
        Repro.setPushRegistrationID(token);
    }

    @Override
    public void onMessageReceived(RemoteMessage message) {
        final int identifier = mRandom.nextInt();
        Map<String, String> data = message.getData();

        // check whether the application should handle this push notification.
        if (!Repro.applicationShouldHandlePushNotification(this, data)) {
            Log.d(TAG, "Ignore push notification: it will be handled by SDK: " + data.toString());
            return;
        }

        // check whether this push notification is already received.
        if (Repro.isAlreadyReceivedPushNotification(this, data)) {
            Log.d(TAG, "Ignore push notification: it is already received: " + data.toString());
            return;
        }

        // mark this push notification as "received".
        Log.d(TAG, "Mark push notification as received: " + data.toString());
        Repro.markPushNotificationReceived(this, data);

        String messageText;
        if (data.containsKey("message")) {
            messageText = data.get("message");
        } else {
            messageText = "default message";
        }

        final Notification.Builder builder = new Notification.Builder(this)
                .setContentTitle(this.getResources().getString(R.string.app_name))
                .setContentText(messageText)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createOrUpdateChannel();
            builder.setChannelId(CHANNEL_ID);
        }

        Intent resultIntent = new Intent(this, SomeActivity.class);

        String notificationIdKey = data.get(io.repro.android.ReproReceiver.NOTIFICATION_ID_KEY);
        if (notificationIdKey != null) {
            resultIntent.putExtra(io.repro.android.ReproReceiver.NOTIFICATION_ID_KEY, notificationIdKey);
        }

        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, identifier, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(resultPendingIntent);

        final NotificationManager notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(identifier, builder.getNotification());
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createOrUpdateChannel() {
        NotificationChannel newChannel = new NotificationChannel(CHANNEL_ID, "Custom Channel", NotificationManager.IMPORTANCE_DEFAULT);
        newChannel.setDescription("");
        newChannel.setShowBadge(true);

        // create or update the Notification channel
        final NotificationManager notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(newChannel);
    }
}

起動されたActivity側では、上述の resultIntent にセットしたExtrasからプッシュ通知IDを取得し、 onResume で Repro.trackNotificationOpened にセットしてください。なおアクティビティのローンチモードが singleTop の場合は、 onNewIntent で同様にプッシュ通知IDをセットしてください。

public class SomeActivity extends Activity {

  @Override
  protected void onResume() {
      super.onResume();

      Intent intent = getIntent();
      Bundle extras = intent.getExtras();
      if (extras != null && extras.containsKey(io.repro.android.ReproReceiver.NOTIFICATION_ID_KEY)) {
          Repro.trackNotificationOpened(extras.getString(io.repro.android.ReproReceiver.NOTIFICATION_ID_KEY));
      }
  }

  @Override
  protected void onNewIntent(Intent intent) {
      super.onNewIntent(intent);

      Bundle extras = intent.getExtras();
      if (extras != null && extras.containsKey(io.repro.android.ReproReceiver.NOTIFICATION_ID_KEY)) {
          Repro.trackNotificationOpened(extras.getString(io.repro.android.ReproReceiver.NOTIFICATION_ID_KEY));
      }

      this.setIntent(intent);
  }

注釈

  • プッシュ通知IDは、受信したメッセージのdataから io.repro.android.ReproReceiver.NOTIFICATION_ID_KEY というキーで取得できます。
  • « プッシュ通知(iOS)
  • プッシュ通知 (Unity) »

About Us Careers Terms of Service Privacy Policy Cookie Policy

© 2020 Repro Inc.