B4A Library Parse Library – Push Notifications and Cloud Storage

Parse service is shutting down: http://blog.parse.com/announcements/moving-on/

Overview


This library is a rather extensive overhaul of and update to Erels’ original Parse library.

Parse is a company that provides several services for mobile applications and has just been purchased by Facebook so it looks like it should continue to be a stable and reliable service.

You create an account on Parse.com and then create what they call “Applications” which would usually correspond with individual Android applications but don’t have to, so several different Android applications could access a common body of data in a single Parse application. Within Parse applications there are “Classes” of data which look much like a database table and contain ParseObjects which look like an individual row of data in a table.

Capabilities

The two major uses for this library are as follows

1) Providing cloud based data storage and retrieval for data held in the individual Parse Applications in a simple way including querying the stored data. No need to mess with HTTP and networking, it is all done for you in an object oriented fashion.

2) Supporting Push Notifications which can be instigated both from the Parse Web Dashboard of your Parse account and, if you have enabled it in the Dashboard for that Parse application, also from devices running the corresponding Android application. Push Notifications are sent in the context of the individual Parse Applications whose Android applications have registered to receive them but individual devices, or groups of devices, may be selectively targeted by using “Channels”.

Parse offer three levels of service, the first being free as long as you keep within their limits which are one million API Requests per month, one million Push Notifications per month and one Gigabyte of storage use. You can exceed these limits without upgrading to the next paid plan, which is presently 199 US dollars per month if you register a credit card. You can exceed these limits and be charged 7 US cents per one thousand excess API Requests, 20 UC cents per one thousand Pushes and 20 US cents per one Gigabyte of excess storage. There is information on your usage of all three available in your Parse Dashboard.

An API Request is a single network operation which are the ones that raise Basic4android events from the library.

The definition of the number of Pushes is “The total number of push notifications queued for delivery to a client device”. I may be being stupid but that seems ambiguous to me and I don’t understand whether that means that the total number of Push Notifications counts as each single message sent to lots of devices or as the total number of devices to which a single message is sent. I suspect the latter. If anyone knows definitively than please post and I will amend this paragraph.

The amount of storage used is obvious and needs no explanation.

Please read the Parse Android Guide, in the Parse.com Documentation section of their web site, for more information about how to use this library. The Parse Android API document might also be useful occasionally. The mapping of this library objects methods to those of the Java API are usually obvious.

All the operations which need to access the Parse online service are done asynchronously and raise events when complete whose parameters include a Success indication. If an error occurs Success will be False and a ParseException with a message describing the error will be placed in LastException. If necessary this should be processed in the event code as there is a possibility that a further error could occur in another asynchronous call that would overwrite the existing LastException.

Note that these asynchronous operations do not necessarily complete in the order in which they are invoked. To deal with this possibility a TaskID parameter is provided in each call that can be used, if required, to identify which of several possible method calls has just completed.

Installation instructions

In order to use this library you should sign up to Parse.com, go to your Dashboard and create a new Parse application called whatever you feel like calling it. You will need two keys, Application Id and Client Key, which you will find under the Settings for your application. Both are required and need to be copied to the appropriate Process Global variables in the demo.

You also need to download the Parse Android SDK from the Downloads section of the Parse.com web site. Inside the zip file you will find a file named Parse-1.2.4.jar or similar. Copy this to your Additional Libraries folder and rename it ParseNative.jar. Also put Parse.jar and Parse.xml from the demo archive in your Additional Libraries folder.

As detailed in the help for Parse. EnableNotifications in order for your application to receive Push Notifications you need to add the following to the Manifest Editor in your project.
B4X:
SetApplicationAttribute(android:name,"anywheresoftware.b4a.objects.ParseObjectWrapper$ParseApplication")

AddApplicationText(<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.USER_PRESENT" />
  </intent-filter>
</receiver>)

Version 1.1 of this library specified the Parse keys in a Parse.Initialize method. This method no longer exists in version 1.2 and later. This is because it it was called from within an Activity, but if Android killed your process and then tried to restart the Parse Service this failed because the service started without the activity being created and so was not initialised correctly. Initialisation now occurs on Application creation and requires the library Application class name to be registered in the manifest and the Parse keys to be specified as an xml resource. When the application is loaded you will see the values in the xml file echoed to the filtered logs.

You also need to place a file called res.xml in your project Objects/res/xml folder and make it read-only. The xml folder does not exist in the project archive but will be created at your first compile. The contents of res.xml should specify your Parse keys as follows
B4X:
<!--?xml version="1.0" encoding="utf-8"?-->
<data>
   <data1 value="YourParseApplicationAppID" />
   <data2 value="YourParseApplicationClientKey" />
</data>

The demo project is already set up to receive push notifications. Try sending one from your Parse Application using the Parse Dashboard.

Run the demo and have a play.

Issues

I have found that using IE10 to send a Push from the Parse Dashboard usually causes the Push to be sent twice, you will see this in the Dashboard list of pushes sent and also at the device and of course they both count towards your free total. Chrome appears to work correctly and sends only one. The work-around is obvious.

The Parse library starts a service called PushService to receive the Pushes and which you can see in Settings -> Apps -> Running -> ParseDemo. Prior to Parse SDK 1.2.4 sometimes this service stopped receiving Pushes and the Process needed to be stopped using a Task Manager and restarted. Parse SDK 1.2.5 should have fixed this.

EDIT:- Version 1.2 now posted. See post #6 for details.

EDIT:- Version 1.3 now posted. See post #13 for details.

EDIT:- Version 1.4 now posted. See post #17 for details.
 

Attachments

  • Parse1.4.zip
    43.2 KB · Views: 1,207
Last edited by a moderator:

uswin4224

Member
Licensed User
Longtime User
agraham, after I implemented my code it seem to work fine if I sent a PN using JSON string , but If I send a PN using a text normal message, the app will throw out error due to unable to find an intent.

How can I catch an exception for this ? is there anyway to check whether the PN that we received is a message or a JSON script ?

need some enlightenment here. Thanks
 

uswin4224

Member
Licensed User
Longtime User
You can catch any exception with a Try / Catch block.

About the JSON string. You can check whether the string starts with a [ or {. Another option is to try to parse it and if it fails then treat it as a regular string.

I see, okay let me try this Erel.
Thanks a lot
 

bluedude

Well-Known Member
Licensed User
Longtime User
Planning to use this because of scheduled notifications

Hi agraham,

I'm planning to use parse.com because it has scheduled notifications and GCM has not. I have however a few questions about your sample:

- what is exactly the content of the message you are sending through the push notification dashboard?
- is "push_hash" a unique hash that you create when sending the message?
- do you have hands on experience with scheduled notifications? How exact on time are they?
- do we need to have a service module to run this when activity is closed?

Cheers and thanks for adding the push notifications to the library.
 

agraham

Expert
Licensed User
Longtime User
what is exactly the content of the message you are sending through the push notification dashboard?
Doesn't matter, anything.
is "push_hash" a unique hash that you create when sending the message?
No, it's part of the notification created by Parse.
do you have hands on experience with scheduled notifications? How exact on time are they?
Never tried it. Have a go yourself.
do we need to have a service module to run this when activity is closed?
No, The demo is complete and doesn't so it is not needed. The library initialises Parse which includes its own service which starts your designated activity when a notification arrives.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Just saw that scheduled is part of Pro and not the free version, no option for me currently.

Cheers,
 

Widowmaker

New Member
Licensed User
Longtime User
Push Notifications and normal notifications

Great Library, all running okay, but if I may ask (and I'm probably being a bit thick). I can see that I can control what happens when the notification is selected but can I actually control the notification.

I will be notified by the push that a sale(s) has occurred on my online shops and the amount , using channels I would want to have one alert per shop but keep track of the totakl sales, eg after 2 hours shop xxxx has had 14 sales for £325.66 sort of thing, and not a long list of 14 notifications just 1 per shop. I would need acces to notifications to update the title and text, I would also like a different sound for each shop if poss.

Hope that make sense.

Tony:sign0104:
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi Agraham,
I'm just studied your library version 1.4,but when I 've download SDK of Parse,I could get only version 1.3.1 which isn't 1.2.5. I don't know that Is it work?
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi Agrahm,
I 've tried already follow as readme.txt,but it's not work. please see attached picture.
 

Attachments

  • error.png
    error.png
    33.5 KB · Views: 186

Theera

Well-Known Member
Licensed User
Longtime User
I've just downloaded and tried 1.3.1 and it works fine in the demo for me. Check your unfiltered logs.

Hi Agraham,
Please see my details which attached. Is there something wrong?
 

Attachments

  • showonly.png
    showonly.png
    2 KB · Views: 173
  • ManifestFile.png
    ManifestFile.png
    22.1 KB · Views: 175

Theera

Well-Known Member
Licensed User
Longtime User
Hi Agraham,

After I've read (I would like to say that I'm not understand all of them,because I'm understand only short message.) I create res.xml and input info as you have shown and then set the file to be read-only. I have run again,I receive the log messages is below. Should I do continue? I'm sorry to make you tried with me.
 

Attachments

  • logfile1.png
    logfile1.png
    13 KB · Views: 165

derez

Expert
Licensed User
Longtime User
Thank you for that library, Agraham.
I followed the instructions and I got it working nicely.
I still have a question about notification -
When I send a test notification from Parse I get it but otherwise I don't get any. What triggers the notifications in the demo application ?
Edit: The test notification from parse doesn't go through notification activity.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
-
When I send a test notification from Parse I get it but otherwise I don't get any. What triggers the notifications in the demo application ?
Edit: The test notification from parse doesn't go through notification activity.
Sorry but I don't understand what you mean.
 

derez

Expert
Licensed User
Longtime User
In Notification activity there is a msgbox that should be displayed when a notification arrive:
B4X:
msg = "My Push Notification Received!" & CRLF
   msg = msg & "Channel = " & Channel & CRLF
   msg = msg & "Alert = " & Alert & CRLF
   msg = msg & "Hash = " & Hash & CRLF
   Msgbox(msg, title)
I never got one.

I enabled push in Parse and a test notification is recieved in my device when I send one from the Parse site. The question is what triggers Parse to send the notification that display the msgbox above ? or - what am I missing in the process (b4a and Parse) ?
Thanks
 
Last edited:

derez

Expert
Licensed User
Longtime User
Glad to hear that :)
What do I need to do to get a notification when a data item is added (for example named "Red") ?

Edit: I saw that pressing on the notification icon shows the msgbox, so this is solved for me, but the question above is still valid.
 
Last edited:
Top