Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I send a silent push notification from a Firebase cloud function?

Is it possible to send a silent APNs (iOS) remote notification from a Firebase Cloud Function? If so, how can this be done? I want to send data to iOS app instances when the app is not in the foreground, without the user seeing a notification.

I currently send a notification that can be seen by users:

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);
});

I would like to be able to send that id to the app without a visual notification.

like image 644
Eugene Avatar asked Sep 05 '17 15:09

Eugene


People also ask

How do I send silent push notifications?

There are two ways users can receive silent push notifications on Android. Users can long press on a notification to get an option to display notifications silently. Users can also enable silent notifications by heading to Settings > App & Notifications > Search for app and choose> Notifications, and turn it off.

Can I send push notifications without FCM?

It's definitely possible -- you don't have to use Firebase to deliver push notifications. You can roll out your own notification solution or consider a paid product such as Pushy (pushy.me) which does not rely on Firebase Cloud Messaging.

What is silent push notification?

Users can choose to mute their notifications and receive silent push notifications instead of being alerted to every new push that arrives. They can activate this on iOS or Android from their individual settings pages. And developers or mobile marketers cannot override this setting once put into place.


1 Answers

I figured out how to modify my code to successfully send a silent notification. My problem was that I kept trying to put content_available in the payload, when it really should be in options. This is my new code:

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);
});

I successfully received the silent notification on my iOS device after implementing application:didReceiveRemoteNotification:fetchCompletionHandler and userNotificationCenter:willPresent:withCompletionHandler.

like image 147
Eugene Avatar answered Sep 19 '22 10:09

Eugene