FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
// Log and toast
String msg = "FCM token:" + token;
binding.tvToken.setText(msg);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(@NonNull String token) {
// Get updated InstanceID token.
Log.d(TAG, "Refreshed token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
// sendRegistrationToServer(token);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
...
}
}
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
msgBody = remoteMessage.getNotification().getBody();
Log.d(TAG, "Message Notification Body: " + msgBody);
}
}
curl --header "Authorization: key=AIzaSyC3-Rz5MiJmWxBy78io0SG4HYHwPJbTsL0"
--header Content-Type:"application/json"
https://fcm.googleapis.com/fcm/send
-d "{ \"notification\": { \"title\": \“title here\", \"text\": \“message body here\" }, \"to\" : \"여기에 토큰이 들어가야 함\"}"