๐น 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 |
๋๊ธ