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: 2,812
  • PushDesktop.zip
    6.3 KB · Views: 2,517
Last edited:

rwblinn

Well-Known Member
Licensed User
Hi All,

cant get the client to work. Have setup a project in GCM with project name, project id and number. also obtained a server api key.
After modifying the parameter, the desktop receives emails, but for the client when entering a name and press register nothing happens.

I read in this thread, the mention of registering the app at google. How to do that?

Appreciate any advice.
 

rwblinn

Well-Known Member
Licensed User
You only need to receive an API key and project ID from Google.

If the desktop app received an email from the device then the device was registered successfully.

Hi Erel,

the desktop did not receive an email from the device (but emails from my googlemail.com account).
The B4A client, used via the emulator, does not sent an email! The log states "Push service started", but then nothing happens.

Do I need to use the app on the device and can not use the emulator?
 

rwblinn

Well-Known Member
Licensed User
Hi Erel,

got it to work:
  • In pushexample.b4a the senderID is the Google Project Number (12 Digit) and not the Project ID
  • Real Device using Android 2.3.5 did not work - Error Message: Invalid Service
  • Real Device using Android 4.3 registration successful
  • Changed the package name to another then the default
  • In B4J POP3 an error occurs when retrieving the devices - using JavaFX 8 :
    B4X:
    java.lang.NoSuchMethodError: javafx.scene.control.CheckBoxTreeItem.setIndependent(Ljava/lang/Boolean;)V
    This happens when initializing the CheckTreeViewItem in Sub ShowDevices.
  • To test used a registered name direct and message came through
Thanks for your support --- will build further upon this.
 

rwblinn

Well-Known Member
Licensed User
Hi Erel,

as requested
B4X:
main._showdevices (java line: 376)
java.lang.NoSuchMethodError: javafx.scene.control.CheckBoxTreeItem.setIndependent(Ljava/lang/Boolean;)V
    at anywheresoftware.b4j.objects.TreeViewWrapper$CheckBoxTreeItemWrapper.Initialize(TreeViewWrapper.java:158)
    at b4j.example.main._showdevices(main.java:376)
    at b4j.example.main._appstart(main.java:93)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.start(main.java:35)
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:745)
 

rwblinn

Well-Known Member
Licensed User
Hi Erel,

using jFX 2.01 following error when requesting checkboxtreeitem check (... ti.Checked...)- in POP3 example.
B4X:
main._btnsend_action (java line: 123)
java.lang.NoSuchMethodError: javafx.scene.control.CheckBoxTreeItem.isSelected()Ljava/lang/Boolean;
    at anywheresoftware.b4j.objects.TreeViewWrapper$CheckBoxTreeItemWrapper.getChecked(TreeViewWrapper.java:185)
    at b4j.example.main._btnsend_action(main.java:123)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA$2.run(BA.java:165)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:745)
 

rwblinn

Well-Known Member
Licensed User
Hi Erel,

found one more = when setting
B4X:
treeitem.checked = true
following error occurs:
B4X:
java.lang.NoSuchMethodError: javafx.scene.control.CheckBoxTreeItem.setSelected(Ljava/lang/Boolean;)V
 

Levisvv

Member
Licensed User
Erel can you explain how communications with this simple method works offline?
Is the B4J application simply connecting directly to the Android devices over TCP/IP? without needing to connect to the online GCM server? Other than the email needed to register devices, can this work on a closed/secured network that has absolutely no internet access?
 

Giacomo Cuciniello

Member
Licensed User
I have a strange behaviour.
The work on google api is done.
my phone send the mail to the desktop app.
desktop app receive the mail and include my phome in the device list.
when I send a message from the desktop app it seems ok
{"multicast_id":7010969338997093013,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1408805079180448%22199a2af9fd7ecd"}]}
but nothing arrive on the phone.
the app is running on the phone, internet connection is ON but I don't receive any message.
What can I check?
the only difference that I find is that my project ID is of 13 digit and not 12 but registation is achived.
thanks for your help
 
Last edited:

Giacomo Cuciniello

Member
Licensed User
thank you Erel,
I don't understand why but today it is working
LogCat connected to: B4A-Bridge: samsung GT-I9300
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:anywheresoftware.b4a.samples.push
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Service (pushservice) Create **
** Service (pushservice) Start **
** Service (pushservice) Start **
** Service (pushservice) Start **
Bundle[{data=ppo, collapse_key=do_not_collapse, from=1048272702146}]
New message arrived: ppo
I noted the messages arrive even after phone rebott without starting again the app.
thanks again
 
Top