Custom notification sound not playing - android

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 );

Related

Show Notifications in Android Without Using Intent

I am a beginner in android .I want to show notification in my app . I use the following code
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Hello";
CharSequence contentText = "Welcome ";
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, null, 0);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(HELLO_ID, notification);
But my problem is whenever run my app the intent is loaded with icon .Then the icon is show in the status bar . how to show notifications without using intent ? please help
I'm not sure if this is what you want, but if you want to show some quick notification, toasts come to mind:
Context context = getApplicationContext();
CharSequence text = "Notification!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
You can change how long the toast is shown with duration.

Notification sound not playing

I know this question has been asked many times, but I'm still having issues getting it working.
In my application, I create a notification to prompt the user of some event and I'd like to play a sound that has been packaged as a raw resource within the application.
I create my notification instance:
Notification notification = new Notification(
R.drawable.some_image,
"some meaningful name",
System.currentTimeMillis() );
notification.setLatestEventInfo(...)
Then set some attributes on that notification:
notification.sound = Uri.parse( "android.resource://com.my.package/" + R.raw.some_mp3_file );
notification.flags = Notification.FLAG_INSISTENT;
Finally I invoke the NotificationManager to display the notification:
notificationManager.notify( 1, notification );
The notification does shows up, but the sound doesn't play.
Am I doing something wrong? Is there a <uses-permission> I'm missing? I can't see anything that I've done differently from anyone else that seems to have gotten it working.
For what it's worth, I'm testing directly on a Nexus 7.
try to look at below code. may help you out to solve your problems.
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);
**For sound** assignmentNotification.defaults |= Notification.DEFAULT_SOUND;
long[] vibrate = {0,100,200,300};
**For vibrate** 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;
Please see this answer; It tells you that you have to explicitly specify notification.sound to play that tone;

Notification not working (htc wildfire)

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);

Notification is set immediately in Android

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.

multiple Notification in android

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...

Categories

Resources