Stop local notification sound in Android - android

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)

Related

MediaPlayer Doesn't finish and stops during Notification

Im using a Broadcast Receiver to send notification at custom time. I use a MediaPlayer to play a sound everytime it fires. The problem is it sometimes just suddenly stops in the middle of the sound playing. This is my code:
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
#SuppressLint("InvalidWakeLockTag")
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TRAININGCOUNTDOWN");
wl.acquire(10*60*1000L /*10 minutes*/);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon);
Intent inteent = new Intent(context, MainActivity.class);
inteent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, xx, inteent, PendingIntent.FLAG_UPDATE_CURRENT);
xx = xx + 1;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(icon)
.setContentTitle("بانگ")
.setContentText(notTitle)
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// notificationId is a unique int for each notification that you must define
// Put here YOUR code.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("com.c4kurd.bang");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"com.c4kurd.bang",
"بانگ",
NotificationManager.IMPORTANCE_HIGH
);
channel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
assert notificationManager != null;
int id = context.getResources().getIdentifier(cc(prefs.getString("voices","")), "raw", context.getPackageName());
// Toast.makeText(context, prefs.getString("voices",""),Toast.LENGTH_SHORT).show();
MediaPlayer mp= MediaPlayer.create(context, id);
notificationManager.notify(xx, builder.setVibrate(new long[]{1000,1000}).build());
mp.start();
wl.release();
}
I don't know the issue. Is it because mp.start() is before the Wake Lock?
Thanks in advance.
DEFAULT: This line returns the default notification sound
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
CUSTOM: This line chooses a custom notification sound:
(put this in RAW (resources) folder)
Uri notificationSound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.my_custom_sound_file);
Choose one of the above and add it to your notification:
setSound(notificationSound)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(icon)
.setContentTitle("Your Title")
.setContentText("Your Content")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setSound(notificationSound)
.setAutoCancel(true);
Remove all extra lines you used your own MediaPlayer.

Notification not showing up time in android

I have created custom notification, I am not able to show current time in my notification bar.. It is not coming even though I am showing System.currentTimeMillis().
final Notification notification = new Notification(R.drawable.notificationicon, context.getResources().getString(R.string.notificationheadingtext), System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.when = System.currentTimeMillis();
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion > 20)
{
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_process);
}
else
{
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_process_lowerversion);
}
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = alarmSound;
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_launcher);
notification.contentView.setTextViewText(R.id.status_text, context.getResources().getString(R.string.notificationheadingtext));
notificationManager.notify(42, notification);
Notification class is deprecated. Use NotificationCompat. Read this tutorial

Android GCM PushNotification - Add add custom sound file in app

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;

How to Merge Multiple Notification Android?

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

Android - Push Notification sound and open activities issues

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

Categories

Resources