0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Android, JS】FCM, payload || notification 超個人的メモ

Posted at
const payload = {
        notification: {
          title: 'たいとる',
          body: 'めっせーじ'
        }
    };

onMessageReceived()で特に何もしなくても通知が表示されたケース
title, bodyなどのキーはおそらく変えられない

const payload = {
        data: {
          message: message,
          date: dateString
        }
    };

受信時の実装をしないと通知が出ないケース
dataの中のキーは自由らしい
Date型のモノは送信時にエラーになった
String限定で送れる模様

const response = await admin.messaging().sendToDevice(token, payload);

で送信
responseには何も入ってこないので不要

public class MyMessagingService extends FirebaseMessagingService {
    // onNewToken()必須らしい

    @Override
        public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
            Log.d("ろぐ", "payloadのdata: " + remoteMessage.getData());
            Log.d("ろぐ", "notificationのbody: " + remoteMessage.getNotification().getBody());
        }
}
<service
    android:name=".MyMessagingService"
    android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>
buildscript {

  repositories {
    google()
  }

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

allprojects {
  repositories {
    google()  // Google's Maven repository
  }
}
apply plugin: 'com.google.gms.google-services'
.
.
.
dependencies {
    implementation platform('com.google.firebase:firebase-bom:28.1.0')
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-analytics'
}
0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?