Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Read The devices collection from an external site

I have an external web site written in php

Is there an appery rest api I can use to read in the contents of the mongodb devices collection?

I want to send push notifications from my external site to my android/ios apps. An alternative would be to send the push data to appery and let appery handle the push notifications.

What are the limits on push notifications? i have read somewhere that the size is 4K, i want to send a title and some content.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Read The devices collection from an external site

Hello Terry,

Sorry, but the list of devices is not available from 3rd part web server because of security restrictions

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Read The devices collection from an external site

OK, Can I instead send a rest call to apppery.io, passing a title & message that will instigate a push notification?

I also have another issue, Only users who subscribe to notifications are required to get them, so there needs to be a tie between the user and the device.

How would I know which device belongs to which user, If I can add a unique user id to each device and then set the channel to decide which users receive the push notification, then that may be my way around it.

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Read The devices collection from an external site

Terry. At startup I tie users to devices in my own collection to solve to the issue you mention.

You can call an Appery server script from your external site to initiate the call to the notification API

Best,
Bruce

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Read The devices collection from an external site

Thanks Bruce. I did a bit more investigation into the appery docs and realised I can determine what the current device is from the app, which means, I can as you say create my own database table containing info about the device.

I can also then set a channel in the devices collection on all users who have opted to receive push notifications.

I can then as you said, send push notification via a rest call to an appery server script.

I notice, that appery.io only mentions pushing a message, whilst other docs say you can tend a title, message and even a ticker tape rotating message.

Do you know if this is possible with appery.io?

Jeffry Reed
Posts: 0
Joined: Sun Aug 14, 2016 3:59 pm

Read The devices collection from an external site

Good morning,

I just started working on this too and have a couple question. Can someone share the js snippet that accesses appery API from external server? I would like to experiment accessing the Push API directly and through server code.

I have server code working that sends a push notification to a specific user by updating the device collection with a custom field (userId which is the userId from external DB). Can you clarify how you are structuring the appery db to achieve this with a collection other than the device collection?

thanks,
Jeff

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Read The devices collection from an external site

Here's some background to what's below:

We associate users to their devices at each login in two ways:

  1. In a table of logins. Good to know who logs into the app, where, when, and with what device ID

  2. At the same time we do (1) - we also associate the user to his device ID(s) and vice versa (in a table indexed on both columns for fast lookup).

    We then use this server code to do the work. You'll find the server code push API exposed herein.

    This is NOT perfect code - but it does the job.

    Best,

    Bruce

    var responseBody = {},
    requestParams = {},
    paramKeys = request.keys();

    for (var key = 0; key < paramKeys.length; key++) {
    requestParams[paramKeys[key]] = request.get(paramKeys[key]);
    }
    responseBody.requestBody = request.body();
    responseBody.requestParams = requestParams;
    var dbId = request.get('dbId');
    var token = request.get('token');
    var sUserID = request.get('sUserID');
    var sIDToUser = request.get('sIDToUser');
    var sDeviceID = request.get('sDeviceID');
    var sBuildNumber = request.get('sBuildNumber');
    var sMessage = request.get('sMessage');
    responseBody.method = request.method; // responseBody.method now contains string "GET" or "POST".
    var collectionName;
    var nSent = 0;
    var bSent = false;
    var bCanSend = true;
    var pushAPIKey = 'YourPushKey';
    var date = new Date();

    console.log('-------------------script called-----------------------');

    if (token === undefined token === null token );

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Read The devices collection from an external site

console.log('[JS-ERROR] - no database token supplied' + token);
}

try {
//
collectionName = 'platform_logins';
// Query the platform_logins table to find thea match for sIDToUser
///
var params = {};
params.criteria = {

Code: Select all

   "userid": sIDToUser 
 } 

console.log('Querying platform_logins looking for the last login of for the user they are sending the message to with the following params'+ JSON.stringify(params));
var oResult = Collection.query(dbId, collectionName, params, token);
console.log('Results from Query against platform_logins Table: ' + JSON.stringify(oResult));
var n = oResult.length;
if (oResult[0].sdeviceid === undefined oResult[0].sdeviceid === '' n === 0){
console.log('[JS-INFO]- No device ID Found For User: '+ sIDToUser);
bCanSend = false;
bSent = false;
}else{
//read the array looking at position [0]
var sDevicePushID = oResult[0].sdeviceid;
var sToken = oResult[0].sToken;
console.log('[JS-INFO]- the device info found sDevicePushID: '+sDevicePushID);
//var messageData = { "message": sMessage, "filter": { "deviceID": sDevicePushID}};
console.log('Message Being sent is: '+ sMessage);
/*
Apperyio.PN.send( pushAPIKey, {
payload: {
"title": 'PeopleReady',
"message": sMessage,
"badge": 1
},
schedule: {
scheduledTime: dbDate(),
// Use timeZone or userDeviceTimeZone but not both
useDeviceTimeZone:true
},
filter: {"deviceID": sDevicePushID}

});
}

Code: Select all

 */ 
    var oNotification = PN.send( 
       pushAPIKey, //Push Notification API key 
       { 
         "message": sMessage, 
         "badge": 1 
       }, { 
         "deviceID": sDevicePushID 
       }); 
 } 

nSent++
bSent = true;

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Read The devices collection from an external site

}catch (e){
console.log('there was an error: ' + e.message + ' Error ncode was:' + e.code);
responseBody.result = "Error"
responseBody.error = "message: " + e.message + "ncode: " + e.code;
}

//Wrap up - complete
responseBody.nSent = nSent;
responseBody.bSent = bSent;
response.success(responseBody, "application/json");

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Read The devices collection from an external site

and Terry - sorry I missed your ?? notice, that appery.io only mentions pushing a message, whilst other docs say you can tend a title, message and even a ticker tape rotating message.

I honestly don't know - I've not tried to add a title or ticker tape rotating message (**yet).

Best,

Bruce

Return to “Issues”