Skip to content

Commit 35ab9cc

Browse files
Updating the Cloud Messaging docs for Angular Fire modular (#3516)
* Update messaging.md * Update messaging.md --------- Co-authored-by: James Daniels <jamesdaniels@google.com>
1 parent 83bc879 commit 35ab9cc

File tree

1 file changed

+187
-9
lines changed

1 file changed

+187
-9
lines changed

docs/messaging.md

+187-9
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,198 @@
66

77
# Cloud Messaging
88

9-
The FCM JavaScript API lets you receive notification messages in web apps running in browsers that support the Push API.
9+
Firebase FCM allows you to register devices with unique FCM tokens, that you can later programtically send notifications to using Firebase Cloud Functions. It is up to the application to update these tokens in Firebase if you want to use them in other layers of your application, i.e send a notification to all administrators, etc. In that case, you would likely want to store your fcm tokens on your user collection, or a sub collection or another collection with different permissions.
1010

11-
[Learn More](https://firebase.google.com/docs/cloud-messaging/)
11+
# Provide Messaging to your existing application
1212

13-
## Dependency Injection
13+
```
14+
import { getMessaging, provideMessaging } from "@angular/fire/messaging";
1415
15-
YADA YADA YADA
16+
bootstrapApplication(AppComponent, {
17+
providers: [
18+
...,
19+
provideMessaging(() => getMessaging())
20+
),
21+
],
22+
});
23+
```
1624

17-
## Firebase API
25+
# Create a Firebase Messaging Service Worker
1826

19-
Something something look at the offical docs
27+
There are two parts to Firebase Messaging, a Service Worker and the DOM API. Angular Fire Messaging allows you to request permission, get tokens, delete tokens, and subscribe to messages on the DOM side. To register to receive notifications you need to set up the Service Worker. [The official Firebase documentation for setting up the details exactly how to do that](https://firebase.google.com/docs/cloud-messaging/js/client).
2028

21-
## Convenience observables
29+
#### Create your firebase-messaging-sw.js file in your src/assets folder
2230

23-
### Foo
31+
*Note: When copying the below file, make sure your firebase version in your installation matches the version your are importing from below*
2432

25-
bar baz
33+
It may be wise to use file replacements or environments here for different environments
34+
35+
```
36+
// This sample application is using 9.22, make sure you are importing the same version
37+
38+
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-app.js";
39+
import { getMessaging } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-messaging-sw.js";
40+
41+
const firebaseApp = initializeApp({
42+
apiKey: "",
43+
authDomain: "",
44+
projectId: "",
45+
storageBucket: "",
46+
messagingSenderId: "",
47+
appId: "",
48+
});
49+
50+
const messaging = getMessaging(firebaseApp);
51+
```
52+
53+
# Example messaging service
54+
55+
```
56+
import { Injectable } from "@angular/core";
57+
import { Messaging, getToken, onMessage, deleteToken } from "@angular/fire/messaging";
58+
import { Observable, tap } from "rxjs";
59+
60+
@Injectable({
61+
providedIn: "root",
62+
})
63+
export class FcmService {
64+
constructor(private msg: Messaging){
65+
Notification.requestPermission().then(
66+
(notificationPermissions: NotificationPermission) => {
67+
if (notificationPermissions === "granted") {
68+
console.log("Granted");
69+
}
70+
if (notificationPermissions === "denied") {
71+
console.log("Denied");
72+
}
73+
});
74+
navigator.serviceWorker
75+
.register("/assets/firebase-messaging-sw.js", {
76+
type: "module",
77+
})
78+
.then((serviceWorkerRegistration) => {
79+
getToken(this.msg, {
80+
vapidKey: `an optional key generated on Firebase for your fcm tokens`,
81+
serviceWorkerRegistration: serviceWorkerRegistration,
82+
}).then((x) => {
83+
console.log('my fcm token', x);
84+
// This is a good place to then store it on your database for each user
85+
});
86+
});
87+
}
88+
this.message$ = new Observable((sub) => onMessage(this.msg, (msg) =>
89+
sub.next(msg))).pipe(
90+
tap((msg) => {
91+
console.log("My Firebase Cloud Message", msg);
92+
})
93+
);
94+
}
95+
deleteToken(){
96+
// We can also delete fcm tokens, make sure to also update this on your firestore db if you are storing them as well
97+
await deleteToken(this.msg);
98+
}
99+
```
100+
101+
# Testing and Sending Notifications
102+
103+
Firebase will allow you to send a test notification under Engage > Messaging > New Campaign > Notifications. Here you can click send a test message. Additionally, you can send them programmatically through Firebase cloud functions.
104+
105+
Here is a barebones Node example:
106+
107+
```
108+
export const sendTestMessage = onRequest(async (_, res) => {
109+
try {
110+
const message = {
111+
notification: {
112+
title: "Test Title",
113+
body: "Test Body",
114+
},
115+
token: "your token here, you can store these and retreive as you please",
116+
};
117+
await admin.messaging().send(message);
118+
res.sendStatus(200);
119+
} catch (error) {
120+
console.error(error);
121+
res.sendStatus(500);
122+
}
123+
});
124+
```
125+
126+
Here is a Node example that listens for a new comment on a collection, then sends a notification, and also adds it to a cache on Firebase so users can click through them.
127+
128+
```
129+
exports.onPostReply =
130+
onDocumentCreated("comments/{commentId}", async (event) => {
131+
if (!event) throw new Error("No event found for document creation");
132+
const snapshot = event.data;
133+
if (!snapshot) {
134+
throw new Error("No data associated with the event");
135+
}
136+
const data = snapshot.data();
137+
if (!data.postId) {
138+
throw new Error("No post ID found");
139+
}
140+
const postRef = await firestore.collection("posts").doc(data.postId).get();
141+
const postData = postRef.data();
142+
if (!postData) {
143+
throw new Error("No postData found");
144+
}
145+
// userUid will be the post author's id.
146+
const {userUid} = postData;
147+
if (!userUid) {
148+
throw new Error(
149+
"Could not find the userUid for the post author for post reply"
150+
);
151+
}
152+
const messageForNotification = {
153+
title: "You have a new reply on your post",
154+
body: "",
155+
};
156+
await createNotificationAndCache(messageForNotification, userUid);
157+
});
158+
159+
// If you want to cache notifications a number of times, abstracting this
160+
// to a function can bring a lot of value.
161+
162+
interface NotificationProps {
163+
title: string;
164+
body: string;
165+
}
166+
167+
async function createNotificationAndCache(
168+
notificationProps: NotificationProps, userAuthUid: string) {
169+
const userRef = await firestore.collection("users").where("authUid", "==",
170+
userAuthUid).get();
171+
const userData = userRef.docs[0].data();
172+
173+
const promises: Promise<any>[] = [];
174+
// This sample application has seperate fcm tokens for web and mobile
175+
if (userData.mobileToken) {
176+
const message = {
177+
notification: notificationProps,
178+
token: userData.mobileToken,
179+
};
180+
const promise = admin.messaging().send(message);
181+
promises.push(promise);
182+
}
183+
if (userData.webToken) {
184+
const message = {
185+
notification: notificationProps,
186+
token: userData.webToken,
187+
};
188+
const promise = admin.messaging().send(message);
189+
promises.push(promise);
190+
}
191+
192+
const notificationCacheValue = {
193+
userAuthUid: userAuthUid,
194+
tokenTitle: notificationProps.title,
195+
tokenBody: notificationProps.body,
196+
isActive: true, // This determines whether a notification has been seen
197+
};
198+
199+
promises.push(
200+
firestore.collection("notificationCache").add(notificationCacheValue));
201+
202+
await Promise.all(promises);
203+
} ```

0 commit comments

Comments
 (0)