Unity

[FCM] Firebase 클라우드 메시징 클라이언트 구현하기 #2

브랑제리 2021. 4. 18. 23:13

# 유니티 샘플 프로젝트 생성

유니티에 신규 프로젝트를 생성합니다.

안드로이드 플랫폼으로 스위칭을 합니다.

 

패키지명은 firebase에 입력하였던 패키지명으로 입력합니다.

 

프로젝트 셋팅에 따라 dotnet3, dotnet4를 선택하여 firebase sdk를 에 FirebaseMessaging.unitypackage를 임포트 합니다.

project setting
firebase sdk import

임포트를 진행합니다.

 

google-serveices.json 도 유니티에 임포트합니다.

 

# 코드 구현

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

using UnityEngine;

using UnityEngine.UI;

 

public class NotificationReciver : MonoBehaviour

{

    [SerializeField] private Text _text;

 

    void Start()

    {

        Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;

        Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;

    }

 

    public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)

    {

        _text.text += "Received Registration Token: " + token.Token + "\n\n\n";

    }

 

    public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)

    {

        var notification = e.Message.Notification;

        if (notification != null)

        {

            _text.text += "Received a new message from: " + notification.Title + "\n" + notification.Body;

        }

        else

        {

            _text.text += "notification is null";

        }

    }

}

Colored by Color Scripter

cs

 

 

NotificationReciver의 Text에 참조를 걸어줍니다.

 

클라이언트 코드는 이것으로 테스트 할 마무리가 되었습니다.

반응형