我可以从Firebase云功能发送静音推送通知吗?

Eug*_*ene 6 ios firebase google-cloud-functions firebase-cloud-messaging

是否可以从Firebase云功能发送静默APN(iOS)远程通知?如果是这样,怎么办呢?我想在应用程序不在前台时向iOS应用程序实例发送数据,而不会让用户看到通知.

我目前发送的通知可以被用户看到:

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

exports.sendNotifications = functions.database.ref('/events/{pushId}').onWrite(event => {
  const id = event.params.pushId

  const payload = {
    notification: {
      title: 'An event has occurred!',
      body: 'Please respond to this event.',
      event_id: id
    }
  };

  return admin.messaging().sendToTopic("events", payload);
});
Run Code Online (Sandbox Code Playgroud)

我希望能够id在没有视觉通知的情况下将其发送到应用程序.

Eug*_*ene 9

我想出了如何修改我的代码以成功发送静默通知.我的问题是,我一直试图把content_availablepayload,当它真正应该是options.这是我的新代码:

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

exports.sendNotifications = functions.database.ref('/events/{pushId}').onWrite(event => {
  const id = event.params.pushId

  const payload = {
    data: {
      title: 'An event has occurred!',
      body: 'Please respond to this event.',
      event_id: id
    }
  };

  const options = {
    content_available: true
  }

  return admin.messaging().sendToTopic("events", payload, options);
});
Run Code Online (Sandbox Code Playgroud)

我成功地接收了无声的通知我的iOS设备上实现后application:didReceiveRemoteNotification:fetchCompletionHandleruserNotificationCenter:willPresent:withCompletionHandler.