Unity에서 Firebase Messaging 사용해보기
프로그래밍 2017. 8. 2. 10:36
사전준비
유니티 코드
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 33 | public void Start() { Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived; Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived; } public void OnTokenReceived( object sender, Firebase.Messaging.TokenReceivedEventArgs token) { Debug.Log( "Received Registration Token: " + token.Token); } public void OnMessageReceived( object sender, Firebase.Messaging.MessageReceivedEventArgs e) { Debug.Log( "Received a new message from: " + e.Message.From); var notification = e.Message.Notification; if (notification != null ) { Debug.Log( "title: " + notification.Title); Debug.Log( "body: " + notification.Body); } if (e.Message.From.Length > 0) Debug.Log( "from: " + e.Message.From); if (e.Message.Data.Count > 0) { Debug.Log( "data:" ); foreach (System.Collections.Generic.KeyValuePair< string , string > iter in e.Message.Data) { Debug.Log( " " + iter.Key + ": " + iter.Value); } } } |
테스트 결과
메시지를 보내기 전에, 앱을 최초 한번은 실행해야
이후부터 앱이 종료되던 샐행 중이던 상관없이 메시지를 받을 수 있다.
앱을 실행하면, 토큰 값을 얻을 수 있으며, 특정 단말기에 보내기 위에선 토큰을 알아야 한다.
1 2 3 | 08-04 10:58:57.467: I /Unity (8972): Received Registration Token: fnQUKrW0hFg:APA91bGd2dsCEhxWFvOUHJDnjFD2mSZ7vy4wVeR9ysAzuNM-OAv67nlnpTJ1i8yn-gRUrvN241QCuwMOa5O4JT646NVmmVm6gC-Dt_kcVg3tAusbcAL8GEZxeuo9eXqo7f-1gCglwFTa 08-04 10:58:57.467: I /Unity (8972): 08-04 10:58:57.467: I /Unity (8972): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) |
Firebase 콘솔의 Notification에서 메시지를 보낼 수 있다.
테스트 메시지 보내기
데이터도 추가해서 보내기
메시지 보내기 결과
앱 실행 중
폰 푸시메시지함에는 추가되지 않고,
OnMessageReceived가 호출되어, 서버가 보낸 메시지를 얻을 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 08-04 11:16:28.977: I /Unity (8972): Received a new message from: 08-04 11:16:28.977: I /Unity (8972): 08-04 11:16:28.977: I /Unity (8972): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:16:28.987: I /Unity (8972): title: test title 08-04 11:16:28.987: I /Unity (8972): 08-04 11:16:28.987: I /Unity (8972): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:16:28.987: I /Unity (8972): body: test body 08-04 11:16:28.987: I /Unity (8972): 08-04 11:16:28.987: I /Unity (8972): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:16:29.007: I /Unity (8972): data: 08-04 11:16:29.007: I /Unity (8972): 08-04 11:16:29.007: I /Unity (8972): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:16:29.027: I /Unity (8972): test_key1: 1212 08-04 11:16:29.027: I /Unity (8972): 08-04 11:16:29.027: I /Unity (8972): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:16:29.027: I /Unity (8972): test_key2: test_value2 08-04 11:16:29.027: I /Unity (8972): 08-04 11:16:29.027: I /Unity (8972): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) |
앱 실행 중이 아닌 경우
폰 푸시메시지함에 추가된다.
도착한 메시지를 클릭해 앱이 실행되면,
OnMessageReceived가 호출되고, 서버가 보낸 메시지를 얻을 수 없지만,
데이터는 얻을 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 08-04 11:17:53.527: I /Unity (9130): Received Registration Token: fnQUKrW0hFg:APA91bGd2dsCEhxWFvOUHJDnjFD2mSZ7vy4wVeR9ysAzuNM-OAv67nlnpTJ1i8yn-gRUrvN241QCuwMOa5O4JT646NVmmVm6gC-Dt_kcVg3tAusbcAL8GEZxeuo9eXqo7f-1gCglwFTa 08-04 11:17:53.527: I /Unity (9130): 08-04 11:17:53.527: I /Unity (9130): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:17:53.537: I /Unity (9130): Received a new message from: 08-04 11:17:53.537: I /Unity (9130): 08-04 11:17:53.537: I /Unity (9130): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:17:53.537: I /Unity (9130): data: 08-04 11:17:53.537: I /Unity (9130): 08-04 11:17:53.537: I /Unity (9130): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:17:53.557: I /Unity (9130): test_key1: 1212 08-04 11:17:53.557: I /Unity (9130): 08-04 11:17:53.557: I /Unity (9130): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) 08-04 11:17:53.557: I /Unity (9130): test_key2: test_value2 08-04 11:17:53.557: I /Unity (9130): 08-04 11:17:53.557: I /Unity (9130): (Filename: . /artifacts/generated/common/runtime/DebugBindings .gen.cpp Line: 51) |
도착한 메시지를 클릭하지 않고, 앱을 실행 한 경우
OnMessageReceived는 호출되지 않는다.
Firebase 콘솔에서 보내는 것 말고, URL로 메시지 보내기
1 | curl --header "Authorization: key=<Server Key>" --header "Content-Type: application/json" https: //android .googleapis.com /gcm/send -d '{"notification":{"title":"Hi","body":"Hello from the Cloud"},"data":{"score":"lots"},"to":"<Registration Token>"}' |
서버키는 Firebase 콘솔의 프로젝트 설정에 있다.
메시지가 정상적으로 도착했다.