Skip to content

Update messaging.md #3627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions docs/messaging.md
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ There are two parts to Firebase Messaging, a Service Worker and the DOM API. Ang

It may be wise to use file replacements or environments here for different environments

```
```ts
// This sample application is using 9.22, make sure you are importing the same version

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-app.js";
@@ -74,7 +74,7 @@ const messaging = getMessaging(firebaseApp);

# Example messaging service

```
```ts
import { Injectable } from "@angular/core";
import { Messaging, getToken, onMessage, deleteToken } from "@angular/fire/messaging";
import { Observable, tap } from "rxjs";
@@ -83,6 +83,7 @@ import { Observable, tap } from "rxjs";
providedIn: "root",
})
export class FcmService {
message$: Observable<any>;
constructor(private msg: Messaging){
Notification.requestPermission().then(
(notificationPermissions: NotificationPermission) => {
@@ -105,19 +106,20 @@ export class FcmService {
console.log('my fcm token', x);
// This is a good place to then store it on your database for each user
});
});
}
});

this.message$ = new Observable((sub) => onMessage(this.msg, (msg) =>
sub.next(msg))).pipe(
tap((msg) => {
console.log("My Firebase Cloud Message", msg);
})
);
}
deleteToken(){
}
async deleteToken(){
// We can also delete fcm tokens, make sure to also update this on your firestore db if you are storing them as well
await deleteToken(this.msg);
}
}
```

# Testing and Sending Notifications
@@ -126,7 +128,7 @@ Firebase will allow you to send a test notification under Engage > Messaging > N

Here is a barebones Node example:

```
```js
export const sendTestMessage = onRequest(async (_, res) => {
try {
const message = {
@@ -147,7 +149,7 @@ export const sendTestMessage = onRequest(async (_, res) => {

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.

```
```js
exports.onPostReply =
onDocumentCreated("comments/{commentId}", async (event) => {
if (!event) throw new Error("No event found for document creation");
@@ -222,4 +224,5 @@ async function createNotificationAndCache(
firestore.collection("notificationCache").add(notificationCacheValue));

await Promise.all(promises);
} ```
}
```