๊ด€๋ฆฌ ๋ฉ”๋‰ด

Welcome! Everything is fine.

[Android] FCM ์ด์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ ๋ณธ๋ฌธ

Android

[Android] FCM ์ด์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ

๊ฐœ๋ฐœ๊ณฐ๋ฐœ 2023. 7. 15.
728x90

๐Ÿ“Œ FCM(Firebase Cloud Messaging) ์ด์šฉํ•ด ํ‘ธ์‹œ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ

Tools - Firebase ์— ๋“ค์–ด๊ฐ€์„œ Cloud Messaging - Set up Firebase Cloud Messaging์„ ํด๋ฆญํ•œ๋‹ค.

๊ทธ๋Ÿผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด์•ผํ•  ๊ฒƒ๋“ค์ด ๋‚˜์˜จ๋‹ค.

Firebase์™€ ํ”„๋กœ์ ํŠธ๋Š” ์—ฐ๊ฒฐ์ด ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ์ƒ๋žตํ•˜๊ณ , Add FCM to your app ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฐฝ์ด ๋œฌ๋‹ค. Accept Changes๋ฅผ ๋ˆ„๋ฅด๊ณ  ์ž ์‹œ ๊ธฐ๋‹ค๋ฆฌ๋ฉด ์™„๋ฃŒ๋œ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด MainActivity์˜ OnCreate() ๋ฐ‘์— ๋„ฃ๋Š”๋‹ค. ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์ฐธ๊ณ ๋ฌธ์„œ์—์„œ 'ํ˜„์žฌ ๋“ฑ๋ก ํ† ํฐ ๊ฐ€์ ธ์˜ค๊ธฐ'์—์„œ๋„ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

FirebaseMessaging.getInstance().getToken()
       .addOnCompleteListener(new OnCompleteListener<String>() {
           @Override
           public void onComplete(@NonNull Task<String> task) {
               if (!task.isSuccessful()) {
                   Log.w("FirebaseMessaging", "Fetching FCM registration token failed", task.getException());
                   return;
               }

               // Get new FCM registration token
               String token = task.getResult();
               Log.d("FirebaseMessaging", "Your device registration token is " + token);
               Toast.makeText(getApplicationContext(), "Your device registration token is " + token, Toast.LENGTH_SHORT).show();
           }
       });

์‹คํ–‰ํ•˜๋ฉด ๋กœ๊ทธ์ฐฝ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด๋‹น ๊ธฐ๊ธฐ์˜ ํ† ํฐ์ด ๋œฌ๋‹ค. ์ด ํ† ํฐ์€ ๋‚˜์ค‘์— ์‚ฌ์šฉํ•ด์•ผํ•˜๋ฏ€๋กœ ๋ณต์‚ฌํ•ด๋‘”๋‹ค.

์ด์ œ ํŒŒ์ด๋ฒ ์ด์Šค ์ฝ˜์†”๋กœ ๊ฐ€์„œ ๋ชจ๋“  ์ œํ’ˆ - Cloud Messaging์„ ํด๋ฆญํ•œ๋‹ค.

๋“ค์–ด๊ฐ€์„œ '์ฒซ๋ฒˆ์งธ ์บ ํŽ˜์ธ ๋งŒ๋“ค๊ธฐ'๋ฅผ ํด๋ฆญํ•œ๋‹ค.

๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฐฝ์ด ๋œจ๋Š”๋ฐ, ํ•˜๋‚˜ ์„ ํƒํ•œ ํ›„ '๋งŒ๋“ค๊ธฐ'๋ฅผ ๋ˆ„๋ฅธ๋‹ค. ๋‚˜๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์•Œ๋ฆผ์„ ๋ฐ›๊ธฐ๋ฅผ ์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ๋ฒˆ์งธ๋ฅผ ์„ ํƒํ•˜์˜€๋‹ค.

์•Œ๋ฆผ ์ œ๋ชฉ๊ณผ ๋ฉ”์‹œ์ง€๋ฅผ ์ž‘์„ฑํ•˜๊ณ  'ํ…Œ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ์ „์†ก'์„ ํด๋ฆญํ•œ๋‹ค.

์ด๋•Œ ์•„๊นŒ ์–ป์€ ํ† ํฐ์ด ํ•„์š”ํ•œ๋ฐ, ๋ณต์‚ฌํ•œ ํ† ํฐ์„ ๋ถ™์—ฌ๋„ฃ๊ธฐํ•˜๊ณ  ํ…Œ์ŠคํŠธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ๋‹ค.

๊ทธ๋Ÿผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์—์„œ ํ…Œ์ŠคํŠธ ์•Œ๋ฆผ์ด ์ •์ƒ์ ์œผ๋กœ ์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์•Œ๋ฆผ์˜ ๋™์ž‘์€ ์ด๋ ‡๊ฒŒ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์ธ์ง€, ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. ๋ฐฑ๊ทธ๋ผ์šด๋“œ๊ฐ€ ์•„๋‹Œ ํฌ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์—์„œ ์•Œ๋ฆผ์„ ์ˆ˜์‹ ํ•˜๋ ค๋ฉด onMessageReceived ์ฝœ๋ฐฑ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

ํฌ๊ทธ๋ผ์šด๋“œ์—์„œ ์•Œ๋ฆผ์„ ์ˆ˜์‹ ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. FirebaseMessagingService๋ฅผ extendsํ•œ๋‹ค.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

}

๋‹ค์Œ์œผ๋กœ AndroidMenifest์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค. <application>ํƒœ๊ทธ ์•ˆ์— ๋ถ™์—ฌ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

<service
    android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ onMessageReceived ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•œ๋‹ค.

 

Android ์•ฑ์—์„œ ๋ฉ”์‹œ์ง€ ์ˆ˜์‹   |  Firebase ํด๋ผ์šฐ๋“œ ๋ฉ”์‹œ์ง•

Google I/O 2023์—์„œ Firebase์˜ ์ฃผ์š” ์†Œ์‹์„ ํ™•์ธํ•˜์„ธ์š”. ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ ์˜๊ฒฌ ๋ณด๋‚ด๊ธฐ Android ์•ฑ์—์„œ ๋ฉ”์‹œ์ง€ ์ˆ˜์‹  ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ˜ํ…์ธ ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜์„ธ์š”. F

firebase.google.com