1

I am trying to convert a firebase-queue worker to send push notification to a cloud function. I am using node-apn to send push notification to iOS devices. It requires setting up a connection which requires me to specify a key.pem file and cert.pem file. These files are present at the same location where the worker js file is present and works without any problem. I moved over the code to a cloud function but I get this error in the Logs console

{ Error: ENOENT: no such file or directory, open './cert.pem'
    at Error (native)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: './cert.pem' } 'Unable to send push notification to iOS device. Socket Error'

Below is how the files are specified and the connection is created in the code

var connectionOptions = {
  cert:'./cert.pem',
  key:'./key.pem',
  production: true
};

var apnConnection = new apn.Connection(connectionOptions);

I have tried specifying the cert file as ./cert.pem and cert.pem but I get a similar error in both the cases. I guess the problem is that the .pem files are not shipped along with the functions.

How can I specify such files in a cloud function?

Varun Gupta
  • 2,870
  • 6
  • 33
  • 73

1 Answers1

3

Your path reference isn't quite right for firebase functions.

It should be:

var connectionOptions = {
  cert:__dirname + '/cert.pem',
  key:__dirname + '/key.pem',
  production: true
};
  • Thanks for the answer. I will try this also. I didn't find any reference to using `__dirname` when I looked at the firebase functions documentation and I just searched through the documentation and the firebase function samples https://github.com/firebase/functions-samples and don't see any reference to `__dirname`. Would it be possible to share a reference for this change for my and others benefit? Thanks! – Varun Gupta Aug 01 '17 at 04:11
  • 2
    Hopefully this should explain the reason for you: https://stackoverflow.com/questions/8131344/what-is-the-difference-between-dirname-and-in-node-js – user2895001 Aug 05 '17 at 18:48
  • Thanks a lot for the reference to the stackoverflow question. Learned something new and important today. I apologize that I have not been able to test with the changes you suggested as I am not able to get time off my current assignment. – Varun Gupta Aug 06 '17 at 03:29
  • I was finally able to test out this change and it works. Thanks! – Varun Gupta Dec 20 '17 at 18:31