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);
Related
How to manage notification with condition:
if get single notification then when clicked to launch to Intent "DetailNotificationActivity" And ,
if get multiple notification , the notification always merged on notification bar then when clicked to launch inten "HomeAplicationActivity" ?
this s my code, always not merge notification on notification bar and always launch DetailNotificationActivity .
int icon = R.drawable.icon_notification_comment;
notification = new Notification(icon, pengomentar+" mengomentari artikel \""+judul+"\" : "+ isi_komentar, when);
notificationIntent = new Intent(context, ContentCommentActivity.class);
Bundle b = new Bundle();
b.putString("judul", judul);
b.putString("komentar", komentar);
b.putString("sekilas_isi_detail", sekilas_isi);
b.putString("date", date);
b.putString("id_tulisan_detail", id_tulisan);
b.putString("gambar_tulisan_detail", gambar_tulisan_detail);
b.putString("status_gambar_detail", status_gambar);
b.putString("kategori_detail", kategori);
b.putString("main_kategori_detail", main_kategori);
b.putString("seo_detail", seo_detail);
b.putString("Notifikasi", "Y");
b.putString("username", username);
b.putString("avatar", avatar);
b.putString("kordinat_lokasi_detail",kordinat_lokasi);
notificationIntent.putExtras(b);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent = PendingIntent.getActivity(context, unique_ids, notificationIntent, 0);
notification.setLatestEventInfo(context, pengomentar+" mengomentari artikel \""+judul+"\"", isi_komentar, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.number += 1;
notificationManager.notify(unique_ids, notification);
How to make it work?
NB: work in from api 8 above
This is a two part issue. I have push notifications work with my app but I can't get a custom sound to work, 1. 2. I can't get the notifications to open the app when you click on it from the notification bar.
This is the code below. I've tried various things to import a custom sound. The sound in in my assets/sounds folder. Will that be carried over with the apk when installed and how to I access it? I'm using webview as well like to open the app to a specific link from the notification, can that be done also?
private void sendGCMIntent(final Context theContext, String theMessage)
{
int icon = R.drawable.noti_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
theContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, theMessage, when);
String title = theContext.getString(R.string.app_name);
Intent notificationIntent = new Intent();
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(theContext, 0, notificationIntent, 0);
notification.setLatestEventInfo(theContext, title, theMessage, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//String path = "file:///android_asset/sounds/soft.mp3";
//File file = new File(path);
//Log.i(TAG,"Sound exists : " + file.exists());
Uri soundUri = Uri.parse("file:///android_asset/sounds/soft.mp3");
if (soundUri != null) {
notification.sound = soundUri;
}
else {
notification.defaults |= Notification.DEFAULT_SOUND;
}
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
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;
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 );
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.