FCM – node.js中的定时器function
这就是我正在做一个Firebase云function发送静默推送通知:
exports.sendSilentPyngNotification = functions.database.ref('/SNotification/{userid}').onWrite(event => { const userid = event.params.userid console.log('Start sending SILENT Notificaitons:-'); // Get the list of device notification tokens for the respective user const getDeviceTokensPromise = admin.database().ref(`/personal/${userid}/notificationTokens`).once('value'); // Get the user profile. const getUserProfilePromise = admin.auth().getUser(userid); return Promise.all([getDeviceTokensPromise, getUserProfilePromise]).then(results => { const tokensSnapshot = results[0]; const user = results[1]; // Check if there are any device tokens. if (!tokensSnapshot.hasChildren()) { return console.log('There are no notification tokens to send to.'); } // Notification details. const payload = { notification: { content_available : 'true' }, data : { senderid: 'Test', notificationtype: String(event.data.val().type) } }; // Listing all tokens. const tokens = Object.keys(tokensSnapshot.val()); // Send notifications to all tokens. return admin.messaging().sendToDevice(tokens, payload).then(response => { // For each message check if there was an error. const tokensToRemove = []; response.results.forEach((result, index) => { const error = result.error; if (error) { console.error('Failure sending notification to', tokens[index], error); // Cleanup the tokens who are not registered anymore. if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') { tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove()); } } console.log('Successfull sent SILENT NOTIFICATION'); }); return Promise.all(tokensToRemove); }); }); });
问题:我想用定时器反复激活这个无声的通知function。 我真的不知道如何在node.js中写这个函数的定时器函数。
有人可以快速引导我吗? 我是一个在Dev.js中没有任何知识的iOS Dev。
谢谢。
您可能要查看https://nodejs.org/en/docs/guides/timers-in-node/多次重复任何代码块的最简单方法是setInterval
// The function you want to fire multiple times function functionToRepeat() { // Put your code here } // Calls functionToRepeat every 1500ms var intervalObj = setInterval(functionToRepeat, 1500); // To stop the interval clearInterval(intervalObj)
希望这可以帮助!
- 名为“”的Firebase应用已经存在,并且Google的Firebase参考在服务器上不起作用
- 如何通过Firebase Admin与代理连接到Firebase?
- 使用Firebase身份validation和Express身份validation社交login?
- TypeError:访问Firebase工具时,Buffer.alloc不是一个函数
- 云function中的云Firestore
- Firebase应用程序在localhost:5000 / / / app上运行
- FCM设备组iOS应用徽章更新会导致Android上的空推
- Firebase admin.auth.createUser错误(nodejs + Typescript + firebase-admin)
- 从使用Cloud Functions for Fire上传的文件获取下载url