i'm sending one notification from background service. If i click on that notification its not calling to perticular activity for some times. (Few times only its calling ).
this is my code:
private void postNotification(String msg, int beaconID) {
if(!notifyMsg.equalsIgnoreCase(msg)){
notifyMsg = msg;
Log.e(TAG, "---------postNotification------- called");
notifyIntent = new Intent(this,
WelcomeHotel.class);
notifyIntent.putExtra("content", msg);
notifyIntent.putExtra("from_activity", ""+TAG);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconMyService.this, 0, /*new Intent[] { notifyIntent }*/ notifyIntent,
/*PendingIntent.FLAG_UPDATE_CURRENT*/0);
Notification notification = new Notification.Builder(
BeaconMyService.this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Monitoring Region").setContentText(msg)
.setAutoCancel(true).setContentIntent(pendingIntent).build();
notification.setLatestEventInfo(getApplicationContext(), "Monitoring Region", ""+msg, pendingIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, notification);
}
}
setLastEventInfo()
This method was deprecated in API level 11. Use Notification.Builder instead.
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
should to
new Notification.Builder(BeaconMyService.this)
.setDefaults(Notification.DEFAULT_ALL)
...
If you use minApi - pre-Honeycomb, you should to change one string
//new Notification.Builder(BeaconMyService.this)
new NotificationCompat.Builder(BeaconMyService.this)
Related
I want to show image from Url in notification panel for that purpose I am trying to make custom UI with following code but its not working
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(1, notification);
and how I can handle the click of each element like image TextView etc. I haven't found any stuff to handle click of each element.
Use remoteView.setOnClickPendingIntent(viewId, pendingIntent) to give click actions to views in your RemoteViews object. This will allow you to do things like start a Service or send a broadcast (to be received by a BroadcastReceiver).
I am developing an application. I want to display multiple notification to status bar area. But every time it's showing previous message. I have tried many things but its not working, how can I display multiple notification in an Android status bar.
Below is my code :
private void Notify(String Title, String Message)
{
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
#SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.ic_launcher,Title, System.currentTimeMillis());
Log.e("NotificationManager","notify");
Intent notificationIntent = new Intent(getBaseContext(), NoteEdit.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
Log.e("NotificationManager","notificationIntent");
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getBaseContext(), Title,Message, pendingIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_ID , notification);
}
}
use this at place of 0 you will have to use a variable and increment it every time
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(),
index++, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
I am developing notification related things.Problem is click on button. I wrote the following notification related code :
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"New update received", when);
Intent notificationIntent = new Intent(context,
MessageReceivedActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle=new Bundle();
bundle.putString("post_title", message);
notificationIntent.putExtras(bundle);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "Agility Logistics", "New update received", intent);
// notification.number +=1;
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, notification);
If button is clicked, the button onclick notification is overriding all the other notifications.This is undesirable. So how can I show all notifications received without having them overwritten by button click notification ?
I have a service that may show a notification, the problem is once the notification is set, it doesn't clear neither when clicked, nor when swiped on. I am using the flag Notification.FLAG_AUTO_CANCEL but it doesn't seem to do anything..
private NotificationManager nm;
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
private final int NOTIFY_ID = 1;
private void showNotification(String date, String name) {
try{
CharSequence text = "You have new Event";
Notification notification = new Notification(R.drawable.small_icon, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, viewEvents.class).putExtra("date", date), 0);
notification.setLatestEventInfo(this, name, "Date: "+date, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFY_ID, notification);
}catch(Exception e){
Log.i("Notification",e.toString());
}
}
So what am I doing wrong?
Try using Notification.Builder or NotificationCompat.Builder instead of rolling the Notification manually.
Among other things, this would prevent the bug in your code, where you are applying FLAG_AUTO_CANCEL to defaults rather than flags.
Notification.FLAG_AUTO_CANCEL is commented out in your code.
But in case it isn't then flag Notification.DEFAULT_LIGHTS should be setted in notification.defaults not in notification.flags
I use this code to show/hide notifications:
private Handler handler = new Handler();
private Runnable task = new Runnable() {
#Override
public void run() {
NotificationManager manager = (NotificationManager) contesto.getSystemService(Context.NOTIFICATION_SERVICE);
Intent launchIntent = new Intent(contesto.getApplicationContext(), SearchHistory.class);
PendingIntent contentIntent = PendingIntent.getActivity(contesto.getApplicationContext(), 0, launchIntent, 0);
//Create notification with the time it was fired
NotificationCompat.Builder builder = new NotificationCompat.Builder(contesto);
builder.setSmallIcon(R.drawable.ic_launcher).setTicker("MESSAGE HERE!").setWhen(System.currentTimeMillis()).setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND).setContentTitle("TITLE").setContentText("MESSAGE").setContentIntent(contentIntent);
Notification note = builder.build();
manager.notify(100, note);
}
};
And to invoke just put:
handler.post(task);
public class test extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
String extra = "test";
NotificationManager myNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, another.class);
Notification notification = new Notification(R.drawable.ic_launcher, extra,
System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(), "title", "text", pendingIntent);
notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_INSISTENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotificationManager.notify(1, notification);
}
}
This snippet code is working fine, the thing is that, when I run it, the notification sound is ringing constantly until i check it on the status bar.
Is there a way to make it ring only one time, instead of constantly ringing ?
Remove the flag Notification.FLAG_INSISTENT
From docs: "Bit to be bitwise-ored into the flags field that if set, the audio will be repeated until the notification is cancelled or the notification window is opened."