Skip to content

Commit c4c8416

Browse files
s-chandelsSumit Chandelgemini-code-assist[bot]
authored
Add new register FID snippets (#411)
* Add new register FID methods * Generated modular snippets --------- Co-authored-by: Sumit Chandel <sumitchandel@google.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 95c8c15 commit c4c8416

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

messaging-next/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ function getToken() {
4848
// [END messaging_get_token]
4949
}
5050

51+
function register() {
52+
// [START messaging_register]
53+
const { getMessaging, onRegistered, register } = require("firebase/messaging");
54+
55+
const messaging = getMessaging();
56+
57+
// 1. Implement callback to receive the Firebase installation ID upon registration.
58+
// This is triggered every time a manual register() finishes, a FID change
59+
// is detected, or a pushsubscriptionchange event is fired.
60+
onRegistered(messaging, (installationId) => {
61+
console.log('Registered installation ID:', installationId);
62+
63+
// Send the Firebase Installation ID to your app server and update the UI if needed.
64+
sendRegistrationToServer(installationId);
65+
});
66+
67+
// 2. You can also manually trigger registration (recommended on app startup)
68+
register(messaging, {
69+
vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>'
70+
}).then(() => {
71+
// Success! The Firebase Installation ID can be used to target messages to this app
72+
// instance and will be delivered asynchronously to your onRegistered() callback.
73+
}).catch((err) => {
74+
console.error('An error occurred while registering', err);
75+
});
76+
// [END messaging_register]
77+
}
78+
5179
function requestPermission() {
5280
// [START messaging_request_permission]
5381
Notification.requestPermission().then((permission) => {
@@ -75,3 +103,8 @@ function deleteToken() {
75103
});
76104
// [END messaging_delete_token]
77105
}
106+
107+
function sendRegistrationToServer(installationId) {
108+
// This function is used in a snippet to indicate that the Firebase Installation ID should be sent to the app server.
109+
// TODO(developer): Implement this function to send the registration ID to your app's server.
110+
}

messaging-next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
},
77
"license": "Apache-2.0",
88
"dependencies": {
9-
"firebase": "^10.0.0"
9+
"firebase": "^12.0.0"
1010
}
1111
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./messaging-next/index.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START messaging_register_modular]
8+
import { getMessaging, onRegistered, register } from "firebase/messaging";
9+
10+
const messaging = getMessaging();
11+
12+
// 1. Implement callback to receive the Firebase installation ID upon registration.
13+
// This is triggered every time a manual register() finishes, a FID change
14+
// is detected, or a pushsubscriptionchange event is fired.
15+
onRegistered(messaging, (installationId) => {
16+
console.log('Registered installation ID:', installationId);
17+
18+
// Send the Firebase Installation ID to your app server and update the UI if needed.
19+
sendRegistrationToServer(installationId);
20+
});
21+
22+
// 2. You can also manually trigger registration (recommended on app startup)
23+
register(messaging, {
24+
vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>'
25+
}).then(() => {
26+
// Success! The Firebase Installation ID can be used to target messages to this app
27+
// instance and will be delivered asynchronously to your onRegistered() callback.
28+
}).catch((err) => {
29+
console.error('An error occurred while registering', err);
30+
});
31+
// [END messaging_register_modular]

0 commit comments

Comments
 (0)