push notification Android Apstrata
  • hi Karim,

    I am settimg up push notification for my android app on apstrata:
    I have some questions for you,

    Manage App --> Push notifications --> Default Store -->  
    First, What shall I put in the password Field?
    Second, Concerning Application Id Field, I added the package name but this name is the same as the one for iOS app(package name for android app is the name as the package name for the android app) so we got an error that the Application ID already exist.
    how can we fix this.
    Third, I created the project for my app in google console and enabled GCM for android in addition to Obtaining Server API Key, there they asked for the server's IP(what IP should I put here for apstrata?) for now I put

    Thank you

  • Hi Nour,

    - The application Id is something that you choose, it does not have to be the package name. 

    For GCM :

    - You do not need a password so keep the field empty.
    - The certificate file should be a simple text file containing your Google API Key. 
    - The IPs are and but you can leave the IP field empty in the Google ev console (not

  • hi Karim,

    I have  1 more question concernig this, Does obtaining the Device ID in android to register it for push notification requires implementing gcm code for client app:

    IF not , how can I obtain the device ID token to insert it in apstrata schema so that it will be sent with parameters in push notification API,

    As a brief , how can I register the android device ID in apstarta for push notification to work.

    Thanks again.
  • Yes,

    You need to implement the nominal way of getting the device ID as described in the Google link of your post. Once your app has the device, it can send it to Apstrata in many different ways, depending on your need.

    A simple approach would be to save the device ID in the user's profile using SaveUser, if the device's user is know by your Apstrata User Directory. Pushing notifications to this device could then be made from an Apstrata script that takes care of retrieving the deviceId from the user's profile as follows:

    // retrieve the user's login from the request (assuming it has been signed 
    // with the user's credentials)
    var user = this.request.user.login;
    // user login = username@appId so let's remove the "@appId"
    var atIndex = user.lastIndexOf("@");
    if (atIndex > -1){
    user = user.substring(0, atIndex);

    var userProfileResponse = apsdb.callApi("GetUser", {"login":user}, null);
    // assume we have a deviceId field in the user's profile
    var deviceId = userProfileResponse.result.user.deviceId;

    Once you have the deviceId, you can push a notification using the PushNotification API, from the front end or from an Apstrata script. You can push notifications by providing an array of device IDs (this is called "ad-hoc" push) or by providing a "channel" Id to which you would have already subscribed the device ID.

    The below code shows how to execute an ad-hoc push from an Apstrata script:

    var tokens = [deviceId1, ..., deviceIdn ];
    // Prepare the parameters to pass to the push notification API
    var notificationStr = "{\"Content\":\"Some nice message- can be a stringified JSON\"}";
    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": "the_push_app_id", // the identifier of the push application in order to use the       //correct credentials
    "adhoc.platform": "Android",
    "fireAndForget": "false", // asynchronous push, do not wait for a response
    "notification": notificationStr


  • Hi Karim,

    Thank you for the above clarification,
    I implemented google client gcm code in my app so that I can register my device in GCM and get the Device ID as you already mentioned,

    For the purpose of testing once I run the app, I grab the Registration ID returned from GCM client  
    regid = gcm.register(SENDER_ID);

    this regid I copied it and added it manully to an android channel that already created where I uploaded the needed certificate..
    once I added the regid in the channel. 
    I ran API Explorer--> push notification--> there I added all the needed parameters also as u mentioned in your comment,

    where notification: {"Content":"Some nice message"}

    and I run the Script , the Response is:
    "metadata": {
    "requestId": "def9772e-9bd4-44b1-8572-3893ded6cbdb", 
    "status": "success", 
    "statusCode": "200"
    "result": {
    "invalidDeviceTokens": [

    But I am not getting any notification on my device,

    Is there anything missing ? , kindly can you help

    Thank you.
  • Hi Nour,

    Try making the message a strinigified JSON:  ex: "{\"Content\":{\"msg\":\"Some nice message\"}}"


    ERRATA (03/23/3015): 
    When using the API Explorer, the format of the message should be: {"Content": "some_string"}  
    When using a script or invoking the API through HTTP, the format should be: 
    Or more specifically: 
    var jsonContent = {"text":"hello"};
    var msgObject = {"Content": JSON.stringify(jsonContent)};
    var notificationStr = JSON.stringify(msgObject); // --> send notificationStr

  • Hi Karim,

    I tried and I am getting an error:

    		<span class="block">{
    <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"metadata"</span>: <span class="block">{
    <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"requestId"</span>: <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"697baec6-52f9-4cfa-ad85-caf53236486a"</span>,
    <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"status"</span>: <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"failure"</span>,
    <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"errorCode"</span>: <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"INVALID_NOTIFICATION"</span>,
    <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"errorDetail"</span>: <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"Payload is not in the expected format {key:value,key:value,...} : <span class="escape">\"</span>{<span class="escape">\\</span><span class="escape">\"</span>Content<span class="escape">\\</span><span class="escape">\"</span>:{<span class="escape">\\</span><span class="escape">\"</span>msg<span class="escape">\\</span><span class="escape">\"</span>:<span class="escape">\\</span><span class="escape">\"</span>Some nice message<span class="escape">\\</span><span class="escape">\"</span>}}<span class="escape">\"</span>"</span>,
    <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"statusCode"</span>: <span class="string" style="color: rgb(136, 0, 0); background-color: rgb(255, 240, 240);">"200"</span>

    <span class="block"><br></span>
  • I will write the error in a clearer way:
    "metadata": {
    "requestId": "697baec6-52f9-4cfa-ad85-caf53236486a", 
    "status": "failure", 
    "errorCode": "INVALID_NOTIFICATION", 
    "errorDetail": "Payload is not in the expected format {key:value,key:value,...} : \"{\\\"Content\\\":{\\\"msg\\\":\\\"Some nice message\\\"}}\"", 
    "statusCode": "200"
  • Hi karim,

    I removed the slash from the notification String
    {"Content":{"msg":"Some nice message"}}

    no more errors appearing, but still not getting any notification on the device,

    Can you give me any example of how should should this Device Token be:


    This is an example for the Token I am getting for my device, is this the right form of Device Token ?

    Thank you
  • Hi Nour,

    This looks like a valid token. Could you try sending the notification synchronously (set "fireAndForget" to true). This should directly inform us about the token's validity. Also could you check your Apstrata store to see if you have new documents that contain information about the result of the push (these are usually created by Apstrata in case error occurred while pushing)?

    Also make sure that you are using a token that was obtained with the same API key as the one registered to Apstrata.

  • When "fireAndForget" is set to "true" the push is asynchronous and when set to "false" it is synchronous and you immediately get the result of the push (list of invalid tokens or tokens for which the push failed)
  • Just noticed that you said that you used my code above. This code is for the "ad-hoc" case, i.e. you pass an array of tokens to push to. If you want to push to all tokens of a channel, you should not use the ad-hoc parameters but rather pass the channel id. Is this what you are doing?
  • hi Karim

    I used both scenarios , sending to a channel id and to ad-hoc and I also tried fireAndForget = true

    but still getting this response
    "metadata": {
    "requestId": "e765a33f-e353-4324-9cbd-41e006a3ef45", 
    "status": "success", 
    "statusCode": "200"
    "result": {
    "invalidDeviceTokens": [

    and no notification on my device
  • hi again,

    please can you clarify the below you have mentioned above and I didn't get this:
    "Also make sure that you are using a token that was obtained with the same API key as the one registered to Apstrata"
  • Hi,

    Sorry I realized that my sentence was unclear indeed. This is what I meant : 

    In the Android client application, in order to obtain a registration id (token) for your device, you have to provide GCM with the number of a project that you created in the Google developer console. In the latter, you should have enabled GCM push notifications for the project and you also should have created an API key that you passed to Apstrata (Certificate file, as explained earlier). My point was that you should make sure that the project you are referring to in the Android app is the same one you used to generate the API key.
  • HI karim,

    yes all the above was done and what I put in the application code is the Sender ID which is the Project Number that is created in the GCM and yes the Certificate is uploaded with the same API Key in GCM https://console.developers.google.com/project/

  • hi nour,

    The response you are getting (notably in the synchronous case) shows that there were no errors. Could you please check your Apstrata store to see if new documents were created containing push data (list of invalid tokens, similar to the response you are obtaining when invoking the service). Keep me posted.

  • Hi Karim,

    nothing in the store except a record for an invalid Token in March 18, which was really an invalid token and I got it in the response but what I am testing now is another Valid Token for another device.

  • Hi Karim,

    Is there any way to test if push notification is working on apstrata, do you guys have any test application code to make sure all is working on your server side.
    Demo project or anything.

    because from my side all is done and code implemented, and still nothing happening.

  • Hi Nour,

    I tried from a server-side script and from the API Explorer and both cases are working. 

    I think that you're problem is in the way you are writing the notification. Please make sure it has the following format:

    - from the API explorer: {Content: "some_string"}
    - from a script or as content sent through HTTP to the API : "{\"Content\":\"some_string\"}"

    Example from a script:

    var json = {"Content": JSON.stringify({"text":"hello"})};
    var notificationStr = JSON.stringify(json);

    Keep me posted
  • yes Karim, now with this notification format it is working.

    Thank you
  • Sorry for the invalid example previously given.I added an ERRATA section.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!