Android notification sound doesn't work - android

var notificationBuilder = new Notification.Builder(context)
.SetSmallIcon(Resource.Drawable.Icon)
.SetLargeIcon(BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Drawable.Icon))
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetStyle(new Notification.BigTextStyle().BigText(message))
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
.SetVibrate(new long[] { 1000, 1000 })
.SetDefaults(NotificationDefaults.All);
var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notificationBuilder.Build());
Where i was wrong? Why my notifications doesn't have any sound or vibration?

Remove .SetDefaults(NotificationDefaults.All), you are resetting the notification settings that you just built:
var notification = new Notification.Builder(Application.Context)
.SetSmallIcon(Resource.Mipmap.Icon)
.SetLargeIcon(BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Mipmap.Icon))
.SetContentTitle("Stack")
.SetContentText("OverFlow")
.SetAutoCancel(true)
//.SetContentIntent(pendingIntent)
.SetStyle(new Notification.BigTextStyle().BigText("OverFlow"))
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
.SetVibrate(new long[] { 1000, 1000 })
.Build();
var notificationManager = (NotificationManager)Application.Context.GetSystemService(Context.NotificationService);
notificationManager.Notify(1, notification);

Try passing uri to setSound in notificationBuilder like this
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(uri);

Related

Notification icon is not displaying

I am receiving a notification but without notification icon...
I don't understand why the icon is not displaying in the notification
notification:
{
title: `Hey ${userName}`,
body: `You Have One New Notification`,
icon: "default",
color: "#0000FF",
sound: "default",
priority: "high",
}
Firebasemessagingservice
String notificationTitle=remoteMessage.getNotification().getTitle();
String notificationbody=remoteMessage.getNotification().getBody();
String click_action=remoteMessage.getNotification().getClickAction();
// String from_user_id=remoteMessage.getData().get("User_data");
NotificationCompat.Builder mbuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.uemround)
.setContentText(notificationbody)
.setContentTitle(notificationTitle)
.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent resultIntent=new Intent(click_action);
//resultIntent.putExtra("user_id",from_user_id);
PendingIntent pendingIntent= (PendingIntent) PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mbuilder.setContentIntent(pendingIntent);
int mnotificationId=(int)System.currentTimeMillis();
NotificationManager mnotifymgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mnotifymgr.notify(mnotificationId,mbuilder.build());
You need to add notification channel for the version oreo and above. Copy the below code and run it. It will work
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "Channel_id";
CharSequence channelName = "Your app name";
int channelImportance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) // channel id is mandatory
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE, 1, 1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
// and icon should be transparent for lollipop and above version
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.icon_transperent);
} else {
notificationBuilder.setSmallIcon(R.drawable.icon);
}
notificationManager.notify(new Random().nextInt(9999 - 1000) + 1000/* ID of notification */, notificationBuilder.build());
}
} else {
if (notificationManager != null) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "")//no need to add the channel id
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE, 1, 1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
// and icon should be transparent for lollipop and above version
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.icon_transperent);
} else {
notificationBuilder.setSmallIcon(R.drawable.icon);
}
notificationManager.notify(new Random().nextInt(9999 - 1000) + 1000/* ID of notification */, notificationBuilder.build());
}
}
onMessageRecieved ->
check -> if (remoteMessage.getData().size() > 0) then
you can set your data as per your requirement and then
notificationBuilder = new NotificationCompat.Builder(this, notification_channel_id)
.setContentTitle(remoteMessage.getData().get("Title"))
.setContentText(remoteMessage.getData().get("Body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
// .setContentInfo("Important")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setColor(getResources().getColor(R.color.colorAccent))
.setLights(Color.RED, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setNumber(++numMessages)
.setSmallIcon(R.mipmap.ic_launcher);
sorry for bad english ;-)

how to make heads-up notifications in android studio?

As I've searched how to reveal heads-up, I found a solution that is to set priority high but it still doesn't show heads-ups. I've got a notfication massenger service. Its function is as below. Is there anything I missed in settings?
Heading
mycode:
private void sendNotification(String body, String title) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
remoteViews.setTextViewText(R.id.title, "Firebase");
remoteViews.setTextViewText(R.id.body, body);
remoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.mipmap.ic_launcher)
.setCustomContentView(remoteViews)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setCustomHeadsUpContentView(remoteViews)
.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_ALL)
.setTicker(body)
.setSound(notificationSound)
.setFullScreenIntent(pendingIntent, true)
.setContentIntent(pendingIntent);
notificationManager.notify(0, notifBuilder.build());
}
Try replacing .setContentIntent(pendingIntent) with .setFullScreenIntent(pendingIntent, true) in your Notification Builder.

Android Studio Notification Lights

Why are the LED Lights not working with this Notification?
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setDefaults(0)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getData().get("username"))
.setContentText(remoteMessage.getNotification().getBody())
.setVibrate(new long[]{1500, 0, 1500, 0})
.setLights(Color.BLUE, 2000, 1000)
.setWhen(remoteMessage.getSentTime())
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
Can you help? I cant find the error.
I put the notificationBuilder.setDefaults(0) on top but it is still not working

Android Push Notification: Style doesn't work when application is closed or killed

NotificationCompat.InboxStyle notifInbox = new NotificationCompat.InboxStyle();
notifInbox.addLine("First Word");
notifInbox.addLine("Second Word");
notifInbox.addLine("Third Word");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notificon)
.setContentTitle(title)
.setSubText("FROM: SYSTEM")
.setContentInfo("X")
.setStyle(notifInbox)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Why is it when my application is closed or killed, then I send a notification, the style doesn't work? Or how to prevent the notification to show when the application closed or killed? I'm using FCM. Any help is appreciated.
It's working for me you can try it.
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
// Do something for lollipop and above versions
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setPriority(Notification.PRIORITY_HIGH)
.setContentTitle(TextUtils.isEmpty(title) ? "XYZ" : title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setColor(getResources().getColor(R.color.colorPrimary));
mBuilder.setSound(Uri.parse("content://settings/system/notification_sound"));
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} else {
// do something for phones running an SDK before lollipop
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(TextUtils.isEmpty(title) ? "xyz" : title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setSound(Uri.parse("content://settings/system/notification_sound"));
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

CWAC updater issue with notification

I'm using this library https://github.com/commonsguy/cwac-updater to provide feature of auto update for an app.
I read the demoUpdater it uses a deprecated API for Notification, so I used the following code:
ConfirmationStrategy buildPreDownloadConfirmationStrategy(Context mContext) {
long[] vibrate = { 0, 100, 200, 300 };
NotificationManager mNotificationManager= (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Update Available")
.setContentText("Click to download the update!")
.setVibrate(vibrate)
.setAutoCancel(true)
.setLights(Color.BLUE, 500, 500);
mNotificationManager.notify(1, mBuilder.build());
return null;
//return(new NotificationConfirmationStrategy());
}
but can't get it working.
I got it working, If anyone finds trouble then use below code
ConfirmationStrategy buildPreDownloadConfirmationStrategy(Context mContext) {
long[] vibrate = { 0, 100, 200, 300 };
NotificationManager mNotificationManager= (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Update Available")
.setContentText("Click to download the update!")
.setVibrate(vibrate)
.setAutoCancel(true)
.setLights(Color.BLUE, 500, 500);
Notification notification = mBuilder.build();
mNotificationManager.notify(1, notification);
return(new NotificationConfirmationStrategy(notification));
}

Categories

Resources