๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ionic

๐ŸšŒ ionic fcm - android ๋ฒ„์ „

by frontChoi 2022. 10. 30.
๋ฐ˜์‘ํ˜•

๐Ÿ•น FCM

ํ•˜์ด๋ธŒ๋ฆฌ๋“œ์•ฑ์„ ๊ฐœ๋ฐœํ•˜๋Š” ์‚ฌ๋žŒ์ด๋ผ๋ฉด ๋ˆ„๊ตฌ๋‚˜ ๋‹ค fcm์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ํ•˜์ด๋ธŒ๋ฆฌ๋“œ๋ฅผ ํ•ด๋„ ํ”Œ๋žซํผ์ด 2๊ฐœ์ด๋‹ˆ android,ios ....

๊ทธ๋ฆฌํ•˜์—ฌ ์ด๋ฒˆ์—๋Š” ionic capacitor์—์„œ fcm์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„๋ณด์ž. fcm์€ android,ios ๋‘˜๋‹ค ์•Œ๋ฆผ์„ ๋ณด๋‚ด์ค„์ˆ˜์žˆ๋Š” ์•Œ๋ฆผ ํ”Œ๋žซํผ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ ๊ฒƒ๊ฐ™๋‹ค.

 

 ๐Ÿ“ž FCM ์„ธํŒ…ํ•˜๊ธฐ

์ฒ˜์Œ์—๋Š” FireBase์—๊ฐ€์„œ ์•ˆ๋“œ๋กœ์ด๋“œ ์„ธํŒ…์ด ํ•„์š”ํ•˜๋‹ค. ์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ํ”„๋กœ์ ํŠธ ์„ค์ •์„ ๋ˆŒ๋Ÿฌ์ฃผ์ž

๋‹ค์Œ์œผ๋กœ ์•ˆ๋“œ๋กœ์ด๋“œ๋ฅผ ํด๋ฆญํ•ด์ฃผ์ž

๊ทธ๋Ÿฌ๋ฉด์€ google-services.json์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

๋‹ค์Œ์œผ๋กœ๋Š” google-services.json ์„ ionic ํ”„๋กœ์ ํŠธ์— android < app ํด๋”์ชฝ์œผ๋กœ ์ด๋™์‹œํ‚ค์ž.

 

 

์œ„์ฒ˜๋Ÿผ ํ•˜๋ฉด FCM๊ด€๋ จ๋œ๊ฒƒ์€ ์„ธํŒ…์ด ์™„๋ฃŒ ๋˜์—ˆ๋‹ค.

 

 

 ๐Ÿ“‡  FCM module ์„ค์น˜

npm install @capacitor/push-notifications
ionic cap sync android

 

์œ„ ๋ชจ๋“ˆ ์„ค์น˜ ํ›„ android sync๋ฅผ ๋งž์ถฐ์ฃผ์ž.

 

 

๐Ÿ” FCM API ํ™œ์šฉํ•˜๊ธฐ

์ด 4๊ฐ€์ง€์˜ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ณผ ๊ฒƒ์ด๋‹ค. checkPermissions, requestPermissions, register , addListener

๋ฅผ ์ด์šฉํ•ด๋ณผ ๊ฒƒ์ด๋‹ค.

 

checkPermissions    : ๊ถŒํ•œ ์ฒดํฌ

requestPermissions : ๊ถŒํ•œ ์š”์ฒญ

register: FCM ๋“ฑ๋ก

addListener : ์ด๋ฒคํŠธ ๊ฐ์ง€(๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ๊ฐ€ ์กด์žฌํ•˜์—ฌ https://capacitorjs.com/docs/apis/push-notifications ์—์„œ ์ด๋ฒคํŠธ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.)

 

๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ถŒํ•œ ์ฒดํฌ ํ›„ ์—†์œผ๋ฉด => ๊ถŒํ•œ ์š”์ฒญ => FCM ๋“ฑ๋ก => addListener("registration") ์„ ํ†ตํ•ด token ์–ป๊ธฐ. 

 

import { defineComponent, onMounted } from "vue";
import { PushNotifications, Token } from "@capacitor/push-notifications";

export default defineComponent({
  name: "App",
  components: {
    IonApp,
    IonRouterOutlet,
  },
  setup() {
    onMounted(async () => {
      //๊ถŒํ•œ ์ฒดํฌ ํ•˜๊ธฐ
      const { receive } = await PushNotifications.checkPermissions();

      if (receive === "prompt") {
        const { receive: permisson } = await fcmRequestPermisson();
        if (permisson === "granted") {
          //๊ถŒํ•œ ๋“ฑ๋ก
          await PushNotifications.register();

          //ํ† ๊ทผ ์–ป๊ธฐ
          getFcmToken();
        }
      } else {
        //ํ† ๊ทผ ์–ป๊ธฐ
        getFcmToken();
      }
    });

    //๊ถŒํ•œ ์š”์ฒญ
    const fcmRequestPermisson = async () => {
      return await PushNotifications.requestPermissions();
    };

    //token get
    const getFcmToken = async () => {
      PushNotifications.addListener("registration", (token: Token) => {
        console.log("token.value : ", token.value);
      });

      PushNotifications.addListener("registrationError", (err) => {
        console.error("get token error : ", err);
      });
    };
  },
});

๐ŸŒญ FCM ํ…Œ์ŠคํŠธ

์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ์–ป์€ token์€ ์•„๋ž˜์ฒ˜๋Ÿผ ํ…Œ์ŠคํŠธ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

FireBase์—์„œ ํ…Œ์ŠคํŠธ ์ „์†ก์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ token์„ ๋“ฑ๋กํ•œ๋‹ค.

์œ„ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์•Œ๋ฆผ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

'ionic' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ionic cordova fcm - android  (0) 2022.12.25
๐Ÿ“ฆ ionic android fastlane  (0) 2022.12.18
๐Ÿค ionic live-reload ํ•ด๋ณด๊ธฐ  (0) 2022.10.22
๐Ÿค ionic fcm background์—์„œ ๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ  (0) 2022.03.20
Ionic Speech Recognition  (0) 2022.01.01

๋Œ“๊ธ€