-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
Copy pathindex.js
26 lines (22 loc) · 954 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const EXPIRATION_TIME = 1000 * 60 * 60 * 24 * 60; // 60 days
/**
* Scheduled function that runs once a day. It retrieves all stale tokens then
* unsubscribes them from 'topic1' then deletes them.
*
* Note: weather is an example topic here. It is up to the developer to unsubscribe
* all topics the token is subscribed to.
*/
// [START remove_stale_tokens]
exports.pruneTokens = functions.pubsub.schedule('every 24 hours').onRun(async (context) => {
// Get all documents where the timestamp exceeds is not within the past month
const staleTokensResult = await admin.firestore().collection('fcmTokens')
.where("timestamp", "<", Date.now() - EXPIRATION_TIME)
.get();
// Delete devices with stale tokens
staleTokensResult.forEach(function(doc) { doc.ref.delete(); });
});
// [END remove_stale_tokens]