Android Tutorial Android push notification (C2DM) framework and tutorial

salmander

Active Member
Licensed User
Longtime User
You said that you were able to run it with the b4aserver url, is this correct?
Yes, when i followed the tutorial first time, it was working fine. Now i have created a new project and trying to follow tutorial but at the moment the service is not even starting. I have pasted my code, can you please tell me wat m i missing?

Thanks.
 

salmander

Active Member
Licensed User
Longtime User
One quick question, can I use the same email address to register for more than one application packages? For e.g: I have used "[email protected]" for "com.test.project1" to register for C2DM. So, if I want to use C2DM in another project (com.test.project2), do I have to register again using "[email protected]" and "com.test.project2"? Or do I have to make another gmail ID?
 

cbal03

Member
Licensed User
Longtime User
Unexpected result from "GetAll"

Hi guys,
I have the app successfully registering and I set the variables in the config.txt file. When I use the desktop commandline as shown.

Sent:
D:\MY Apps\Basic4Android\_tutorial files\C2DM\desk_ext\Desktop>java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM send DEVICENAME "this is a test"

Returned:
java.lang.RuntimeException: java.io.IOException: Server returned HTTP response code: 401 for URL: https://android.apis.google.com/c2dm/send
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:66)
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:63)
at anywheresoftware.b4a.c2dm.C2DM.main(C2DM.java:228)


So I run the GetAll command and I get the raw text - php script from my server in the command window including names and passwords of the database??

Has anyone seen this before?

ok.. so I found that the php script does not conform to my php installation rules. e.g. '<?php' is a required beginning tag instead of '<?'. After correcting this, i get my one device name registered for the GetAll command.


I still haven't been able to track why I get error 401 from C2DM when sending messages to my device.

RETURNED:
java.lang.RuntimeException: java.io.IOException: Server returned HTTP response code: 401 for URL: https://android.apis.google.com/c2dm/send
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:66)
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:63)
at anywheresoftware.b4a.c2dm.C2DM.main(C2DM.java:228)

Thanks for any info.

I have also moved the tool to a directory which doesn't contain spaces. e.g. c:\C2DM
 
Last edited:

salmander

Active Member
Licensed User
Longtime User
I have figured out that in v1.8 you do not need to edit the manifest file manually. All you need to do is write this code in the inbuilt manifest editor.(Project>Manifest Editor).
B4X:
'C2DM Permissions
AddManifestText(<permission android:name="$PACKAGE$.permission.C2D_MESSAGE" android:protectionLevel="signature" />)
AddPermission($PACKAGE$.permission.C2D_MESSAGE)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
' Push Service Receiver Attribute
SetReceiverAttribute(PushService, android:permission, "com.google.android.c2dm.permission.SEND")
' Service Receiver Text
AddReceiverText(PushService,
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="$PACKAGE$" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="$PACKAGE$" />
</intent-filter>)
There is no need to change the package name or add anything, just add this as it is and it should be good to go.
 
Last edited:

cbal03

Member
Licensed User
Longtime User
Thanks for your quick reply Erel.
I did as you suggested and I get the same exact error while using the commandline utility.

I successfully registered my device while pointed to b4a server.

Thank you too salmander.
I did as you sugested and the app timed out after the changes (would not register). I'm not very familiar with the new manifest tool so I deleted the new manifest file, renamed the working backup copy and checked 'do not overwrite' and the app successfully registered again.


This is what I have found so far:
I know my username and password are correct in the config file because I manually sent them to google via the browser and received a sensible response.

sent:
https://www.google.com/accounts/ClientLogin?service=ac2dm&Email=MYEMAIL&Passwd=MYPASSWORD

received:
SID=DQAAALw...
LSID=DQAAAL0...
Auth=DQAAALA...

This led me to believe that my java version you are calling in your batch file might be incorrect.
So I go to Java's webpage and perform a check. I receive the response:
Verified Java Version
Congratulations!
You have the recommended Java installed (1.7.0_01).

Since this version is the latest and should be backwards compatible I 'assume' all is ok here.

I know the board_url and server_password in the config file are correct because if i purposely change the password and then run the tool I get this response:
D:\C2DM>java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM Send MYDEVICE "this is a test"
java.lang.RuntimeException: Wrong server_password.
at anywheresoftware.b4a.c2dm.C2DM.getIdForName(C2DM.java:105)
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:51)
at anywheresoftware.b4a.c2dm.C2DM.main(C2DM.java:228)

I know that my device name is correct because if I change it slightly and then run the tool I get this response:
D:\C2DM>java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM Send WRONGDEVICENAME "this is a test"
java.lang.RuntimeException: No id found for name: WRONGDEVICENAME
at anywheresoftware.b4a.c2dm.C2DM.getIdForName(C2DM.java:100)
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:51)
at anywheresoftware.b4a.c2dm.C2DM.main(C2DM.java:228)

I really appreciate the time you guys are spending. B4A is an awesome IDE and this forum is packed with great information. I'll definitely post the cure if I can find it (unless someone beats me to it).
 

salmander

Active Member
Licensed User
Longtime User
If I am not mistaken, B4A requires java 1.6 to run properly. I don't know if that could be the problem for the desktop java app.
 

salmander

Active Member
Licensed User
Longtime User
"javac: target release 1.5 conflicts with default source release 1.7" - Currently Basic4android requires Java 6. It will not work with Java 7. Both versions can be installed on the same computer."
source: "http://www.b4x.com/android/downloads.html"
 

cbal03

Member
Licensed User
Longtime User
Did you beat me to it?!?

I'll roll it back and see. Thanks!
 

cbal03

Member
Licensed User
Longtime User
B4A runs fine. I remember having to point B4A to a 1.6 java installation when I set it up via Tools > Configure Paths.

I wonder if I need to set an env var to the 1.6 (or other version) location and then edit the commandline tool to use that var.

something like:
newjavavar -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM %*

I have several versions installed since I run other IDEs that require them.
 

salmander

Active Member
Licensed User
Longtime User
B4A runs fine. I remember having to point B4A to a 1.6 java installation when I set it up via Tools > Configure Paths.

I wonder if I need to set an env var to the 1.6 (or other version) location and then edit the commandline tool to use that var.

something like:
newjavavar -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM %*

I have several versions installed since I run other IDEs that require them.
No I don't think, you would need to set any environment variables to set up b4a. The installation is pretty straight forward, just follow the official guide.
 

cbal03

Member
Licensed User
Longtime User
Ok so I validated my java version by forcing the commandline utility to use 3 different versions.

First I read the current version in use:
D:\C2DM>java -version

Result:
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) Client VM (build 21.1-b02, mixed mode, sharing)

java version "1.7.0_01" allows for the result from the commandline utility:
D:\C2DM>java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM send DEVICENAME "this is a test"

Result:
java.lang.RuntimeException: java.io.IOException: Server returned HTTP response code: 401 for URL: https://android.apis.google.com/c2dm/send
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:66)
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:63)
at anywheresoftware.b4a.c2dm.C2DM.main(C2DM.java:228)

So then I try version 1.6.

D:\C2DM>java -version:1.6 -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM send DEVICENAME "this is a test"

Result:
java.lang.RuntimeException: java.io.IOException: Server returned HTTP response code: 401 for URL: https://android.apis.google.com/c2dm/send
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:66)
at anywheresoftware.b4a.c2dm.C2DM.sendMessageTo(C2DM.java:63)
at anywheresoftware.b4a.c2dm.C2DM.main(C2DM.java:228)

So then I try version 1.5.

D:\C2DM>java -version:1.5 -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM send DEVICENAME "this is a test"

Result:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

It appears that version 1.7 and 1.6 are operating the same. So I don't think Java version is at fault here.

I know that the config file is ok because of the responses I get when I make changes to the values.

It would be really nice to see the commandline utility work. It has tons of value for my project.
 

salmander

Active Member
Licensed User
Longtime User
* Make sure you have your gmail id and gmail password in c2dm/desktop/config.txt file for sender_mail_id and sender_mail_password. (This should be the gmail account that you used to signup for c2dm service on google servers.)
* Make sure the board_url in c2dm/desktop/config.txt is of the b4a server (default). This should also be the same for the device/push/PushExample.b4a under BoardUrl process global variable.
* Make sure that DeviceBoardPassword in process globals device/push/PushExample.b4a should be the default value, if you are using the b4a server.
I recommend download the files again and change only package name in manifest file and the gmail address and gmail password in the desktop/config.txt file. And then try running it again. Good Luck mate.
 

cbal03

Member
Licensed User
Longtime User
I found the fault and it took a while to prove it since I had to wait on an email.

It appears that when I registered the project (on googles website) name I spelled it incorrectly. I re-registered the project name (ensuring accuracy) using the same account. Just now I was able to test it and I received:

D:\C2DM>java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM send DEVICENAME "hello"
Success: id=0:blah blah blah

From now on I'll write down what I do before entering it into a web form. Especially when I need to use the info later.

In this case the permissions error 401 was correct. There was no permission for the intended device name to receive a message from from a different project name.

Thank you for your time guys. You are doing wonderful things here! I hope this helps others.

oh.. it works on my server here too! :)
 
Last edited:
Top