Android Tutorial Simple Push Framework

It is recommended to use Firebase for new projects: FirebaseNotifications - Push messages / Firebase Cloud Messaging (FCM)

This solution allows you to use the standard GCM push framework without an online server.

SS-2013-12-12_15.48.10.png


You should first be familiar with GCM and get an API Key and Project id as explained here:
Android push notification (GCM) framework and tutorial

The desktop solution is based on this B4J project: Building a mini "Email based server"

When a user registers the device, an email is sent to the desktop tool. The desktop tool then adds the device to an internal store. If the desktop tool is not online then nothing bad happens. The mail will wait until it the tool is started.

The emails are only used for the registration process.

Note that you can send to multiple targets at once with this solution (unlike the other solution).
As it is written in B4J it should be simple to extend it as needed.

In order to use it you should set the ApiKey variable in the B4J project as well as the mailbox parameters.

You also need to set the mailbox parameters on the device (PushService) and set the SenderId in the Main module.

Tips / Notes

- The B4J project depends on jMsgboxes library (and other internal libraries). It also requires B4J v1.05+.
- If you plan to distribute your app on many devices then you should probably not use a free mail service such as Gmail for the devices. You can use the default email client instead. However in that case it is better to "obfuscate" the message before sending it as the user will see the mail content.
 

Attachments

  • PushDevice.zip
    8.6 KB · Views: 3,220
  • PushDesktop.zip
    6.3 KB · Views: 2,945
Last edited:

schimanski

Well-Known Member
Licensed User
Longtime User
Hello!

I'm trying the simple push framework, but it is not possible to send a message to my devices.The devices are registert very well, but I always get the message: Error sending message: Unauthorized....:(

B4X:
Program started.
Checking mails
No messages.
Feb 25, 2014 6:57:04 PM org.apache.http.impl.client.DefaultRequestDirector handleResponse
Warnung: Authentication error: Unable to respond to any of these challenges: {}
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Error sending message: Unauthorized
 

schimanski

Well-Known Member
Licensed User
Longtime User
Thanks for your replay, Erel..

But I have tried the server-api-key and a browser api-Key, but both the same Problem....

I'm not able to see, where the Problem is...
 

schimanski

Well-Known Member
Licensed User
Longtime User
For my server-api key, I have used the IP 0.0.0.0/0.

Could that be the Problem?
 

schimanski

Well-Known Member
Licensed User
Longtime User
Yes, I have activated the following features:

BigQuery API ON
Google Cloud Messaging for Android ON
Google Cloud SQL ON
Google Cloud Storage ON
Google Cloud Storage JSON API ON
 

schimanski

Well-Known Member
Licensed User
Longtime User
:( I have now make a complete new google-account with a brand new api-key...but it is always the same problem...


B4X:
Program started.
Checking mails
Found messages: 1
1
Message from:  xxxxxx@gmail.com, subject:  add
Testname: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Feb 28, 2014 9:49:50 AM org.apache.http.impl.client.DefaultRequestDirector handleResponse
Warnung: Authentication error: Unable to respond to any of these challenges: {}
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Error sending message: Unauthorized

I'm not sure, if there is something to do for me in

Next Steps
Once you've finished the tasks listed above, you're ready to start implementing GCM. Here is an overview of the basic steps:

  1. Decide which Google-provided GCM connection server you want to use— HTTP or XMPP (CCS). GCM connection servers take messages from a 3rd-party application server (written by you) and send them to a GCM-enabled Android application (the "client app," also written by you) running on a device.
  2. Implement an application server (the "3rd-party application server") to interact with your chosen GCM connection server. The app server sends data to a GCM-enabled Android client application via the GCM connection server. For more information about implementing the server side, see Implementing GCM Server.
  3. Write your client app. This is the GCM-enabled Android application that runs on a device. See Implementing GCM Client for more information.
 

schimanski

Well-Known Member
Licensed User
Longtime User
Jo, now I have tried the browser-key with the new account...and it runs perfect...:)
 

schimanski

Well-Known Member
Licensed User
Longtime User
I have one question about the gcm: Which data is stored by google, when I register my device? I thought, that it is my device-name and the id, which I get from google. When receiving a message, I get the sender in 'from' and the message in 'data'. But the sender in from is not my regitered name. It is only a number.???
 

schimanski

Well-Known Member
Licensed User
Longtime User
When I copy the files on my usb-stick an starts the POP3.jar on another windows-computer, I get the following error, after clicking 'Check Now'.

03/26/2014 19:04:49: Error: (SunCertPathBuilderException) sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

what is the problem?
 

schimanski

Well-Known Member
Licensed User
Longtime User
Now I have installed b4j on my second windows 7-notebook and compiled the pop3.jar directly on the notebook, but I always get the same error....

???:(
 

JOANORSKY

Member
Licensed User
Longtime User
Is there a way to get the currently subscribed devices on the GCM? This would be great in order to implement a real-time subscription UI button (like.. GREEN if already subscribed or RED if not yet subscribed).. well.. i guess you get the idea..
 

Harris

Expert
Licensed User
Longtime User
Is there a way to get the currently subscribed devices on the GCM?

All registered devices should update your table. This is how I know how many "paid" subscribers can access a users (owner) account.

In order for a device to register, they must be provided - by the owner of the account - with a reg ID. Some one has to communicate to them this key (GCM id). Now, all messages produced from the client (device) or the server are passed thru this account.

I have even configured this: messages sent from a device can be forwarded to another device - once it has reached the server.
This is handy when someone cannot monitor the web app, but can receive all messages from the "fleet" on their device when away from their desktop.

Now, to implement: respond directly to these messages from the sender. Not so difficult as I see.

Summary: GCM is a powerful (free) service. It works 99% of the time (the 1 percent failure likely due to me).
It is more akin to magic than something I can understand fully. Also, Android handles this differently from 2.3 to 4.4.2 (as I have noticed).

I have example code if you need.

Thanks
 

schimanski

Well-Known Member
Licensed User
Longtime User
Hello!

Ich have two questions about the GCM. Perhaps someone knows an answer:

1.) Is it a problem, when I change the packagename of my app, after some devices has registered on the GCM? I think, that all devices have to be registered again under the new packagename, but I don't know it.

2.) Google says, that it is possible to send up to 1000 messages at the same time:

Multicast messages
In GCM you can send the same message to multiple devices simultaneously. For example, a sports app wanting
to deliver a score update to fans can now send the message to up to 1000 registration IDs in the same request
(requires JSON).

But a push-mail can't be bigger then 4kb. Does anybody know, how much push-IDs could be send in one push-mail? I have tested it until now with up to 50 IDs in one push without problems, but I don't know, how much IDs one push can hold?

Thanks for help...
 
Top