javascript 推送通知的云功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 推送通知的云功能相关的知识,希望对你有一定的参考价值。

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


/**
 * Triggers when new subject added in the list and sends a notification.
 */
exports.sendNotification = functions.database.ref('/subjects/{subjectKey}').onWrite((event) => {
  // Grab the current value of what was written to the Realtime Database.
  const subject = event.data.val();

  // Notification details.
  const payload = {
    notification: {
      title: 'hello',
      body: `${subject.author} just posted new subject "${subject.title}".`,
      icon: 'photoURL',
      sound: 'default',
      clickAction: 'fcm.ACTION.HELLO',
      // badge: '1'
    },
    data: {
      extra: 'extra_data',
    },
  };

  // Set the message as high priority and have it expire after 24 hours.
  const options = {
    collapseKey: 'demo',
    contentAvailable: true,
    priority: 'high',
    timeToLive: 60 * 60 * 24,
  };

  // Send a message to devices subscribed to the provided topic.
  const topic = `/topics/list`;
  return admin.messaging().sendToTopic(topic, payload, options)
    .then((response) => {
      console.log('Successfully sent message:', response);
    });
});

以上是关于javascript 推送通知的云功能的主要内容,如果未能解决你的问题,请参考以下文章

Android使用google Firebase Cloud Messaging的云推送发送通知

Android使用google Firebase Cloud Messaging的云推送发送通知

使用 Cloud Functions for Firebase 发送推送通知

SwiftUI - 当用户打开推送通知时打开特定视图

带有自定义声音的推送通知未在 iOS 中显示

将服务器推送通知解析到特定用户问题