푸시 알림 유니티 (Notification Unity)

2021. 12. 24. 10:05기술/네트워크

반응형

파이어베이스 푸시 알림 (Firebase Cloud Messaging)

 

STEP ANDROID

별로 어렵지는 않으나 경험담 공유합니다. 가끔 뭔가 안 되면 당황할 때가 있어서 ㅋ

iOS 특이사항은 APN 인증서(Apple push notification service)를 업로드해야한다는 것 외에는 비슷하다.

 

1. 파이어베이스에서 프로젝트를 만들고 만들어져 있으면 com.company.appname 패키지명만 맞추면 된다.

2. google-services.json 을 다운로드 해서 Assets 폴더에 추가한다.

3. FirebaseMessaging.unitypackage 를 임포트 한다. (2019 버전 이상에서는 dotnet4 폴더에 있는)

4. 코드 작성

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Firebase;
using Firebase.Messaging;
using UnityEngine;
using UnityEngine.UI;
 
public class FireBaseCloudMessaging : MonoBehaviour
{
    [SerializeField] Text 텍스트Token = null;
    [SerializeField] Text 텍스트Msg = null;
 
    public void Start()
    {
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
        {
            if (task.Result == DependencyStatus.Available)
            {
                FirebaseMessaging.TokenReceived += OnTokenReceived;
                FirebaseMessaging.MessageReceived += OnMessageReceived;
            }
        });
    }
 
    void OnTokenReceived(object sender, TokenReceivedEventArgs e)
    {
        if (e != null)
        {
            Debug.LogFormat("[FIREBASE] Token : {0}", e.Token);
            if (텍스트Token != null)
                텍스트Token.text = e.Token;
        }
    }
 
    void OnMessageReceived(object sender, MessageReceivedEventArgs e)
    {
        if (e != null && e.Message != null &&
            e.Message.Notification != null)
        {
            string msg = string.Format("[FIREBASE] From: {0}, Title: {1}, Text: {2}",
                e.Message.From,
                e.Message.Notification.Title,
                e.Message.Notification.Body);
            if (텍스트Msg != null)
                텍스트Msg.text = msg;
            Debug.LogFormat("[FIREBASE] Token : {0}", msg);
        }
    }
}
 
cs

5. 패키지명 확인하고 스마트폰 기기에 빌드

6. 앱 실행하면서 Token이 잘 오는지 확인

7. 파이어베이스 Cloud Messaging에서 실제 메시지 전송

8. 앱에 Message가 잘 오는지 확인

9. 앱을 백그라운드로 실행해두고 7번 다시 테스트

10. 스마트폰 상단에 알림바가 동작하는지 테스트

 

참조

Unity로 Firebase 클라우드 메시징 클라이언트 앱 설정  |  Firebase Documentation (google.com)

 

Unity로 Firebase 클라우드 메시징 클라이언트 앱 설정  |  Firebase Documentation

의견 보내기 Unity로 Firebase 클라우드 메시징 클라이언트 앱 설정 Unity로 크로스 플랫폼 Firebase 클라우드 메시징 클라이언트 앱을 작성하려면 Firebase Cloud Messaging API를 사용하세요. Unity SDK는 Android

firebase.google.com

 

STEP iOS

1. 위 프로젝트를 SVN이나 GitHub를 통해 맥기기로 동기화

2. 패키지명 올바르게 수정 (Firebase에 설정한 것으로)

2. iOS를 위한 GoogleService-Info.plist 다운로드 > Assets 폴더로 이동

3. XCode로 빌드

4. 위 메뉴얼에서 7단계: 사용자 알림 프레임워크 추가, 8단계: 푸시 알림 사용 설정

 ㄴAutomatically manage signing 체크

 ㄴGeneral탭에서 UserNotifications.framework 라이브러리 추가

 ㄴSiging & Capabilities탭에서 Push Notifications, Background Modes 설정

5. pod install, pod update

6. 테스트 기기로 빌드 

 ㄴframework not found fblpromises 에러 
 ㄴ.xcodeproj 대신 .xcworkspace 파일을 사용

7. 파이어베이스에 Apple APN 등록

 ㄴApple Developer > Account APN 인증서 발급 받고 다운로드 (Windows X, Mac O)

 ㄴFirebase 프로젝트 설정에 보면 APN 인증서 업로드 부분이 있음

 ㄴ키 ID, 팀 ID 입력해서 인증서 업로드

8. APN 인증키만 등록돼 있어도 되는 듯

9. 파이어베이스에서 iOS로 메시지 보내기

10. 테스트 기기 > 앱 켜진 상태에서 메시지 확인, 앱 백그라운드 시 상단 메시지창 테스트 확인

APN 인증 키(.p8) 발급 및 업로드 | 핑거푸시 개발자 사이트(helloworld.fingerpush.com)

 

APN 인증 키(.p8) 발급 및 업로드

APN 인증 키(.p8) 발급 1. Apple Developer Member Center 사이트로 이동합니다. 2. Certificate, Identifiers & Profiles 을 클릭합니다. 3. Keys 메뉴로 이동 후 인증 키를 발급합니다. 인증 키는 최대 2개까지 생성할 수

helloworld.fingerpush.com

 

iOS) APNs :: 인증서 발급받는 방법 (p.12, pem) (tistory.com)

 

iOS) APNs :: 인증서 발급받는 방법 (p.12, pem)

안녕하세요 :) 소들입니다 오늘은 Push 인증서 갱신하는 법에 대해 알아볼 거예요!!! APNs가 모냐면 Apple Push Notification service 임 ㅎㅎ 앱에서 푸시를 받기 위해선 APNs란 놈을 발급받아야 하기 때문에

babbab2.tistory.com

 

로컬 알림

Unity-Technologies/NotificationsSamples: Sample project for Unity Notifications (github.com)

 

 

 

 


 

반응형