Disclaimer: This document applies to iOS, I have not tried it on Android.

Parse server has a feature to send push notifications. But for that to work, you need to store device tokens in Parse server somehow from your React Native app. I found that part to be undocumented so here’s my notes on it.

Obtaining device tokens

There’s documentation for this already but in case you have not read it, here’s a recap:

You obtain device tokens from the devices after user has accepted the “allow push notifications?” dialog. That’s easy, you just follow the React Native documentation regarding PushNotificationIOS

If you’ve followed that documentation, you probably now have a PushNotificationIOS.requestPermissions() call somewhere in your code and a listener that will receive the device token, something like this:

PushNotificationIOS.addEventListener('register', (token) => {
  console.log("I got the token yeehaw! "+token);
});

PushNotificationIOS.requestPermissions();

If not, go back to the documentation and fix it until you get the device token. That part is not covered here.

Now to the interesting part, how to store that token in Parse server so that Parse finds the tokens automatically and you can send push notifications to devices through parse dashboard.

Send the device tokens to Parse

There was documentation on how to save the push token to Parse from native apps, but no documentation how to do that from React Native app.

I had to dig into Parse’s React Native code to find this out:

  • You need to create an _Installation object for that device in Parse.
  • That object should have the push token obtained in the PushNotificationIOS’s register event listener.

So, let’s add some Parse code to that event listener:

PushNotificationIOS.addEventListener('register', (token) => {
  Parse._getInstallationId()
    .then(function(id) {
      var Installation = Parse.Object.extend("_Installation");
      var query = new Parse.Query(Installation);
      query.equalTo("installationId", id);
      query.find()
        .then((installations) => {
          var installation;
          if (installations.length == 0) {
            // No previous installation object, create new one.
            installation = new Installation();
          } else {
            // Found previous one, update.
            installation = installations[0];
          }
          installation.set("channels", []);
          installation.set("deviceToken", token);
          installation.set("deviceType", "ios");
          installation.set("installationId", id);
          return installation.save()
        })
        .catch((error) => {
          console.log("Error:");
          console.log(error);
        });
    });
});

Add that to your React Native code, re-run the application.

Now if you go to parse dashboard (remember to refresh) you should see a new class called “Installation” and your device in it.

If that is true, you should be able to send push notifications through parse dashboard UI and receive them in your device.

That’s all. If you have any questions or suggestions, please leave a message.