How to send a push notification to a specific device with Firebase and Capacitor

There is a helpful guide for sending push notifications with Capacitor and Firebase, but it only describes how to send global notifications to an app. Is there a way to get a device id with Capacitor, so that you can send a notification to that device with the Firebase Server SDK?

You need the device token provided by Capacitor’s Push Notifications plugin (following the guide you mentioned):

  // On succcess, we should be able to receive notifications
  PushNotifications.addListener('registration', 
    (token: PushNotificationToken) => {
      alert('Push registration success, token: ' + token.value);
    }
  );

You can use that token value to target specific devices. To test notifications on a specific device head over to your Firebase project’s Cloud Messaging page, click on “Send your first message” if you haven’t already done so and click “Send test message”.

Enter the token value that was given to you before, and click on ‘Test’.

image

If everything was set up correctly, you will get a notification on your device. I have personally test notifications this way on Android and it works as expected. You could extend notifications functionality later on by implementing Cloud Functions based on specific events triggered in your database.

2 Likes