I am developing an App for read notifications all my code work great, but have two problems. I use this code for read notifications:
String pack = sbn.getPackageName();
String ticker ="";
if(sbn.getNotification().tickerText !=null) {
ticker = sbn.getNotification().tickerText.toString();
}
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
//String text = extras.getCharSequence("android.text").toString();
String text = null;
if (extras.getCharSequence("android.text") != null) {
text =(String) extras.getCharSequence("android.text");
}
if (text == null) {
// for whatsapp on kitkat second whats app text
// will be null
if (extras.get("android.textLines") != null) {
CharSequence[] charText = (CharSequence[]) extras
.get("android.textLines");
if (charText.length > 0) {
text = charText[charText.length - 1].toString();
}
}
}
The code works, but the problem is with WhatsApp, when I receive more than one message I get the following structure.
Title: NameUser
Text: Hi, how are you? <---- This is ok!
but the second message I get
Title: NameUser
Text : Hi again! <--- This is ok;
Title: NameUser
Text: 2 New messages <---- Agggr that is wrong!!!!!
How can avoid the "2 New messages"??
And the second question is Can I get images send via whatsapp? For the moment I only get the following structure.
Title: NameUser
Text: Image
Thanks in advance
There is no way you can influence (from your app) what notifications are sent by some other app's server to your phone.
Related
I am trying to make an app that sends files from my Android Watch to my Android Phone.
The problem I have is that if I record and save multiple files and send all of them at the same time, I do not get all the files back on the phone side. I only receive one file.
The code for sending the file is as follows. This code is implemented on the Watch side.:
public void sendData(View v){
String fname = "_Activity.bin";
int FileCounterCopy = FileCounter;
if(mGoogleApiClient.isConnected()){
for (int i = 0; i < FileCounterCopy ; i++){
String FileName = String.valueOf(i) + fname;
File dataFile = new File(Environment.getExternalStorageDirectory(), FileName);
Log.i("Path", Environment.getExternalStorageDirectory().toString());
Log.i("file", dataFile.toString());
Asset dataAsset = createAssetfromBin(dataFile);
sensorData = PutDataMapRequest.create(SENSOR_DATA_PATH);
sensorData.getDataMap().putAsset("File", dataAsset);
PutDataRequest request = sensorData.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, request).setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
#Override
public void onResult(DataApi.DataItemResult dataItemResult) {
Log.e("SENDING IMAGE WAS SUCCESSFUL: ", String.valueOf(dataItemResult.getStatus().isSuccess()));
}
});
boolean deleted = dataFile.delete();
Log.i("Deleted", String.valueOf(deleted));
FileCounter--;
}
mTextView.setText(String.valueOf(FileCounter));
Return();
}
else {
Log.d("Not", "Connecteddddddddd");
}
}
The code for receiving the files is as follows and is implemented on the phone side.
#Override
public void onDataChanged(DataEventBuffer dataEvents) {
Counter++;
final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
dataEvents.close();
Log.e("List Size: ", String.valueOf(events.size()));
for (DataEvent event : events) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
Log.v("Data is changed", "========================");
String path = event.getDataItem().getUri().getPath();
if (SENSOR_DATA_PATH.equals(path)) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
fileAsset = dataMapItem.getDataMap().getAsset("File");
myRunnable = createRunnable();
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
new Thread(myRunnable).start();
}
}
}
status.setText("Received" + " File_"+ String.valueOf(Counter) );
}
Right before the for loop, I check the size of the event and it only shows a size of 1, no matter how many files I save.
I am stuck on how to implement this (tbh I used code from youtube video/online resources so I am not 100% sure on how some of the api works).
Thanks in advance!
You're putting all of the files at the same path, with nothing to differentiate them - so each one you put in overwrites the previous ones. The Data API works much like a filesystem in this regard.
In your sendData method, you need code something like this:
sensorData = PutDataMapRequest.create(SENSOR_DATA_PATH + '/' + dataFile.toString());
And then in onDataChanged, either only check the path prefix...
if (path.startsWith(SENSOR_DATA_PATH)) {
...or, preferably, put the value of SENSOR_DATA_PATH in your manifest declaration as an android:pathPrefix element in the intent-filter of your data receiver. You can then remove the path check from your Java code completely. Docs for that are here: https://developers.google.com/android/reference/com/google/android/gms/wearable/WearableListenerService
One other thing: it's good practice to clear stuff like these files out of the Data API when you're done using them, so that they're not taking up space there.
I am working on a Spring-MVC webapplication in which we are trying to get a screenshot of an URL. Currently I am using PhantomJS for that task, but it's too slow(>10seconds). Also, the URL's have to be with http/https and www for detecting that it's an URL. As this is a chat application, there can be simple URL's which users add like helloworld.com. Any help would be nice. Thank you.
Code:
String[] words = message.split(" ");
for( String item : words ){
boolean val = ResourceUtils.isUrl(item);
if(val){
urlIdentifier = calcUrl(item);
break;
}else {
System.out.println("Url false is "+item);
}
}
if(urlIdentifier!=null){
replies.setPreviewIdentifier(urlIdentifier);
input.put("preview_identifier",urlIdentifier);
}
Method to calculate screenshot :
private String calcUrl(String website){
try {
String identifier = String.valueOf(new BigInteger(130, random).toString(32));
String previewLocation = msg + "chatthumbs/" + identifier ;
Process proc = Runtime.getRuntime().exec("phantomjs --ssl-protocol=any " +
"/home/deploy/phantom/rasterizepdf.js " +" "+website+" " +previewLocation);
proc.waitFor();
BufferedImage image = ImageIO.read(new File("/home/akshay/testme.png"));
if(image!=null){
if (image.getWidth() > image.getHeight()) {
image = Scalr.resize(image, Scalr.Mode.FIT_TO_HEIGHT, 250, 250);
} else {
image = Scalr.resize(image, Scalr.Mode.FIT_TO_WIDTH, 250, 250);
}
image = Scalr.crop(image, 250, 250);
ImageIO.write(image, "png", new File(previewLocation));
}
return identifier;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
Any help would be nice. Thank you.
(a) I think the process of taking a screen shot is taking time. Is this code running on the same device as the chat screen? Why not use java.awt.Robot to take the screen shot ? or just save the text why do you need a screen shot?
(b) Is the system too busy/ Use on a SSD to see if faster?
(c) But curious as to the end application, is this part of a web app? How will your code run on the client systems? Or will you java agent be installed on all user systems that monitors the web page and takes screen shots? Then why use a web page, use a java app to chat, and parse the text as typed.
Parsing the text. What if user types/ pastes a long message? Are you parsing everything once or on change? Think of ways to improve that if it seems to be a problem. Ignore if not the immediate issue now.
Also if the msg is too long the parsing can take a lot of time. maybe process after every key press or change event (if paste) keep local js copy of previous text to get diff?
Tried everything on stackoverflow, now i am being able to take the msg frwd. But the msg is not sent until you actually click a no. from whatsApp contact list ..... Plz help been stuck here for few days....
Thing is that i am trying to build an app, in which when I give a number then msg should be sent to it using whatsapp. With this code msg is being frwded and when I select a number it sends the pre-defined msg to it. But I want that the msg should be directly sent without whatsApp waiting for user to click on a number..
//checks if whats app is installed or not..
private boolean whatsappInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
//Main Process
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
String a="91*********0";// the nuber to which msg is to be sent
Uri uri = Uri.parse("smsto:" + a);
Intent sendIntent = new Intent(Intent.ACTION_SEND, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Test message"+a);//msg to be sent
//sendIntent.putExtra("sms_body", "ydyeryyerf");
sendIntent.setType("text/plain");// type of msg->text
sendIntent.putExtra("chat",true);
sendIntent.setPackage("com.whatsapp");// picks whats app
// startActivity(sendIntent);
startActivity(Intent.createChooser(sendIntent, a));//starts whats app
} else {
// should redriect to play store to download whatsApp
Toast.makeText(getApplicationContext(), "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent playStore = new Intent(Intent.ACTION_VIEW, uri);
startActivity(playStore);
}
Instead of using Uri uri = Uri.parse("smsto:" + a);
use sendIntent.putExtra("jid", + number + "#s.whatsapp.net")
"number" should be the actual number with no leading +
Short answer: This is not possible.
You can not do this unless you build your own implementation for whatsapp api and in this case you will need sender phone number and password and this can not be done pragmatically. Long steps to do this will be as follow :
1 - bring rooted mobile phone and put sender sim card in
2 - get the files that whatsapp stores password in and get the password
3 - implement whatsapp mini client that, using this user name and password, send your message.
I am working on android application, which recognize the voice and convert the voice into text as a background process, it is working fine yet, but my next step is to open different built-in activities like message, gallery, email camera through intents,
my code is
public void onResults(Bundle b) {
ArrayList<String> as = b
.getStringArrayList(sr.RESULTS_RECOGNITION);
String res = "";
for (int i = 0; i < as.size(); i++) {
Log.e("OnResult", as.get(i));
res += as.get(i);
}
if (res.equalsIgnoreCase("message")) {
Intent i = new Intent(Intent.ACTION_SEND);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);
}
}
I am getting exception at this :(
Anyone help, I would appreciate , peace,
I am trying to make a notification message appear when I click the menu item, it shows me the indicator but I don't see the message, can someone explain where i did wrong :
private MenuItem AMenu = new MenuItem("Notify", 101, 10)
{
public void run()
{
ReadableListImpl mylist= new ReadableListImpl();
ApplicationMessageFolder folder = null;
if(ApplicationMessageFolderRegistry.getInstance().getApplicationFolder(0x33c7ce29883abe5fL)==null){
folder = ApplicationMessageFolderRegistry.getInstance().registerFolder(
0x33c7ce29883abe5fL, "Test Folder", mylist );
}else {
folder = ApplicationMessageFolderRegistry.getInstance().getApplicationFolder(0x33c7ce29883abe5fL);
}
//DemoMessage source is available in the messagelistdemo.
DemoMessage msg = new DemoMessage("me#here.com", "Pizza Toppings","What would you like on your pizza?", System.currentTimeMillis());
mylist.addMessage(msg);
folder.fireElementAdded(msg,true);
System.out.println("nr of messages"+folder.hasNewMessages());
ApplicationIndicatorRegistry reg =
ApplicationIndicatorRegistry.getInstance();
EncodedImage image = EncodedImage.getEncodedImageResource("new.png" );
ApplicationIcon icon = new ApplicationIcon( image );
ApplicationIndicator indicator = reg.register( icon, false, true);
ApplicationIndicator appIndicator = reg.getApplicationIndicator();
appIndicator.setIcon(icon);
appIndicator.setValue(appIndicator.getValue() + 1);
appIndicator.setNotificationState(true);
appIndicator.setVisible(true);;
}
};
I noticed two things looking at your code:
First, you create a new ReadableListImpl each time the menu item gets invoked. This means the ReadableListImpl instance you add the message to is not always the same as the one that was used when registering the folder. So your code should work on first invocation but not on subsequent ones.
Second, with BB OS 6 a message can appear in two places, the home screen (notification bar) and the message list (the 'Messages' app). It might be possible that your message did actually show up in the message list but not in the notification bar. From my experience messages show up in the notification bar only if the message's status is ApplicationMessage.Status.UNOPENED.
Use ApplicationFolderIntegrationConfig if you want to have control over where your message should show up.