Skip to content

This Firebase cloud function aim to send VOIP push to APNS. Without any server installation you can send VOIP push from one client to other.

Notifications You must be signed in to change notification settings

emndeniz/VoipPush-FirebaseCloudFunction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VoipPushCloudFunction

This Firebase cloud function aim to send Voip push to APNS. Without any server installation you can send voip pushes from one client to other.

Call Flow

It is vital to understand how you this function allows you to send voip push. This section explains that flow.

App stores devices push tokens in firestore when user launch app.

Screen Shot 2021-05-24 at 16 40 34

When UserA decides to call UserB, device will trigger Firebase Cloud Function api. Firebase Cloud Function retrieves push token that was previously stored and sends push request to APNS with that token. At last APNS sends voip push to UserB

Screen Shot 2021-05-24 at 16 43 20

Installation

After clone this repo you need to change a few things.

  1. First you should create admin sdk json from firebase. In the link below there is nice article which explains how you can do it.

https://medium.com/litslink/firebase-admin-sdk-basics-in-examples-ee7e009a1116

After you created it place it ./functions/certs/ directory

  1. Next you should place voip push certificates to ./functions/certs/ directory.

You can check it out link below how you can obtain voip push certificate.

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_certificate-based_connection_to_apns

  1. You need to place APNS auth key to ./functions/certs/ directory.

You can check it out link below how you can obtain APNS auth key.

https://developer.clevertap.com/docs/how-to-create-an-ios-apns-auth-key

  1. Install node dependencies using npm install command on project root path.

For more information look at firebase documentation. https://firebase.google.com/docs/functions/get-started

  1. Change <Firebase admin SDK json> with your json file name.

var serviceAccount = require("./certs/<Firebase admin SDK json>");

  1. Change with your project id.
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<Your app name in firebase>.firebaseio.com"
});

Push tokens usage will be explained in Usage section

For more info please check: https://firebase.google.com/docs/reference/rest/database

  1. Change tokensPath with where you store device push tokens.
const tokensPath = admin.firestore().doc('<Firestore path where you hold push tokens>').get();
  1. Change apns credentials with your own.
var options = {
        token: {
          key: "./certs/APNsAuthKey_1234567890.p8", //Where you store apns auth key
          keyId: "key-id", // The number which presented last part of apns key, 1234567890.
          teamId: "developer-team-id" // Developer team id in developer.apple.com
        },
        production: false
      };
  1. Change app bundle id with your own.
note.topic = "<Your app bundle id>";

We are ready now!

Usage

To be able to send push a device we need to store its push token. Apple documents describes how you can obtain push token in detail.

If you need a good article to check out how you can do it please check this article.

Get push token from function below and send it to firestore using firebase sdk.

func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        let deviceToken = pushCredentials.token.map { String(format: "%02x", $0) }.joined()
        print("Voip push token :\(deviceToken)")
}

After you deploy this function to firebase, we can send push one device to another using rest request.

As described in the Fireabase document you can use cloud function as rest api.

You can test it via postman or curl your api is working or not.

Example: Lets say my project name in firebase is myDemoProject curl -X POST -H "Content-Type:application/json" -H "X-MyHeader: 123" "https://us-central1-myDemoProject.cloudfunctions.net/callRequest?callee=Customer1" -d '{"callerName":"TestUser"}'

If it is successfull you can make same request in iOS client using Alamofire or URLSession.

Contributing

I am not an JavaScript developer. That's why you might see index.js seems untidy. I am an iOS developer that needs to send Voip push and couldn't find required function code. I like to share tihs with Mobile developers such as me. If anyone can suggest better implementation on js part please feel free to contact me.

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

About

This Firebase cloud function aim to send VOIP push to APNS. Without any server installation you can send VOIP push from one client to other.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published