I have build a sms Service Application now I wish to display notification on Receiving SMS in which I wish to use dafault means on device(Mobile Device) based setting i.e. if one select Vibration then Vibrate or if one select LED then Notification.DEFAULT_LIGHTS or if one select Sound or ring then Notification.DEFAULT_SOUND.
Is it possible sir please help me in this regards or sorry for my bad English or not understand. I also tag my code below
private void displayNotification(String msg)
{
Intent i = new Intent(this.context,ZigbeeActivity.class);
i.putExtra("ID", ID);
i.putExtra("msg",msg);
PendingIntent pendInt = PendingIntent.getActivity(context, 0, i, 0);
Notification notif = new Notification(0,"Receiving SMS",System.currentTimeMillis());
NotificationManager nm = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
notif.setLatestEventInfo(context, "SMS", msg, pendInt);
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.icon = R.drawable.notify;
notif.defaults |= Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notif.ledARGB = Color.WHITE;
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 1500;
notif.ledOffMS = 1500;
nm.notify(ID, notif);
}
The problem is in Notification.DEFAULT_LIGHTS flag set. You shouldn't set it.
This code works for me
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 500;
notification.ledOffMS = 500;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
Related
I have custom notification sound in my application, with those parameters.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ex_mark_r)
// open app on click
.setContentIntent(contentIntent)
.setSound(defaultSoundUri, RingtoneManager.TYPE_ALARM)
.setContentTitle(title)
.setAutoCancel(false)
.setCategory(Notification.CATEGORY_ALARM)
.addAction(R.mipmap.ex_mark_r, getResources().getString(R.string.alert), pendingIntentAlert)
.addAction( R.mipmap.close, getResources().getString(R.string.dismiss), pendingIntentDismiss)
.setContentText(message);
// .setFullScreenIntent(contentIntent, true);
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
mNotification = mBuilder.build();
mNotification.flags |= Notification.VISIBILITY_PUBLIC;
mNotification.flags |= Notification.FLAG_INSISTENT;
mNotification.flags |= Notification.FLAG_HIGH_PRIORITY;
mNotification.flags |= Notification.PRIORITY_MAX;
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
}
How can I make my sound to rise [start low and become lowed] ?
I didn't find a single solution on the internet that solves the problem.
int notificationId = Integer.parseInt(Common.getStringSharedPreference(context, "NOTIFICATION_ID", "ID", "1"));
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.app_logo, message, System.currentTimeMillis());
String title = context.getString(R.string.app_name);
Intent notificationIntent = null;
notificationIntent = new Intent(context, MyLawyerActivity.class);
notificationIntent.putExtra(ExtrasKeys.ITEM_OBJECT, (Serializable) object);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, notificationId, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.sound = Settings.System.DEFAULT_NOTIFICATION_URI;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Common.putStringSharedPreference(context, "NOTIFICATION_ID", "ID", ""+(notificationId+1));
Note that I am using a device with lollipop for testing.
Just change the number for multiple notification like
notificationManager.notify(0, notification);
notificationManager.notify(1, notification);
I am getting GCM Push Notification successfully. Now i want to add custom sound file instead of default sound. I have tried with Uri from
file:///res/raw/pop.mp3
in
Notification.DEFAULT_SOUND;
but not success. Please share if you have better solution.
My GCMIntentService.java method code is below -
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
System.out.println("Called generateNotification>>>>>>>>>>>>>"+message);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(title)
.setStyle(
new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message);
Intent notificationIntent = new Intent(context,
SplashActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(intent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
To add custom sound add this
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pop);
i.e. In your code change
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
to
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;
I have the following code:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
CharSequence tickerText = "HI!";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.droid,
tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.DEFAULT_VIBRATE;
for some reason - none of the flags really applied except for the auto cancel.
No vibration, no sound, no lights.
What can cause this?
I tried in 3.1 & in 2.2.1
Thanks
Try:
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
I have the following code to produce a notification. The notification comes up but no sound. I have the following code to pouyt the sound on
notification.defaults |= Notification.DEFAULT_SOUND;
code listing
NotificationManager manager = (NotificationManager) gContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "Teeth Alert", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(gContext, 0, new Intent(gContext, NotifyMessage.class), 0);
notification.setLatestEventInfo(gContext, "Your teeth appoitmernt:", "Date:", contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
manager.notify(1,notification);
cDates.SetAlarm(i);
Ted
You should try setting
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS;
or setting explicitly:
notification.sound =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Not sure if you resolved this but I had the same problem and it's because you have
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
but you need to assign it first like
notification.defaults = Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
or more simply as #rekaszeru put
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS;