Receiving SMS sent from the WTK emulator in a custom application - java

I'm using Sun WTK to run a midlet that needs to send and optionally receive SMS. WMA console can be used to send and receive messages to the midlet but I'd like to do the same thing using my own application.
I have done some sniffing, and noticed that the messages are sent by UDP from the WMA console to the emulator.

After digging inside the jars in WTK I was able to figure out how to send and receive SMS. I had to include the jars kvem.jar and kenv.zip in the application classpath. Tested under Linux.
public static void main(String[] args) throws IOException, PhoneNumberNotAvailableException, InterruptedException {
System.setProperty("kvem.home", "/home/jassuncao/usr/WTK2.5.2");
WMAClient wmaClient = WMAClientFactory.newWMAClient(null, 4);
wmaClient.connect();
wmaClient.setMessageListener(new MessageListener() {
#Override
public void notifyIncomingMessage(WMAClient wmaclient) {
try {
System.out.println("Message received:"+wmaclient.receive());
} catch (IOException e) {
e.printStackTrace();
}
}
});
System.out.println("This number "+wmaClient.getPhoneNumber());
String[] receivers = wmaClient.getKnownReceivers();
for (String receiver : receivers) {
System.out.println("Sending SMS to "+receiver);
Message msg = new Message("Hello world!!");
msg.setFromAddress("sms://"+wmaClient.getPhoneNumber());
msg.setToAddress("sms://"+receiver);
//It seems the ports must be set AFTER the address to work
msg.setToPort(50000);
msg.setFromPort(50000);
wmaClient.send(msg);
}
System.in.read();
wmaClient.unregisterFromServer();
}

Related

how to publish mqtt message with retained true in paho on android

I am using Paho java client library for my on android App. Code provided below.
MqttAndroidClient client_Panic = new MqttAndroidClient(this.getApplicationContext(), serverURL, CLIENT_ID);
try {
MqttConnectOptions options = new MqttConnectOptions();
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
options.setCleanSession(false);
options.setKeepAliveInterval(90);
options.setConnectionTimeout(100);
IMqttToken token = client_Panic.connect(options);
//Few callbacks
} catch (MqttException e) {
e.printStackTrace();
}
And publishing messages, when required
String msg = "messages";
MqttMessage message = new MqttMessage();
message.setPayload(msg.getBytes());
try {
client_Panic.publish(topic, message);
} catch (MqttException e) {
e.printStackTrace();
}
It is working fine, but somehow incomplete. What I need is, whenever other client subscribed to same topic, should get the last retained message, which publisher might had published earlier.
For that I searched on their documentation, I got
protected void setWill(String topic,
MqttMessage msg,
int qos,
boolean retained)
So setWill as per documentation have the option to enable the retained option. So I tried with
options.setConnectionTimeout(100);
options.setWill(topic,null,2,true); // This place I added
IMqttToken token = client_Panic.connect(options);
But got error java.lang.IllegalArgumentException at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateWill on the line containing options.setWill.
Is setWill is the correct method for enabling retained true on android, if yes then what parameters need to be provided or else their is any other method for enabling it on android? TIA.
The Will is a very specific message that is only published if the client disconnects uncleanly from the broker (e.g. network drops).
You can not set a null message as the Will message which is what the error is about.
The retained state is specific to a given message so you do not set it globally, it is set on each message. To mark a message as retained when you just call the setRetained(boolean) e.g.
String msg = "messages";
MqttMessage message = new MqttMessage();
message.setRetained(true);
message.setPayload(msg.getBytes());
try {
client_Panic.publish(topic, message);
} catch (MqttException e) {
e.printStackTrace();
}

pIRCbot won't join Twitch channel or send messages

The following code connects to Twitch's IRC successfully and triggers the onConnect method which is where everything stops.
Because pIRCbot has verbose enabled I can see that it does send JOIN #twitchplayspokemon after connecting but the specification says that I should get an immediate response with the same text followed by a list of users (which would trigger the onJoin and onUserList methods) which doesn't happen.
I have also tried connecting to my channel and sending a message with
joinChannel("#[mychannelname]");
sendMessage("#[mychannelname]", "Hello World");
All this does is send two JOIN commands and doesn't ever join nor message appear in chat.
The tutorial/reference I'm using is http://help.twitch.tv/customer/portal/articles/1302780-twitch-irc
Other responses are also not being output. I am getting the MOTD but not seeing the "End of /MOTD command".
import org.jibble.pircbot.*;
public class MyBotMain extends PircBot {
public static void main(String[] args) throws Exception {
MyBotMain bot = new MyBotMain();
bot.setVerbose(true);
bot.setName("[myname]");
bot.setLogin("[myname]");
try {
bot.connect("irc.twitch.tv", 6667, "oauth:db4aai4mh474ikbgzzuh76fv67n"); // Not the key I'm using
} catch (NickAlreadyInUseException e) {
System.err.println("Nickname is currently in use");
} catch (IrcException e) {
System.err.println("Server did not accept connection");
e.printStackTrace();
}
}
#Override
protected void onConnect() {
System.out.println("Connected!");
joinChannel("#witchplayspokemon");
super.onConnect();
}
#Override
protected void onJoin(String channel, String sender, String login, String hostname) {
System.out.println(login + " joined channel " + channel);
super.onJoin(channel, sender, login, hostname);
}
#Override
protected void onUserList(String channel, User[] users) {
for (User user : users) {
System.out.println(user);
}
super.onUserList(channel, users);
}
}
Your code works perfectly fine, the only problem is that the channel you are trying to join is "witchplayspokemon" instead of "twitchplayspokemon" the onUserList(String channel, User[] users) is only giving me one user, But I have been having that problem recently on my own Bot So I am not yet sure on the cause.

send and receiving message using smack API

I have setup my open fire(jabber server) on local machine with two user testuser1 and testuser2 .using Spark client both users perform chat without any issue,it's nice.
openfire IP -192.168.1.65
I want to use smack API(3.3.0) for send and receiving message. i have write sender side code to send message(with testuser1) and tested with Spark client(with testuser2) message received on testuser2 side,but when i try with java code to receive sender message ,i am not able to receive those publish messages.
Sender.java
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;
public class Sender
{
public static void main(String a[]) throws XMPPException, InterruptedException
{
XMPPConnection connection = new XMPPConnection("192.168.1.65");
System.out.println(connection);
connection.connect();
connection.login("testuser1", "test123");
Chat chat = connection.getChatManager().createChat("testuser2#sameek", new MessageListener() {
public void processMessage(Chat chat, Message message) {
// Print out any messages we get back to standard out.
System.out.println("Received message: " + message);
}
});
chat.sendMessage("Howdy test1!");
while (true) {
Thread.sleep(50);
}
}
}
Receiver.java
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;
public class Receiver
{
public static void main(String a[]) throws XMPPException,, InterruptedException
{
XMPPConnection connection = new XMPPConnection("192.168.1.65");
System.out.println(connection);
connection.connect();
connection.login("testuser2", "test123");
Chat chat = connection.getChatManager().createChat("testuser1#sameek", new MessageListener() {
public void processMessage(Chat chat, Message message) {
// Print out any messages we get back to standard out.
System.out.println("Received message: " + message);
}
});
chat.sendMessage("Howdy test2!");
while (true) {
Thread.sleep(50);
}
}
}
please help me and suggest if i am following wrong approach.
Thanks
I had a similar problem, after following the tutorial here (http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html) and this is what I found:
When you create the chat, you chat the user you want to connect to (eg in my case "user1#gbd038"). This works fine if user1 is using a GUI client such as Spark (which presumably has built-in support and/or error handling for this), and user1 will receive the message. This process attaches the messageListener to a chat now associated with "user1#gbd038". However, when I reply from Spark as user1, the chat that smack receives is actually coming through complete with the location tag, eg:
Received message 'hi' from user1#gbd038/Spark 2.6.3
So it creates a new chat that the application is not listening for (and therefore your application will not receive / print out). I have found two ways to solve this problem:
use the location tag when starting the conversation (although this doesn't seem very scalable or robust):
xmppManager.sendMessage("Hello mate", "user1#gbd038/Spark 2.6.3");
as Robin suggested, use a ChatManagerListener (which will create a new chat when receiving the reply from user1, which can be forwarded to the messageListener):
chatManager = connection.getChatManager();
messageListener = new MyMessageListener();
chatManagerListener = new ChatManagerListener() {
#Override
public void chatCreated(Chat chat, boolean createdLocally) {
chat.addMessageListener(messageListener);
}
};
chatManager.addChatListener(chatManagerListener);
Hope that helps someone in the same position!
You are creating a chat and sending a chat message from both ends but not listening for a chat from either. Use a ChatManagerListener to listen for incoming chats from other clients.

How to read recieved messages in j2me midlet using pushregistry?

i don't know how to read received messages in mobile device using J2ME midlet.Actually I have been sending the messages to other mobiles using SMS gateway.The sms gateway reply to the same mobile device,but i want to read the reply message on the device directly, not going to check the inbox.how to do this in j2me midlet using the PushRegistry concept.please give me the good idea or sample code for me...thanks in advance.
You should use the PushRegistry mechanism for this. In order to do so, you should mark in the .jad file that your application reacts to incoming SMS and also mark the SMS permission.
Put these properties to the JAD file:
MIDlet-Push-1: sms://:10214,hu.bute.daai.example.sms.MidletSMSPushDemo,*
MIDlet-Permissions: javax.microedition.io.PushRegistry, javax.microedition.io.Connector.sms, javax.wireless.messaging.sms.receive
Please note that you should use your own package and MIDlet name. In addition to that you should use the same port for SMS sending as it was defined in the JAD (10214 in the example).
After that when your MIDlet starts, you should call your SMS receiver code to get the SMS:
public class MidletJSMSProxy extends MIDlet {
public void startApp() {
initalize();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void initSMSReceive() {
new Thread() {
public void run() {
MessageConnection conn = null;
try {
String url = "sms://:10214";
MessageConnection conn = (MessageConnection) Connector.open(url);
TextMessage message = (TextMessage)conn.receive();
System.out.println("SMS: "+message.getAddress()+" - "+message.getPayloadText());
}
catch(Exception e){
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
}.start();
}
}
More info:
http://www.developer.nokia.com/Community/Wiki/How_to_launch_a_MIDlet_remotely_via_SMS_or_locally_via_an_Alarm_with_PushRegistry_in_Java_ME

Successful Push Notifications from javapns but not showing up on ios devices

I'm using javapns 2.2 to send push notifications and am getting successful responses from it but the notifications are not showing up on my iOS test phone. I've created a unit test that uses this code from the wiki:
#Test
public void shouldSendATestMessageNotifyingDeviceThatNewMessageIsReady()
throws RepositoryException {
NotificationService nService = new NotificationService();
nService.sendNotification("incoming message",
"4854862ec7b58a939538f2e1262a48a7e8a3331c5f0e11feb11e1a79ff764649");
}
public void sendNotification(String message, String deviceId) {
try {
List<PushedNotification> notifications = Push.alert(message, certLocation, certPassword, false,
deviceId);
for (PushedNotification notification : notifications) {
if (notification.isSuccessful()) {
/* Apple accepted the notification and should deliver it */
System.out.println("Push notification sent successfully to: " +
notification.getDevice().getToken());
/* Still need to query the Feedback Service regularly */
} else {
String invalidToken = notification.getDevice().getToken();
/* Add code here to remove invalidToken from your database */
/* Find out more about what the problem was */
Exception theProblem = notification.getException();
theProblem.printStackTrace();
/* If the problem was an error-response packet returned by Apple, get it */
ResponsePacket theErrorResponse = notification.getResponse();
if (theErrorResponse != null) {
System.out.println(theErrorResponse.getMessage());
}
}
}
} catch (CommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeystoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm using the production push SSL cert (p12) that I created in keychain. I have an adhoc build on my iOS device and I pulled the device Id that I pass into this test from NSLog code in my iOS app for the device I'm using for testing so it is valid.
I swapped out my prod cert with the dev cert and got the invalid token error as I would expect to make sure it was finding the file.
Not sure what else to look at since javapns is telling me the pushed message was sent successfully. Ideas?

Categories

Resources