Skip to content

ElectricImp-CSE/FirebaseCloudMessaging

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Google Firebase Cloud Messaging 1.0.0

Google Firebase Cloud Messaging service offers Imp agent to push notifications/messages to an application instance , a device group or application instances subscribed to certain topics. For information can be found here.

Create New Project

Skip this section if you already have an existing Firebase project.

  1. Open Firebase Console
  2. Log in with Google account
  3. Click on "CREATE NEW PROJECT"
  4. Enter the preferred project name
  5. Select the preferred country/region
  6. Click on "CREATE PROJECT"

Authentication

Firebase console provides the Server Key and Sender ID, passed into the following constructor’s serverKey and senderId parameter.

  1. Open Firebase Console
  2. Log in with Google account
  3. Click on the relevant project
  4. Click on the gear icon next to the project name on the left
  5. Click on "Project settings"
  6. Select "CLOUD MESSAGING" tab
  7. Copy the Server Key and Sender ID to the clipboard and paste it into the constructor.

FirebaseCloudMessaging Class Usage

Constructor: FirebaseCloudMessaging(serverKey, senderId)

This contructs a FirebaseCloudMessaging object.

The serverKey and senderId parameters are supplied by the Firebase Console (see above).

// Instantiate a fcm class.
local fcm = FirebaseCloudMessaging(SERVER_KEY, SENDER_ID);

FirebaseCloudMessaging Class Methods

sendMessage(data[, callback])

This method pushes a message/notification to a recipient or a group of recipients subscribed to certain topics.

data is a table and information on its fileds can be found here;

callback is optional. If not provided , sendMessage() will block until its completion.

// send a message to a recipient
local data = {
    "to" : "RECIPIENT_REGISTRATION_TOKEN",
    "data" : {
    "message" : "A message"
    }
};
fcm.sendMessage (data,function (error,response){
    if (error) {
        server.error(error);
    } else {
        server.log(response);
    }
});


// send a message to a group of recipients subscribed to a TOPIC 
local data = {
    "to" : "/topics/TOPIC",
    "data" : {
    "message" : "A topic message"
    }
};
fcm.sendMessage (data,function (error,response){
    if (error) {
        server.error(error);
    } else {
        server.log(response);
    }
});
 

manageDeviceGroup(data[, callback])

This method creates a device group / assigns an app instance to a device group / removes an app instance from a device group. sendMessage() can be used to push message/notification to a device group .

data is a table and information on its fileds can be found here;

callback is optional. If not provided , manageDeviceGroup() will block until its completion.

// create a device group
local data = {
    "operation" : "create",
    "notification_key_name" : "GROUP_NAME",
    "registration_ids" : ['DEVICE_ONE','DEVICE_TWO']
};
fcm.manageDeviceGroup (data,function (error,notification_key){
    if (error) {
        server.error(error);
    } else {
        server.log(notification_key);
    }
});


// assign an app instance to a device group
local data = {
    "operation" : "add",
    "notification_key" : "NOTIFICATION_KEY" ,
    "notification_key_name" : "GROUP_NAME",
    "registration_ids" : ['NEW_DEVICE']
};
fcm.manageDeviceGroup (data,function (error,notification_key){
    if (error) {
        server.error(error);
    } else {
        server.log(notification_key);
    }
});



// remove an app instance to a device group
local data = {
    "operation" : "remove",
    "notification_key" : "NOTIFICATION_KEY" ,
    "notification_key_name" : "GROUP_NAME",
    "registration_ids" : ['NEW_DEVICE']
};
fcm.manageDeviceGroup (data,function (error,notification_key){
    if (error) {
        server.error(error);
    } else {
        server.log(notification_key);
    }
});
 

describeDevice(registrationId, ifDetailed[, callback])

This method retrieves inforamtion on an app instance

registrationId is a string containing app instance's registration token.

ifDetailed is a boolean variable. Click here for detailed description

callback is optional. If not provided , describeDevice() will block until its completion.

 
fcm.describeDevice (REGISTRATION_ID,true,function (error,deviceInfo){
    if (error) {
        server.error(error);
    } else {
        server.log(appInfo);
    }
});
 
 

subscribeDevicesToTopic(registrationId, ifDetailed[, callback])

This method subscribes app instances to a topic

registrationIds an array of app instance's registration token.

topic is string specifying the topic app instances subscribed to.

callback is optional. If not provided , subscribeDevicesToTopic() will block until its completion.

 
fcm.subscribeDevicesToTopic ([REGISTRATION_ID_1],TOPIC,function (error,response){
    if (error) {
        server.error(error);
    } else {
        server.log(response);
    }
});
 
 

unsubscribeDevicesFromTopic(registrationId, ifDetailed[, callback])

This method unsubscribes app instances from a topic

registrationIds an array of app instance's registration token.

topic is string specifying the topic app instances unsubscribed from.

callback is optional. If not provided , unsubscribeDevicesFromTopic() will block until its completion.

 
fcm.unsubscribeDevicesFromTopic ([REGISTRATION_ID_1],TOPIC,function (error,response){
    if (error) {
        server.error(error);
    } else {
        server.log(response);
    }
});
 
 

Callbacks

Callback functions passed into the above methods should be defined with the following parameters:

Parameter Value
error This will be null if there was no error. Otherwise it will contain the error message
response For sendMessage() : result object
For manageDeviceGroup() : notification key
For describeDevice() : deviceInfo
For subscribeDevicesToTopic() and unsubscribeDevicesFromTopic() : response object

License

This library is licensed under MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors