C2DM Help

rfresh

Well-Known Member
Licensed User
Longtime User
OK, I finally got the C2DM system working on my Droid2 phone. At this point I am only using Erel's C2DM sample coding. Mainly the device code and the command line batch file to send the msg.

I have two questions. (1) In the Sub below, what is the Regex function doing to the txtName.Text string?

B4X:
Sub btnRegister_Click
    If Regex.IsMatch("[\d\w]+", txtName.Text) = False Then
        ToastMessageShow("Name not valid", True)
        Return
    End If
    Register(txtName.Text)
End Sub

I want to make the registration of C2DM transparent (if possible) to my users. (2) What name can I use to programatically register their devices?

In playing with the sample code Erel has kindly provided, I just chose 'droid2' because my phone is a Droid2. But clearly this won't work for many users with Droid2 phones as I need to message them one at a time and not as a group. Should I use the device ID string since this a unique string?

Update: I tried to use the Device ID string to register my device but it didn't work and wouldn't register my Droid2. I got the msg: Code=403, invalid server password.

So now I don't know what a correct registration string represents?

Thanks...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The regex is used to only allow alphanumeric characters.
You probably need to escape other characters.

You can use this code to generate a random id:
B4X:
Sub FindRandomId As String
   If File.Exists(File.DirInternal, "id.txt") Then
      Return File.ReadString(File.DirInternal, "id.txt")
   Else
      Dim id As Int
      id = Rnd(0, 0x7fffffff)
      File.WriteString(File.DirInternal, "id.txt")
      Return id
   Return
End Sub

It is unlikely that two devices will get the same value (there is a higher chance that Phone.DeviceId will return the same value for two phones).
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Thanks Erel.

I was able to switch over to using my own database on my own server. And, at least I was able to successfully register my Droid2 phone.

And I can see the record for this ID in my database.

However, I can't send a message to it. Any idea's on what I can check?

B4X:
C:\0C2DM\Desktop>b4a_c2dm.bat send 8200 "new test"

C:\0C2DM\Desktop>java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM send 8200 "new test"
java.lang.RuntimeException: No id found for name: 8200
        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)

Do I need to change anything in the .bat file?
java -cp b4a_c2dm.jar anywheresoftware.b4a.c2dm.C2DM %*
 
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Yes, it is in my database. I created a new unique device id with the random generator code you gave me and registered it successfully. But I still can't send a msg to it. When I shift things back to using your server it works fine but when I try and use my server I can't send to my droid2 phone.

The oauth.txt file data string matches the database record id field.

See attachments...
 
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Yes I configure the file config.txt for my server. I checked it and then saved it again.

Now, for the first time, when I sent from the command line window, it returned success!! But I never saw the 'hello' msg display on my device.

Maybe the msg is delayed?

I have a droid3 phone too. When I send from the command line, won't I have to edit the oauth.txt file and put in the droid3 id string to make it work for that phone?

Update: I was able to successfully send to my droid3 phone!! I will try my droid2 phone again.

My goal is to send msgs from my php website to one phone at a time but have many customer phones to send to. So, I'm not clear yet on how the oauth.txt file would work in that environment?
 
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Ahhhh I see. Ok, I got this working on all three of my devices (droid2, droid3 and a wifi tablet).

Thanks for your patience and help Erel. Can you point me in the direction where I can read about sending these mgs from a php file on a Linux website?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I notice when I unregisted my device, it's record get deleted from my database but not when I uninstall my app. Do we have any way to remove known orphan records?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
The sender app is a java application. Search for a PHP script that runs Java application.

The following may be of interest of others who want to setup their own web server with Java so they can make use of C2DM.

In my situation, I need to make PHP send the C2DM message to my device, so this means installing Java on my Linux server. My hosting company is Hostgator and they gave me these options for my VPS server: install for free Tom Cat Java 5.5 or for a 75.00 install fee, install Tom Cat Java vers 6 or 7.

Here is the next surprise they tell me: My VPS server (and BTW way, they they can't run Java on a shared server because it is too resource hungry, only on VPS or dedicated servers) only has 800MB of ram and they recommend 2MB at a greatly increased monthly cost! I told them to install 5.5 and I'll see how the performance is on my server.

If it turns out I don't have enough ram, I don't think I want to pay 50.00 per month more for enough server memory just to run Tom Cat Java. I'll just have to forget about trying to implement C2DM on a Linux server.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
This Java app will not take more than 16mb. You do not need them to install anything. Just download the Java JRE and unzip it in a folder.

Thanks for info. They already installed Java.


I notice when I unregisted my device, it's record get deleted from my database but not when I uninstall my app. Do we have any way to remove known orphan records?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
This Java app will not take more than 16mb. You do not need them to install anything. Just download the Java JRE and unzip it in a folder.

What files do I need to copy to my Linux server? The .jar file and what about the config.txt file and the oauth.txt file?

I'm having a terrible time getting the PHP script to call the .jar file successfully and am wondering if I am missing other files...right now I only have the .jar file copied over to the Linux server.

Thanks...
 
Upvote 0
Top