I am using following code for notification. It should vibrate and make sound at the time of event. But it is giving sound when the notification is creating, although the notification time is after 30 min.
final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp",nextAlarmTime);
Context context = getApplicationContext();
CharSequence contentTitle = "Viramune";
CharSequence contentText = notificationAlart;
Intent notifyIntent = new Intent(context, Myapp.class);
PendingIntent intent1 = PendingIntent.getActivity(ViewDoughnut.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent1);
notifyDetails.flags = Notification.FLAG_ONLY_ALERT_ONCE;
notifyDetails.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
mNotificationManager.notify((int) editEventid, notifyDetails);
What is the problem in my code?
Android does exactly what it told to do:
mNotificationManager.notify((int) editEventid, notifyDetails);
This line creates a notification. You should use AlarmManager to schedule your notification in future.
The third parameter to the Notification constructor is not used to determine when to shoot the notification, but is just used for display and sort.
I believe you're trying to do something that needs to be done using AlarmManager, and not Notification.
Related
I try to make my notification(from Service) updating or refreshing in every five minutes. How can I do this? This is, what i want to update.
if (...){
int icon = R.drawable.updatedImage1;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence message = "II Tydzień";
Notification notification = new Notification(icon, message, when );
String title = this.getString(R.string.app_name); // Here you can pass the value of your TextView
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, title, message, intent);
notification.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(0, notification);
} else { ...other notification }
Condition is period of time, so I need to change notification depending on what time is it.
You can use AlarmManager to schedule your notification repeatedly at different time intervals. The docs mention that:
This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running
You might be able to use setRepeating function for your purpose.
Each time I send a notification the device acts as if it the notification has been sent an infinite amount of times. That is, the ringtone and vibrate go off continually until I open the notification menu. I can get this behavior to stop if I comment out the notification.flag |= line but then I am unable to use the auto-cancel flag. Any ideas on what is causing this?
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(resourceID)
.setContentTitle(title)
.setContentText(contentText)
.setSound(soundUri, RingtoneManager.TYPE_NOTIFICATION)
.setVibrate(CommonUtilities.VIBRATE_PATTERN);
Notification notification = mBuilder.build();
/* comment this out to stop the infinite notification loop */
notification.flags |= Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL
| Notification.FLAG_ONLY_ALERT_ONCE;
Intent intent = new Intent(this, DashboardActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, CommonUtilities.REQUEST_NOTIFICATION, intent, 0);
String pref = getSharedPreferences(CommonUtilities.NOTIFICATION_PEF, MODE_PRIVATE).getString("incoming", contentText);
notification.setLatestEventInfo(this, title ,pref, activity);
notification.number += 1;
//Display notification
notificationManager.notify(0, notification);
Hmm well you should check your flags again!
FLAG_ONLY_ALERT_ONCE
Bit to be bitwise-ored into the flags field that should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.
So my guess is that with this flag you will get your notification to vibrate all the time.
From the official docs http://developer.android.com/reference/android/app/Notification.html
I use the following code to show the notifications
private void SendNotification(String notificationTitle, String notificationMessage)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, notificationMessage,
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("notif_id", "5100");
//Setting the single top property of the notification and intent.
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify((int)System.currentTimeMillis(), notification);
}
Each time a new notification comes, the previous one should be removed and the new one should be shown.
Any clues will be helpful.
Thanx in advance
o.
You are generatting a notification with different id each time (System.currentTimeMillis()), change it for a single ID.
notificationManager.notify(0, notification);
Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
NotificationManager.html
There is one way that you should clear all Notification of your Application before you post a newer one.
Give it a shot like,
notificationManager.cancelAll();
To clear a particular Notification you can do it like,
notificationManager.cancel(notification_id);
I am working on an Android application.The app is like native sms application.I am facing a problem in notification part. In native app, they handle the notification in the following way.
1)if messages from an specific number ,then the click on the notification will lead to the chat page of the corresponding contact and the notification will clear.
2)if messages from the different number, then the click on the notification will lead to home page and notification won't clear.
I had done the first part and I don't have any idea to do the second part.
Is there any way to leave the notification part without clear and call an intent.?
you have to add a flag:
notification.flags |= Notification.FLAG_ONGOING_EVENT;
here a whole example:
private static final int NOTIFICATION_EX = 0;
private NotificationManager notificationManager;
...
in onCreate:
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.youricon;
CharSequence tickerText = "Sticky notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Sticky notification";
CharSequence contentText = "...click here and it wont go away...";
Intent notificationIntent = new Intent(this, mainmenu.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle,
contentText, contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_EX, notification);
the intent you can adjust to whatever you like.
You will have to create two different kind of notification, the first one you have already implemented, and the second one you can just copy the first one and check if the number is a different one use the second notification and set the flags to not clear notification2.flags |= Notification.FLAG_ONGOING_EVENT; on click with the HomeActivity in the PendingIntent which has Intent:
I have developing an App where the user can create event and set notification for that very event. I am using the following code.
final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp",calendar.getTimeInMillis());
Context context = getApplicationContext();
Intent notifyIntent = new Intent(context, ViewDoughnut.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ViewCal.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
notifyDetails.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(eventId, notifyDetails);
I want to delete any specific notification if the user delete the event corresponding to it. I have tried with following code.
mNotificationManager.cancel(eventId);
But it is not working at all. How to do it?
see
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageYourNotifications
and
http://developer.android.com/reference/android/app/NotificationManager.html#cancel%28int%29
i used the tag to cancel my notifications:
int IdFromNotification = intent.getIntExtra("myId", -1);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(IdFromNotification + "", IdFromNotification);
needless to say that i used the same tag when setting the notification!?