Through my android app I want to clear/remove message notification (that is generated by default in android device) of an incoming message if it contains keyword. Can anyone please help how can I do that?
use NotificationListenerService api and get notification id and text then clear notification by notification id.
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = "";
String ticker = "";
String title = "";
String text = "";
String postTime = "";
if (sbn.getPackageName() != null)
pack = sbn.getPackageName();
postTime = sbn.getPostTime() + "";
Bundle extras = sbn.getNotification().extras;
CharSequence data = extras.getCharSequence(Notification.EXTRA_TITLE);
extras.getCharSequence(Notification.EXTRA_TITLE);
if (data != null) {// notification title
title = data.toString();
}
data = extras.getCharSequence(Notification.EXTRA_TEXT);
if (data != null) {// notification text
text = data.toString();
}
String id= sbn.getKey() ;// notification id
}
Related
I'm working on an android application that listens for notifications. Is there any way to get the name of the app which created the notification.
You need to get the package name first on receiving the new notification from the notification service::
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String ticker = new String();
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();
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap id = sbn.getNotification().largeIcon;
}
And then use the "pack" value to get the app name:
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( pack, 0);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ?
pm.getApplicationLabel(ai) : "(unknown)");
Is there a way to listen if a notification is updated. For example if I receive a message in Messenger my notification listener will notice that, but once I receive a second message - the notification listener ignores that.
I have tried with cancelling notifications, but that did not work
My notification listener:
public class Reader extends NotificationListenerService{
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
int id = sbn.getId();
String ticker = sbn.getNotification().tickerText.toString();
Bundle extras = sbn.getNotification().extras;
final String title = extras.getString("android.title");
String tag = sbn.getTag();
int ides = extras.getInt("android.id");
final String ss = extras.getString("android.text").toString();
final String text = ss.toLowerCase();
if (pack.equals("com.facebook.orca")) {
String e = "Tah";
Log.e(e, pack);
Log.e(e, ticker);
Log.e(e, title);
Log.e(e, ss);
Intent msgrcv = new Intent("Msg");
msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", ">" + text + "");
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}
}
It says null to my message, and i want to take message that i write from another class, is it possible ? Can anyone help to see what's my error ? I've tried moving button to this class too, but it wont work either,
public class ReceiveMessage extends BroadcastReceiver {
private Button btnchange;
EditText qwer;
private Context ctx;
EditText text;
TextView asd;
static TextView messageBox;
static String phoneNumber;
private EditText pesan;
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] messages = (Object[]) bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
{
sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
break;
}
for (SmsMessage msg : sms)
{
String msga = msg.getMessageBody();
String phoneNumber1 = msg.getOriginatingAddress();
String name = getContactName(context, phoneNumber1);
if (name != null && !name.equals("null"))
{
SmsManager smsManager = SmsManager.getDefault();
String sendTo = phoneNumber1;
// i want to locate my message that i write from another class
asd = (TextView) findViewById (R.id.textView1);
qwer = (EditText) findViewById (R.id.editText1);
try {
// here's the message that i want to send but the program says null inside my msg
String smsMessage = "Hi " +name+ "\n\n" +qwer.getText().toString()+ "\n\nThx";
smsManager.sendTextMessage(sendTo, null, smsMessage, null,
null);
}catch(Exception ex){
ex.printStackTrace();
System.out.println("errror karena " +ex.getMessage());
}
}
}
}
private EditText findViewById (int e) {
return null;
}
}
You can not access the another class inflated view from this class.
You need to check if you've declare this receiver in the manifest.
Have a look on this stack overflow question.
Android - SMS Broadcast receiver
I make an SMS app which simple perform sending and receiving SMS ... everything is ok but when i send smilays or emojis it display emojis code not emois i used edittext for sending sms and textview for receiving sms i also changed input type into longmessage but still have the same issue ... I want to show proper smilays when anyone send me and when i send anyone ....
my english is not good plz dont mind
for example i want to send happy smile to my friend when i pick default android emojis its show me emojis code like :) :( :P not show me the emojis
private void SendSms() {
// TODO Auto-generated method stub
mPhoneNumber = FriendData.getNumber();
mMessage=messagetosend.getText().toString();
SmsManager smManager=SmsManager.getDefault();
ArrayList<String> parts = smManager.divideMessage(mMessage);
smManager.sendMultipartTextMessage(mPhoneNumber, null, parts, null, null);
//String userInput = messagetosend.getText().toString();
try
{
ContentValues values = new ContentValues();
values.put("address", mPhoneNumber);
values.put("body", mMessage);
values.put("date", DateFormat.getDateTimeInstance().format(new Date()));
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
Calendar c = Calendar.getInstance();
SenderMessages(mMessage, c.getTimeInMillis());
messagetosend.setText("");
Toast.makeText(getApplicationContext(), " Message Sent", Toast.LENGTH_LONG).show();
// finish();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
#Override
public void onReceive(Context contx, Intent intent) {
String body = "";
String number = "";
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
// ---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
body += msgs[i].getMessageBody().toString();
number +=msgs[i].getOriginatingAddress();
}
}
try
{
Toast.makeText(contx, number, Toast.LENGTH_LONG).show();
Toast.makeText(contx, body, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(contx, number, Toast.LENGTH_LONG).show();
Toast.makeText(contx, body, Toast.LENGTH_LONG).show();
}
}
public void ReciverMessages(String message , long time){
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.receiver_activity, null);
mContainerView.addView(view);
TextView texttime = (TextView) view.findViewById(R.id.time_date_receiver);
TextView textmessage = (TextView) view.findViewById(R.id.tvreceiver);
//DateFormat format = new DateFormat();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String MSGDate = df.format(time);
if(MSGDate.equalsIgnoreCase(Utils.GetCurrentDate()))
{
SimpleDateFormat df1 = new SimpleDateFormat("HH:mm");
String MSGDate1 = df1.format(time);
texttime.setText(MSGDate1);
}else
{
texttime.setText(MSGDate);
}
textmessage.setText(message);
}
I found a very useful Emoticon Keyboard. This keyboard is not using Unicode sequences but rather just local image assets. I am thinking that this type of keyboard can only be useful within this app and not with other apps or Operating Systems.
So instead I am replacing the ImageView containing an asset with a TextView containing a Unicode sequence.
After cross referencing Supported Unicode Sequences as well as the Visual Unicode Database I realized that \u1F601 was a 32 bit Unicode representation, and the 16bit representation can be set like :
EditText messageInput = (EditText) findViewById(R.id.message_input);
messageInput.getText().append("\ud83d\ude01");
Implementations of Emoji (Emoticon) View/Keyboard Layouts
I can receive my mails from Gmail Server and show theme on a listview on my Android Project. Yesterday, I started to change my mail's imageview for "unread" or "read" situation. But, then I realized that when my application connected to Gmail Server and receiving my mails, the RECENT mails become SEEN mails. And because of this reason I can't set my imageview's for unread mails.
I mean; I want to receive my mails from Gmail Server without changing their situations on Gmail Server. I want to receive them 3 unread and 4 read as in server.
What should I do for doing that?
My connection code sample is:
public Message[] ConnectionToServer(String email, String password)
throws Exception
{
Properties props = System.getProperties();
props.setProperty("mail.imaps.partialfetch", "false");
URLName server = new URLName("imaps://" + email + ":" + password + "#imap.gmail.com/INBOX");
Session session = Session.getDefaultInstance(props, null);
folder = session.getFolder(server);
if (folder == null)
{
System.exit(0);
}
folder.open(Folder.READ_WRITE);
messages = folder.getMessages();
for (int i = messages.length - 1; i >= 23; i--)
{
Part p = messages[i];
subject = messages[i].getSubject();
if (messages[i].isSet(Flags.Flag.RECENT)) {
isSet = true;
System.out.println("Recent");
isSetlist.add(String.valueOf(isSet));
}
if (messages[i].isSet(Flags.Flag.SEEN))
{
isSet = false;
System.out.println("Read");
isSetlist.add(String.valueOf(isSet));
}
else
{
isSet = true;
System.out.println("Recent");
isSetlist.add(String.valueOf(isSet));
}
body = getText(p);
list.add(body);
}
return (Message[]) messages;
}
I am using getContent in getText() method
public String getText(Part p) throws MessagingException, IOException {
if (p.isMimeType("text/*")) {
boolean textIsHtml = false;
String s = (String) p.getContent();
textIsHtml = p.isMimeType("text/html");
return String.valueOf(s);
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart) p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return String.valueOf(s);
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart) p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return String.valueOf(s);
}
}
return null;
}
Access the message flags (to determine if the message is SEEN or not) before you access the content. Accessing the content normally sets the SEEN flag.
Try opening the folder in READONLY mode if you don't need to do any modifications - alternately, try preserving the Message flags across getContent calls.