3. 左上のパネルで、[プロジェクトの概要]の横にある[設定アイコン]をクリックします。
4. [プロジェクトの設定]をクリックします。
5. [プロジェクトの設定]で、[サービスアカウント]に移動します。
6. [新しい秘密鍵を生成]をクリックします。
7. これにより、秘密鍵を含むサービスアカウントのJSONファイルが生成されます。
9. FCMコンソールの[Cloud Messaging]で、Firebase Cloud Messaging(FCM)API(V1)が有効になっていることを確認します。
10. 無効になっている場合は、Firebase Cloud Messaging API(V1)の横にある[3点アイコン]をクリックし、[Google CloudコンソールでAPIを管理]を選択して、[有効にする]をクリックします。
AndroidのクライアントアプリでFirebase Cloud Messaging(FCM)を設定するには
FCMの設定が完了したら、デバイスで通知の受信を開始するため、次の手順を実行します。
1. 次のコードをアプリレベルのマニフェストファイルに追加します。これにより、Firebaseから通知を受信できるようになります。
- <service android:name='.NotificationFCMReceiver'
android:exported='false'>
<intent-filter>
<action android:name='com.google.firebase.MESSAGING_EVENT' />
</intent-filter>
</service>
2. FirebaseMessagingServiceを継承するクラスをアプリのプロジェクトに追加します。使用する言語に応じて、次のコードをご参照ください。
JAVAの場合は、次のコードを使用します。
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
- import com.zoho.pagesense.android.PSNotification;
- public class NotificationFCMReceiver extends FirebaseMessagingService {
- @Override
- public void onNewToken(@NonNull String s) {
- super.onNewToken(s);
- PSNotification.sendDeviceToken(s);
- }
- @Override
- public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
- super.onMessageReceived(remoteMessage);
- if(PSNotification.isFromPageSensePlatform(remoteMessage.getData())) {
PSNotification.showNotification(remoteMessage.getData(),
- com.zoho.pagesense.android.R.drawable.notification_bg);
- }
- }
- }
KOTLINの場合は、次のコードを使用します。
- import com.google.firebase.messaging.FirebaseMessagingService
- import com.google.firebase.messaging.RemoteMessage
- import com.zoho.pagesense.android.PSNotification
- class NotificationFCMReceiver : FirebaseMessagingService() {
- override fun onNewToken(s: String) {
- super.onNewToken(s)
- PSNotification.sendDeviceToken(s)
- }
- override fun onMessageReceived(remoteMessage: RemoteMessage) {
- super.onMessageReceived(remoteMessage)
- if (PSNotification.isFromPageSensePlatform(remoteMessage.data)) {
- PSNotification.showNotification(remoteMessage.data,
- com.zoho.pagesense.android.R.drawable.notification_bg)
- }
- }
- }
- onNewTokenは、デバイストークンが生成されたときに呼び出されます。トークンをZoho PageSenseに送信するには、API PSNotification.sendDeviceToken(s) を使用します。
- onMessageReceivedは、通知を受信したときに呼び出されます。ここでは、通知がPageSenseから送信されたものかどうかを確認するため、PSNotification.isFromPageSensePlatform(remoteMessage.getData()) APIを呼び出す必要があります。その後、データと通知アイコンを渡して、API PSNotification.showNotification(remoteMessage.getData(), com.zoho.pagesense.android.R.drawable.notification_bg);を呼び出します。
実行時に必要な権限
Android 12以降では、通知内の通話ボタンとスヌーズボタンを処理するために、次の権限を有効にする必要があります。
- <uses-permission android:name='android.permission.CALL_PHONE'/>
- <uses-permission android:name='android.permission.SCHEDULE_EXACT_ALARM' />