Notification sound not stopping android - android

I simply call a Notification . But It continuously playing sound unless I drag notification ..
Below is my code and right now I am testing on below OREO..
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "droidudes.zcode";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String name = "easyTouch";
String Description = "Best Utility Tool";
int importance = NotificationManager.IMPORTANCE_HIGH;//IMPORTANCE_HIGH
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern();
mChannel.setShowBadge(false);
mNotificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext, NOTIFICATION_CHANNEL_ID)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
///.setVibrate(longArrayOf(0, 1000, 500, 1000))
.setSmallIcon(R.drawable.icon_of_app)
.setContentTitle(mContext.getString(R.string.app_name))
.setContentText("Toucher Hiding Here")
.setAutoCancel(true);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 1,
new Intent(mContext,EasyTouchService.class)
.setAction(EasyTouchService.ACTION_SHOW), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
Notification mNotification = mBuilder.build();
mNotification.flags |= Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
//mNotification.tickerText = getString(R.string.app_name);
// setUpAsForeground(message);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
I have tried multiple ways by commenting vibration sound in above code . But still sound play for unlimited time..

Try this out:
final PendingIntent pendingIntent = PendingIntent.getActivity(
mCtx,
notfication_id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mCtx);
Notification mNotif = builder.setSmallIcon(R.mipmap.logo)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle("title of your notification")
.setContentText("details of your notification")
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(),R.drawable.logo_full))
.build();
mNotif.flags = Notification.FLAG_AUTO_CANCEL;
mNotif.defaults |= Notification.DEFAULT_SOUND;
mNotif.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager)mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), mNotif);

Related

go to android app's webview on click push notification addAction button

Please be informed we are able to send push notifications with addAction (buttons). But we are unable to simply direct it to app's webview url onclick of those buttons despite providing intents.
Further explaining, we have two buttons(addAction): Receive, Dismiss in the heads up push notification. We would like Receive button to direct to app's webview url onclick and Dismiss button to close the notification.
The Code is as given below
private void sendMyNotification(String title, String message, String click_action, String uri, String tag, String nid) {
//On click of notification it redirect to this Activity
Intent intent = new Intent(click_action);
intent.putExtra("uri", uri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
int notification_id = nid!=null ? Integer.parseInt(nid) : MainActivity.ASWV_FCM_ID;
String channelId = MainActivity.asw_fcm_channel;
Uri soundUri= Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/pristine.mp3");
Uri uri = Uri.parse("https://www.google.com/");
Intent ur = new Intent(Intent.ACTION_VIEW, uri);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, MainActivity.asw_fcm_channel)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel (true)
.setSound(soundUri)
.addAction(R.mipmap.ic_launcher,"Receive",ur)
.addAction(R.mipmap.ic_launcher,"Dismiss",ur)
.setSound(soundUri)
.setContentIntent(pendingIntent);
Notification noti = notificationBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build();
// Configure the notification channel.
mChannel.setDescription("Common notifications");
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(soundUri, attributes);
mChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mChannel.enableVibration(true);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(notification_id, notificationBuilder.build());
}
}
Please guide us as where we are going wrong.

Set notification in loop until user click

here is my code of notification:
private void APP_FOREGROUND_showNotificationMessage(Context context, String title, String message, Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "CHANNEL_ID";
String channelName = "CHANNEL NAME";
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(channelId,
channelName,
NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setDescription(title);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic), attributes); // Here you set the sound
if (manager != null)
manager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.vb_grey)
.setContentTitle(title)
.setColor(Color.RED)
.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic))
.setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_INSISTENT;
manager.notify(0, builder.build());
}
This is my notification code.
I have used .setDefaults(Notification.FLAG_INSISTENT) for looping but notification sound coming one time only.
You can not set the flag by using setDefaults()
You can use the following code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.vb_grey)
.setContentTitle(title)
.setColor(Color.RED)
.setOngoing(true) //<-- you also need this one
.setSound(Uri.parse("android.resource://"+context+context.getPackageName()+"/"+R.raw.offic))
.setContentText(message).setAutoCancel(true).setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;
also check this stackoverflow question

Android 8 (Oreo) notification not showing up

Intent intent = new Intent(this, SplashActivity.class);
Bundle bundle = new Bundle();
bundle.putString("splash", psd);
bundle.putString("targetId", targetId);
intent.putExtras(bundle);
intent.setAction(psd);
intent.setAction(targetId);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), id, intent,
0);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_notification_overlay)
.setContentTitle(title)
.setContentText(remoteMessage.getData().get("body"))
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText("" + remoteMessage.getData().get("body")))
.setContentIntent(pendingIntent)
.setSound(defaultSoundUri);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
inboxStyle.setBigContentTitle("" + title);
for (int i = 0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
//.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notificationBuilder.build());
It used to be working, no clue what's happening on new Android devices like Android O. I didn't try back going to old devices, but it's happening on pixel.
Starting in Android 8.0 (API level 26), all notifications must be
assigned to a channel. For each channel, you can set the visual and
auditory behavior that is applied to all notifications in that
channel. Then, users can change these settings and decide which
notification channels from your app should be intrusive or visible at
all
Notification channels
try this code it works perfectly for me.
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),"channel_01")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(message)
.setContentIntent(resultPendingIntent)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH)
.setChannelId("channel_01")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setStyle(inboxStyle)
.setContentTitle(Title);
mNotificationManager.notify(Notification_ID, mBuilder.build());
NotificationChannel channel = new NotificationChannel(Notification_ID, "Playback Notification", NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId("channel_01");
mNotificationManager.createNotificationChannel(channel);
}else {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),Notification_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(Title)
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(inboxStyle)
.setSound(soundUri)
.setAutoCancel(true);
mNotificationManager.notify(Notification_ID, mBuilder.build());
}
I just figured this one out myself, you need to setup Channels in Oreo, see my previous post on the issue - I though it was a master-detail issue!!! Turns out Oreo has an additional attribute you need but it seems to fail silently if you don't provide it.
Set up notification channels. Example code below
public static void createNotificationChannel(final Context context, final
String channelId, final CharSequence channelName, final String channelDescription, final int importance, final boolean showBadge) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
postAsyncSafely("createNotificationChannel", new Runnable() {
#Override
public void run() {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.setDescription(channelDescription);
notificationChannel.setShowBadge(showBadge);
notificationManager.createNotificationChannel(notificationChannel);
Logger.i("Notification channel " + channelName.toString() +
" has been created");
}
});
}
}catch (Throwable t){
Logger.v("Failure creating Notification Channel",t);
}
}

How to display results in notification bar on Android

I'm creating an Android application. What I'm looking for is after computing the results I want the numeric value to be displayed on the notification window. How can I do it?
To display your result into notification you can use below code to create notification:
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.noti_icon);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.noti_icon)
.setLargeIcon(icon)
.setContentTitle("Your Title of Notification")
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setContentText("write here your score which you have counted");
Intent notificationIntent = new Intent(this, YourActivity.class); // here on notification click you are moved on this activity
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(contentIntent);
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.visibility |= Notification.VISIBILITY_PUBLIC;
notificationId = 0;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);

Vibrate and Sound defaults on notification

I'm trying to get a default vibrate and sound alert when my notification comes in, but so far no luck. I imagine it's something to do with the way I set the defaults, but I'm unsure of how to fix it. Any thoughts?
public void connectedNotify() {
Integer mId = 0;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Device Connected")
.setContentText("Click to monitor");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setOngoing(true);
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_SOUND;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, note);
}
Some dummy code might help you.
private static NotificationCompat.Builder buildNotificationCommon(Context _context, .....) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(_context)
.setWhen(System.currentTimeMillis()).......;
//Vibration
builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
//LED
builder.setLights(Color.RED, 3000, 3000);
//Ton
builder.setSound(Uri.parse("uri://sadfasdfasdf.mp3"));
return builder;
}
Add below permission for Vibration in AndroidManifest.xml file
<uses-permission android:name="android.permission.VIBRATE" />
An extension to TeeTracker's answer,
to get the default notification sound you can do as follows
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Device Connected")
.setContentText("Click to monitor");
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
This will give you the default notification sound.
Notification
Vibrate
mBuilder.setVibrate(new long[] { 1000, 1000});
Sound
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
for more sound option
Its work fine to me,You can try it.
protected void displayNotification() {
Log.i("Start", "notification");
// Invoking the default notification service //
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText("You have "+unMber_unRead_sms +" new message.");
mBuilder.setTicker("New message from PayMe..");
mBuilder.setSmallIcon(R.drawable.icon2);
// Increase notification number every time a new notification arrives //
mBuilder.setNumber(unMber_unRead_sms);
// Creates an explicit intent for an Activity in your app //
Intent resultIntent = new Intent(this, FreesmsLog.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(FreesmsLog.class);
// Adds the Intent that starts the Activity to the top of the stack //
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
// mBuilder.setOngoing(true);
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_SOUND;
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on. //
mNotificationManager.notify(notificationID, mBuilder.build());
}
This is a simple way to call notification by using default vibrate and sound from system.
private void sendNotification(String message, String tick, String title, boolean sound, boolean vibrate, int iconID) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
if (sound) {
notification.defaults |= Notification.DEFAULT_SOUND;
}
if (vibrate) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
notificationBuilder.setDefaults(notification.defaults);
notificationBuilder.setSmallIcon(iconID)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setTicker(tick)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
Add vibrate permission if you are going to use it:
<uses-permission android:name="android.permission.VIBRATE"/>
Good luck,'.
I m using the followung code and its working fine for me .
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}
To support SDK version >= 26, you also should build NotificationChanel and set a vibration pattern and sound there. There is a Kotlin code sample:
val vibrationPattern = longArrayOf(500)
val soundUri = "<your sound uri>"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val attr = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
val channelName: CharSequence = Constants.NOTIFICATION_CHANNEL_NAME
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel =
NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, channelName, importance)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.setSound(soundUri, attr)
notificationChannel.vibrationPattern = vibrationPattern
notificationManager.createNotificationChannel(notificationChannel)
}
And this is the builder:
with(NotificationCompat.Builder(applicationContext, Constants.NOTIFICATION_CHANNEL_ID)) {
setContentTitle("Some title")
setContentText("Some content")
setSmallIcon(R.drawable.ic_logo)
setAutoCancel(true)
setVibrate(vibrationPattern)
setSound(soundUri)
setDefaults(Notification.DEFAULT_VIBRATE)
setContentIntent(
// this is an extension function of context you should build
// your own pending intent and place it here
createNotificationPendingIntent(
Intent(applicationContext, target).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
)
)
return build()
}
Be sure your AudioAttributes are chosen right to read more here.
// set notification audio
builder.setDefaults(Notification.DEFAULT_VIBRATE);
//OR
builder.setDefaults(Notification.DEFAULT_SOUND);
For Kotlin you can Try this.
var builder = NotificationCompat.Builder(this,CHANNEL_ID)
.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)

Categories

Resources