Child pages
  • Push notifications to devices

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

With Apstrata you can easily push notifications to any iOS or Android devices, asynchronously or synchronously just by invoking the PushNotifications API. You also can create reusable device groups, called channels, to which you can push simultaneously, or you can dynamically build a list of devices further to the execution of a no-SQL query on your content or user directory for example. To configure your connection to a push notification middle tier such as APNS or GCMFCM, you invoke the AddCertificate API or, even simpler, resort to the Apstrata Workbench.

...

  • APNS
    • If you intend to push through APNS, fill the "password" field with the password of your APNS certificate
    • From the "certificate" field, upload your certificate
    • If your certificate is for development only, set the value of the "development" field to "true", otherwise set it to "false"
  • GCMFCM
    • Leave the password field empty
    • Click on the "certificate" field to upload text file that contains your API Key
    • Leave the "development" field empty

...

Localtab Group
Localtab
titleApstrata script
Code Block
languagejs
<script>
	<scriptACL>
		<execute>nobody</execute>
		<read>nobody</read>
		<write>nobody</write>
	</scriptACL>
	<code>
		<![CDATA[
			
			// Prepare the parameters to pass to the ListUsers API in order to search for users
			// who registered more than n months ago
			var queryUsersParams = {
			    // Search for users who have a creation time <= the given date and that
			    "apsdb.query": "apsdb.creationTime<date> <= \"2014-07-01\" and pushToken is not null", 
			    // only return the login and pushToken attributes of the user's profile. We assume that we added the "pushToken" field to the users profiles
			    "apsdb.attributes": "login, pushToken" 
			}
			
			// Execute the request by invoking the ListUsers API
			var response = apsdb.callApi("ListUsers", queryUsersParams, null);
			if (response.metadata.status == "failure") {
			    return response.metadata;
			}
			
			var users = response.result.users;
			if (users.length == 0) {
			    return {
			        "result": "No matching users"
			    }
			}
			
			// Create an array of device tokens (GCMFCM registration ids) from the data returned by the query
			var tokens = [];
			for (var i = 0; i < users.length; i++) {
			    tokens.push(users[i].pushToken[0]); // We assume that users only have one device token (GCMFCM registration id)
			}
			
			// Prepare the parameters to pass to the push notification API
    		var notificationStr = "{\"Content\":\"Get 50% using this promotion code for your next purchase: AZX34W \"}";
			var pushParams = {
			    "apsdb.store": "DefaultStore",
			    "adhoc.tokens": tokens,
                "adhoc.lifeTime": "3600", // the lifetime in seconds of a notification before it is discarded if not pushed
                "adhoc.applicationID": "gcmfcm", // the identifier of the push application in order to use the correct credentials
                "adhoc.platform": "Android",
                "fireAndForget": "false", // asynchronous pus, do not wait for a response
                "notification": notificationStr
			};
			
			// Call the PushNotification API
			var pushNotificationsResponse = apsdb.callApi("PushNotification", pushParams, null);
            if (pushNotificationsResponse.metadata.status == "failure") {
                return pushNotificationsResponse.metadata;
            }
            
            return pushNotificationsResponse;
		]]>
	</code>
</script>

 

 



Try it!

Download the following apk and deploy it to your Android device. Once done, run the application, which will display your device registration id and open an email client. Enter your email address and send, then copy/paste the registration id that you received into the below field.

HTML
<script type="text/javascript">
	
	function runTestPush() {
		var url = "https://varick.apstrata.com/apsdb/rest/O71307F690/RunScript?apsws.responseType=json&apsdb.scriptName=tutorial/pushNotifications";
		var registrationId = document.getElementById("registrationId");		
		url += "&registrationId=" + registrationId.value;
		window.open(url);
	}
</script>
<form id="pushParameters" style="display:bloc">
	<label for="registrationId" style="display:inline-block;text-align:left;width:100px">Registration id</label>
	<input type="text" id="registrationId" name="registrationId " value="" style="width:300px" title="Paste you GCMFCM registration id here"/><br>	
	<input type="button" onclick="runTestPush()" value="Push"/>
</form>

...

Navigation Map
messaging
messaging