Skip to content

Commit

Permalink
Merge pull request #13 from fan9704/feature_firebase
Browse files Browse the repository at this point in the history
Register Firebase Cloud Messaging
  • Loading branch information
fan9704 authored Jul 18, 2023
2 parents 0c280ab + 90d6f5f commit 708b0de
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
13 changes: 13 additions & 0 deletions public/firebase-messaging-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
importScripts('https://www.gstatic.com/firebasejs/8.2.7/firebase-app.js')
importScripts('https://www.gstatic.com/firebasejs/8.2.7/firebase-messaging.js')

var firebaseConfig = {
apiKey: "AIzaSyA1FevAEUfg7u_tmhjPKGLJfVFBiQaghCY",
authDomain: "petmonitoringsystem-729da.firebaseapp.com",
projectId: "petmonitoringsystem-729da",
storageBucket: "petmonitoringsystem-729da.appspot.com",
messagingSenderId: "218937674834",
appId: "1:218937674834:web:2961695e7663494adb638b",
measurementId: "G-D4TRM99C0G"
}
const app = firebase.initializeApp(firebaseConfig);
23 changes: 18 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@
<meta name="theme-color" content="#ffffff">
<link rel="manifest" href="/manifest.json" />
<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/sw.js")
.then(res => console.log("service worker registered"))
.catch(err => console.log("service worker not registered", err));
// if ("serviceWorker" in navigator) {
// navigator.serviceWorker
// .register("/sw.js")
// .then(res => console.log("service worker registered"))
// .catch(err => console.log("service worker not registered", err));
// }
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('firebase-messaging-sw.js')
.then(reg => {
console.log(`Service Worker Registration (Scope: ${reg.scope})`);
})
.catch(error => {
const msg = `Service Worker Error (${error})`;
console.error(msg);
});
} else {
// happens when the app isn't served over HTTPS or if the browser doesn't support service workers
console.warn('Service Worker not available');
}
</script>
</head>
Expand Down
20 changes: 19 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@

<script>
import HelloWorld from './components/HelloWorld.vue'
import {onMounted, provide, reactive} from "vue";
import {onMounted, provide, reactive, ref} from "vue";
import { getToken,onMessage } from "firebase/messaging";
export default {
name: 'App',
Expand All @@ -64,6 +65,7 @@ export default {
HelloWorld,
},
setup(){
const notification = ref(null);
const states = reactive({
deferredPrompt: null,
});
Expand Down Expand Up @@ -110,5 +112,21 @@ export default {
this.drawer = false
},
},
mounted () {
console.log('Firebase cloud messaging object', this.$messaging)
getToken(this.$messaging,{
vapidKey: "BIGbuGz1MOztzbd3qpesAdtm1DKfhNVatZAdTU_ncPLrQWISpzFbbTjQS5dkQkqkLah43ic5MhJHWK3K_Ix-h0I"
})
.then((token) => {
console.log('FCM Token:', token);
})
.catch((error) => {
console.error('FCM 訂閱失敗:', error);
});
onMessage(this.$messaging, (payload) => {
console.log("收到通知:", payload);
notification.value = payload;
});
}
}
</script>
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const vuetify = createVuetify({
components,
directives,
})
// Firebase
import {messaging} from './utils/firebaseCore';


const app = createApp(App);
app.use(vuetify);
Expand All @@ -33,4 +36,5 @@ app.use(VueAxios, axios);
app.use(loadFonts);
app.use(AOS.init());
app.use(VueSweetalert2);
app.config.globalProperties.$messaging = messaging
app.mount('#app');
6 changes: 5 additions & 1 deletion src/utils/firebaseCore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAuth, GithubAuthProvider,GoogleAuthProvider } from "firebase/auth";
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
import { getMessaging } from "firebase/messaging";


const firebaseConfig = {
apiKey: "AIzaSyA1FevAEUfg7u_tmhjPKGLJfVFBiQaghCY",
Expand All @@ -16,8 +18,10 @@ const firebaseConfig = {
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const auth = getAuth(app);
const messaging = getMessaging(app)

const githubProvider = new GithubAuthProvider();
const googleProvider = new GoogleAuthProvider();

export { auth, githubProvider,googleProvider }

export { auth,messaging, githubProvider,googleProvider }

0 comments on commit 708b0de

Please sign in to comment.