Skip to content

Messaging

The schmuckliCloud service provides different kind of services. Therefore, all the services are splitted in to different packages, so that you only have to install these packages, which you want to use in your project. In this documentation, it's all about the messaging package.

Getting started

To get started, first you have to install the package via the command line terminal in your app, where the package.json file is already located.

npm i schmucklicloud_messaging

If you want to use the unpacked version, please use:

<script src="https://unpkg.com/schmucklicloud_messaging/dist/min.js" type="text/javascript"></script>

Initialization (with npm)

Now you have to create an instance, where you can connect to the schmuckliCloud service. To do that, import first the package with the class.

import { sCMessaging } from "schmucklicloud_messaging";

After that, create a new instance from the class sCMessaging and pass the APP_ID and APP_SECRET inside of it. You can get the two strings via the schmuckliCloud console.

var reference = new sCMessaging('YOUR_APP_ID', 'YOUR_APP_SECRET');

You are now ready to go with start building apps on top of the schmuckliCloud service.

Initialization (without npm)

If you are using the unpacked version of the library, you don't have to import the package anymore, since you already mapped it in the script tag. The difference between the npm version is instead, that you have to use the namespace 'schmuckliCloud', if you want to access the constructor.

var reference = new schmuckliCloud.sCMessaging('YOUR_APP_ID', 'YOUR_APP_SECRET');

Now you're all set and can start building amazing apps.

Notice

Please also make sure that you install the firebase packages especially the firebase/messaging package from npm to fetch an device token in order to use this service.

Also please define a server key in the messaging settings to send data to your devices.

Assign device tokens

Firebase gives every device an unique device token, which can be used to send a small amount to the specific device. Now before you can start sending data, we first have to assign this from Firebase created token to the user in schmuckliCloud. schmuckliCloud will then send the data to every device where the user has been assigned via FCM (Firebase Cloud Messaging).

To get assign it, you first have to sign in the user to get an authentication token. After you got the token, you must also set the auth token to the sCMessaging instance like this.

reference.setAuthToken(authentication_token);

Now that the instance knows which user is using the service, we finally can assign the device to the user.

var response = await reference.assignTokenToUser(device_token); // Returns a promise

if (response.status === 200) {
    // The token has been assigned
} else {
    // There was another problem.
}

Send a request now

When you want to send a request with data right now, to notify for example all other devices, then you can use this function.

var body = {
    notification: {
        title: "Hello World",
        description: "This is a test notification."
    }
};

var response = await reference.sendRequestNow(body);

if (response.status === 200) {
    // The request has been sent.
} else {
    // There was an unexpected error.
}

Send a request on a specific time

When you want to send a request on a specific time, then you can use the sendRequestLater function. Just pass again a body with data and a UNIX timestamp in seconds (not milliseconds).

var body = {
    notification: {
        title: "Hello World",
        description: "This is a test notification."
    }
};

var timestamp = Math.floor((new Date('2042-12-24 14:00')).getTime() / 1000); //Get a date in UNIX timestamp in seconds

var response = await reference.sendRequestLater(body, timestamp);

if (response.status === 200) {
    // The request has been sent.
} else {
    // There was an unexpected error.
}

Get all open requests

You can get all the open requests for the authenticated user, which are planned to send in the future.

var response = await reference.getOpenRequests();
if (response.status === 200) {
    var open_requests = response.data; // Fetching the open requests
} else {
    // Check the response.message for further information.
}

Get all device tokens

To get all the assigned device tokens from FCM, you can use the following function:

var response = await reference.getAllAssignedTokens();

if (response.status === 200) {
    var tokens = response.data; // Fetching the assigned tokens. 
} else {
    // Check the response.message for further information.
}

Delete an open request

When you have open requests in your queue, you can delete them as long as they have not sent yet. You find the list of open requests with the function getOpenRequests().

var response = await reference.deleteOpenRequest(id); // Retrieve the id with the function getAllAssignedTokens.
if (response.status === 200) {
    // The token has been deleted and the device will not get notified anymore.
} else {
    // There was an error while trying to delete the token. Get response.message for more information.
}

Delete an assigned token

When you want to unregister a device from the messaging SDK, you can use the following function:

var response = await reference.deleteAssignedToken(id); // Retrieve the id with the function getAllAssignedTokens.
if (response.status === 200) {
    // The token has been deleted and the device will not get notified anymore.
} else {
    // There was an error while trying to delete the token. Get response.message for more information.
}