How to remove SMS icon in notification bar - android

I am working with SMSReceiver and a dialog to display address, message and time of the SMS will be shown when a message arrives. I want to remove notification icon when a user clicks OK button in the custom dialog. Pleas let me know how I can do this.
Thanks in Advance.

It is not possible to directly remove the notification. This is because the notification is being generated by a different application (system Messaging app or a custom SMS app). Only the app which generates the notifications is able to remove them.
There is however something you can try to do.
You could consume the system broadcast for the incoming SMS instead of propagating it further which would mean that the other applications responsible for handling SMS messages would not be informed about a new message being delived.
In order to do that you should:
Increase the priority of your receiver:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Abort the broadcast in your onReceive() implementation:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
// Do whatever with the message
abortBroadcast(); // Stop the broadcast from being propagated further
}
}

Related

How to get count of total push notifications in android for an application?

I am working on badges on launcher icon of application in android,I want to get total number of push notification for my app to display as a badge on my app's launcher icon,I am using parse.com for the push notifications in android,So can any buddy please tell me how to get count of push notifications and reflect it in badge in android?
code
Parse.initialize(this, "Hl59kzuF4yGr36XEUZByOgtorvyrYcVm7zAb6amG",
"YohxPugahDoiBZ2kaQ7qtqmO40y0JmLYkMuT");
PushService.setDefaultPushCallback(this, QuestionOFDayActivity.class);
If you are using parse.com then you might have using BroadcastReceiver for handling push notification(if not then use that).
So in onRecieve() of Broadcast receiver you can take count as application level. then u will get no. of push notification through out of app.
Edited :
Add this Receiver in your manifest
<receiver android:name="your.package.MyCustomReceiver" android:exported="false">
<intent-filter>
<action android:name="your.package.UPDATE_STATUS" />
</intent-filter>
</receiver>
create new class:-
public class MyCustomReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Here you will get the event when you receive Notification
// you can maintain a count here as application level.
}
}
If you want detailed doc - Then read this

Parse Push - How to Automatically Open an activity without user action on receiving a push on Android

I have a requirement (android) where my app should run its main activity automatically when a push notification is received without the user clicking on notification in system tray. I am having a map where it shows current location, but in the push, i will receive a location, and i need my map in the main activity to move the camera to the currently received location on receiving the push, and alert the user with a custom sound. All this should happen without the user clicking on anything. Pls help. Thanks in advance.
Yes, it's possible. Parse.com documentation says:
You can also specify an Intent to be fired in the background when the push notification is
received. This will allow your app to perform custom handling for the notification, and
can be used whether or not you have chosen to display a system tray message. To implement
custom notification handling, set the Action entry in your push notification data
dictionary to the Intent action which you want to fire. Android guidelines suggest that
you prefix the action with your package name to avoid namespace collisions with other
running apps.
So you send push notifications in this way:
JSONObject data = new JSONObject("{\"action\": \"com.example.UPDATE_STATUS\""}));
ParsePush push = new ParsePush();
push.setData(data);
push.sendPushInBackground();
Then in your AndroidManifest.xml register a broadcast receiver that will be called whenever a push notification is received with an action parameter of com.example.UPDATE_STATUS:
<receiver android:name="com.example.MyBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.example.UPDATE_STATUS" />
</intent-filter>
</receiver>
In your broadcast receiver you can start a new activity:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startActivity(new Intent(context, MyActivity.class));
}
}
warning to #makovkastar, major changes since version V1.8. for instance: no more com.example.UPDATE_STATUS.
push notifications guide is more clear now: https://www.parse.com/tutorials/android-push-notifications
this is the ParsePushBroadcastReceiver subclass: https://gist.github.com/panchicore/97d5ad25842258576109
this answer have a good tutorial to send/receive local broadcasts: https://stackoverflow.com/a/8875292/155293
Basically onPushReceive will be called when a push is received in the device, in this method use LocalBroadcastManager to make something in the app, for instance, add a new message to a chatroom.

GCM clarifications

I know these methods are deprecated, but since the new GCM API seems to be buggy, I am reverting to these methods until a stable version is pushed by Google.
We are declaring this receiver inside the manifest.
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myApp" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
And we have the onMessage() method inside the GCMIntentService class.
#Override
protected void onMessage(Context context, Intent intent)
{
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("msg");
}
1. However, upon receiving a message this method is never called. Why
?
Moreover, the example I follow uses the following.
registerReceiver(mHandleMessageReceiver, new IntentFilter("intent_filter_string"));
associated with the following class.
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
String newMessage = intent.getExtras().getString("data");
}
};
which gets unregistered inside the onPause.
Why do we need to create this Broadcast Receiver?
Can't we do this in the manifest ?
Isn't this already covered by the onMessage() inside the GCMIntentService class ?
What role does the Pending Intent String play ?
Answers are appreciated.
Why do we need to create this Broadcast Receiver?
In some cases you might be interested in updating the UI if the app is running.
So you create a broadcast receiver at runtime and unregister it when the app
goes into background.
Can't we do this in the manifest?
Yes, you can do it in manifest too.
Isn't this already covered by the onMessage() inside the GCMIntentService class?
GCMIntentService extends GCMBaseIntentService. So any message coming from gcm,
will first be recieved in the onMessage of the GCMIntentService.
Its upto you to decide how you handle the message.
You can create a notification or send a broadcast to your custom broadcast
receivers and update the UI when the app is running.
What role does the Pending Intent play ?
According to the docs
A PendingIntent itself is simply a reference to a token maintained by the system
describing the original data used to retrieve it. This means that, even if its
owning application's process is killed, the PendingIntent itself will remain
usable from other processes that have been given it.
Did you registered your app/device combination to your GCm project? You have to do that first in the onRegistered Method. And did you add all necessary permissions? Google says you dont have to add the Custom Permissions above android 4.0 but my apps never worked without'em.
If you're looking for an easier way to work with GCM I recommend apiOmat: http://www.apiomat.com I know its Backend as a Service. But if your app is small you don't have to pay anything and it's much easier with it.

Sending message from a application on one mobile device to another application on another device

Hi I want to send message from one application (which will be installed on android mobile) to
another application (which will be installed on another mobile). This means
One mobile ------------sends message-------- >to another mobile.
Similarly I want second mobile to send----------message ----- to first mobile.
If I use sms to send message I think it will be saved in the sms box and so the user can read
the message. I want to hide the message . So is there any way I can send message directly from
one application to another.
If you want to prevent saving sms to inbox, you can abort broadcast:
public class SmsReceiver extends BroadcastReceiver {
// ...
#Override
public void onReceive(Context context, Intent intent) {
// ...
if( smsIsMine() ) {
// Do something with sms
this.abortBroadcast();
}
}
}
You also need to set the priority of SmsReceiver the highest possible priority in your AndroidManifest.xml as below:
<receiver
android:name=".SmsReceiver">
<intent-filter android:priority="10" >
<!-- ... -->
</intent-filter>
</receiver>

How can I intercept an incoming SMS message with specific text?

I want to know about intercepting an incoming SMS message for a specific key word; for example, "Hi", so that I can read that sms containing "Hi" in it and delete it after reading the message, and if that message doesn't contain any such text then it wouldn't be deleted and instead saved in the inbox.
Look for Broadcast Receiver, this is dependent on the apps installed on the phone but you can give your app priority for listening to messages. Although, when a notification is shown, the message won't be in the SMS Database yet, so you will need to use abortBroadcast() to stop other apps being notified. See example below:
public class MessageReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i(TAG, messages.getMessageBody());
if(messages.getMessageBody().contains("Hi")) {
abortBroadcast();
}
}
And you would need to declare the receiver in the manifest, like so:
<receiver android:name="com.encima.smsreceiver.MessageReceiver" android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
Finally, make sure you have the permission to RECEIVE_SMS in the manifest.
As of Android 4.4, what you ask to do in the question is impossible
Since Android version 4.4, If your app is not the default messaging app, you won't be able to perform those things promised in the accepted answer. You can neither stop the default messaging app from getting notified, nor you can delete an sms message.

Categories

Resources