Call to method:
initNotificatie(endDate.getTime());
Log.d("temp", "end: " + endDate.toGMTString()); // end: 13 Apr 2011 12:45:00 GMT
The method:
public void initNotificatie(long when) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.herinnering_button_dropdown;
String contentTitleString = getString(R.string.notification_title);
String contentTekstString = getString(R.string.notification_tekst);
CharSequence tickerText = "NotificationTekst";
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = contentTitleString;
CharSequence contentText = contentTekstString;
Intent notificationIntent = new Intent(this, MapDashboardActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
How come this notification is set immediately while the notification is set for tomorrow? Is this because the notification constructor I'm using is deprecated? I can only use that one because I'm programming on level 4.
The when parameter refers to the time to be shown in the time field. It is not the time at which notification should be shown. In order to do that, use AlarmManager.
Related
I reasearched this, and this code is what I got
The app crashes when I open the MainActivity
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Get Other APW Co. Apps on Play!")
.setContentText("Want more? All our apps are free!");
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemmService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, builder.build());
Try this, Give context name getSystemmService(Context.NOTIFICATION_SERVICE);.
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "App Name";
String notificationText = Msg;
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog),
context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,
notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
This will work:
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!",
System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Get Other APW Co. Apps on Play!";
String notificationText = ""Want more? All our apps are free!"";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(SOME_URL));
PendingIntent pendingIntent
= PendingIntent.getActivity(AndroidNotification.this,
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context,
notificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(1, myNotification);
}});
You have to specify contentIntent, i.e. the PendingIntent that will be executed when the item is clicked. It's mandatory, and you haven't specified it which causes the error.
You can do this in the builder or on the Notification:
in builder:
builder.setContentIntent(contentIntent);
Notification n = builder.build();
on Notification:
Notification n = builder.build();
n.contentIntent = contentIntent;
Only after can you send it to the NotificationManager:
mNotifyMgr.notify(mNotificationId, n);
The exact contentIntent value depends on what you want to do. See reference here:
http://developer.android.com/reference/android/app/Notification.html#contentIntent
See working example here: https://github.com/nheid/unitedcoders-android/blob/master/src/com/unitedcoders/android/examples/download/DownloadProgress.java
I have the Problem, when a user press the Notification activity is opens but it can't be closed.
Here is my code:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Good Morning";
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, time);
Context context = getApplicationContext();
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, NotificationManagerDemoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
try it like this..hope it will work..
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Set Auto Cancel to true while building Notification as follow.
notificationBuilder.setAutoCancel(true);
You should try to write code like below
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), "Channel")
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher_logo_round))
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher_logo_round);
mNotificationManager.notify(id,mBuilder.build());
.setAutoCancel(true)
I'm trying to make a custom sound play on a status bar notification. The .mp3 file is in res/raw/. But when I notify the user the sound is not played. I've tryied with MediaPlayer, and it works, but I dont want to make it play with MediaPlayer.
Here is my method:
public void showNotification()
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.feedback; // icon from resources
CharSequence tickerText = mContext.getString(R.string.statusbar_notification); // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = mContext.getString(R.string.statusbar_notification); // message title
CharSequence contentText = mContext.getString(R.string.statusbar_notificatione_detailed); // message text
Intent notificationIntent = new Intent(mContext, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
//notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/R.raw.notificationsound");
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
}
Thanks.
From the documentation for ContentResolver:
The Uri should be one of the following formats:
android.resource://package_name/id_number
You are passing the String "R.raw.notificationsound" which means nothing.
Instead try this:
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound );
I am trying to play a sound, and flash the backlight of android phone using notification manager. I have used the following code. All the required permissions are there in the manifest file. But I am not sure why this is not giving any notification in emulator or in the device (htc wildfire). If you know any feasible solution please let me know
XYNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int NOTFICATION_ID = 1331;
Notification notifyDetails = new Notification();
notifyDetails.icon = R.drawable.icon12;
notifyDetails.tickerText = "Message Received!!!";
notifyDetails.when = System.currentTimeMillis();
notifyDetails.vibrate = new long[] {0,1000,1000,1000,1000,1000,1000,1000}; //vibrate;
Intent notifyIntent = new Intent(this, XYReceiverAppActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
CharSequence contentTitle = "XYs Notification";
CharSequence contentText = "Get back to XY HOME screen by clicking me";
notifyDetails.setLatestEventInfo(this, contentTitle, contentText, pIntent);
Uri xysound = Uri.parse("android.resource://" + getPackageName() +"/"+ "soundxy");
notifyDetails.ledARGB = Color.BLUE;
notifyDetails.ledOnMS = 10000;
notifyDetails.ledOffMS = 1000;
notifyDetails.flags |= Notification.FLAG_SHOW_LIGHTS;
notifyDetails.sound = xysound;
XYNotificationManager.notify(NOTFICATION_ID, notifyDetails);
The device is not vibrating neither is there any sound alert. LED light is same. how do I send the notification?
Here is a code I use in one of my programs, it always worked...
int icon = R.drawable.alerte;
CharSequence tickerText = getString(R.string.lieuproche);
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Intent intent = new Intent(getApplicationContext(),
ActivityToLaunch.class);
notification.setLatestEventInfo(
MainActivity.this,
"title",
"action", PendingIntent.getActivity(
this.getBaseContext(), 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(0, notification);
i want to create reminder application...I am using Notification Manager...
I am using this line as instantiate of Notification...
long when=sdf.parse("09 06 2010 15:45:00");
Notification notifyDetails = new Notification(R.drawable.reminder_1,"Reminder",when.getTime());
I need to start the notification at specified time...but here it is notification started immediately when i gave the date...and also help me to show multiple notification
The when argument is a time for the Notification which is displayed when the notification bar is in expanded view. It is not used for scheduling when the Notification is displayed.
If you want to schedule something to happen in the future try the the AlarmManager service.
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.update;
CharSequence tickerText = "assignments";
long when = System.currentTimeMillis();
Notification assignmentNotification = new Notification(icon, tickerText, when);
assignmentNotification.defaults |= Notification.DEFAULT_SOUND;
long[] vibrate = {0,100,200,300};
assignmentNotification.vibrate = vibrate;
Context context = getApplicationContext();
CharSequence contentTitle = "check assignments";
CharSequence contentText = "chek ur app for assignments ";
Intent notificationIntent = new Intent(context, ViewAssignmentnotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0);
assignmentNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
** final int id = 2;
Use another "id" for multiple notifiucation...