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;
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 have implemented local notification with sound. It's working properly but when I clear local notification the custom sound what I am playing should stop.
Uri uri = null;
String[] split = content.split("-");
Intent intents = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intents, 0);
//getting audio from raw folder
uri = Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.beep);
//plays audio infinite times until user clicks or clear notification
try
{
MediaPlayer player = MediaPlayer.create(context, uri);
player.setLooping(true);
player.start();
Notification noti = new Notification.Builder(context)
.setContentTitle(split[0])
.setContentText(device_name + split[1]).setSmallIcon(R.drawable.hi_notification)
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.build();
noti.sound = uri;
noti.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
notificationManager.notify(0, noti);
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
Set the notification sound to be INSISTENT.
1.Notification.DEFAULT_SOUND:used to play sound.
2.Notification.FLAG_INSISTENT:this flag is makes your sound to ring continuously until you have done few operations on notification ie, either you drag the bar or you click the bar.
3.Notification.FLAG_AUTO_CANCEL:this flag is used to automatically cancel the notification after you have seen it
builder.setSmallIcon(R.mipmap.app_icon).setContentTitle(title)
.setContentText(msg)
.setColor(getResources().getColor(R.color.white))
.addAction(R.drawable.alarm_off, getString(R.string.dismiss), pendingIntent)
.setOngoing(true)
.setSound(Uri.parse(path));
Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(WAKE_UP_ALARM_ID, notification)
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);
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);
}