Android Notification Builder don't play custom sound - android

I post the code that I use to retrieve a message and display a notification from firebase. I receive the notification with the correct text and title, but this notification is silent. Even I set the default sound or the custom sound.
How do I play the correct sound?
public class NotificationMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("sb.fbv", "NotificationMessaginService.onmessageReceived");
String title = remoteMessage.getNotification().getTitle();
String subText = remoteMessage.getNotification().getBody();
String message = remoteMessage.getData().get("subtext");
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
nb.setContentTitle(title);
nb.setSubText(message);
nb.setContentText(subText);
nb.setSmallIcon(R.drawable.notificavedelago);
nb.setAutoCancel(true);
nb.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
nb.setLights(Color.BLUE, 3000, 3000);
//SOUND DEFAULT
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
nb.setSound(alarmSound);
//CUSTOM SOUND
//Uri customSound= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound);
//nb.setSound(customSound);
nb.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, nb.build());
}
}

To set a custom alarm sound in your notification builder, do this:
int soundResId = R.raw.myCustomSoundResource;
Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + soundResId);
nb.setSound(soundUri, AudioManager.STREAM_ALARM);
using the Resource ID number of a file like res/raw/myCustomSoundResource.ogg. It can be in any of these supported file formats.

Related

How to open Play Store via a FCM notification?

I would like to send a notification to my users to remember them to update the app, but I can't change the app code as obviously the problem is that the app is outdated.
Is there a way to open Play Store via a FCM notification, using clickAction or something like that, without needing to make changes to the app?
you can open play store-specific app using FCM you just need to put following logic in your onMessageReceived of your FCM custom class.
Bellow is the Logic:
public static void openStore(Context context,RemoteMessage remoteMessage) {
Intent intent;
if (remoteMessage.getData().containsKey("appPackageName")) {
final String appPackageName = remoteMessage.getData().get("appPackageName"); // getPackageName()
try {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
} catch (android.content.ActivityNotFoundException anfe) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
int notificaionId = 1;
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.BigTextStyle bigTextNotiStyle = null;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int color = ContextCompat.getColor(this, R.color.profileFontColor);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("" + remoteMessage.getData().get("title"))
.setContentText("" + remoteMessage.getData().get("desc"))
.setStyle(bigTextNotiStyle)
.setAutoCancel(true)
.setColor(color)
.setContentIntent(pIntent)
.setLights(Color.RED, 3000, 3000);
notificationManager.notify(notificaionId, mBuilder.build());
}
}

How to debug if android.resource:// does exist when having Uri to that resource

I am working on push notifications with Android studio with my phone plugged in via USB as debugging device and notifications are coming through just fine custom text set, etc. However when I try to set custom sound by using
Uri defaultSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alarm);
which then evaluates to android.resource://gcm.play.android.samples.com.gcmquickstart/2131099648 it does not play ball.
My guess is that sound file is not there and it simply fails silently.
Code:
private void sendNotification(String message) {
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);
Uri defaultSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alarm);
// RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(message)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
How would I debug the application where by having Url I could confirm if resource is there or not?

Android Local Notification

I need to show a local notification in android in title bar when certain event occurs. I want the notification also play certain sound as well.
On tap of notification I want app should not be launched and notification should be cleared.
I am using this code but on tap of notification it opens my app again.
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Title", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, JobView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(JobView.this, title, message, null);
notificationManager.notify(1010, notification);
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Title", System.currentTimeMillis());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.YOUR_SOUND_FILE);
notificationManager.notify(1234, builder.build());
Just replace the File Reference from your Raw folder in the code and the code should work fine.
I hope this helps.
Try this:
CharSequence title = "App title";
CharSequence message = "This to alert you that estimated time is getting complete.";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle(title);
mBuilder.setSound(Uri.parse("android.resource://"
+ this.getPackageName() + "/" + R.raw.sound));
mBuilder.setContentText(message);
mBuilder.setTicker(message);
mBuilder.setWhen(System.currentTimeMillis());
NotificationManager notificationManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
mBuilder.setContentIntent(pendingIntent);
notificationManager.notify(1010, mBuilder.build());
Change R.raw.sound with path to your sound file

How to set notification with custom sound in android

I copied the mp3 (kalimba.mp3) file into the raw folder in the res folder. But when the notification is triggered it produces the default sound.
This is how I make a notification:
protected void GenerateNotify() {
NotificationManager myNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(android.R.drawable.ic_btn_speak_now,"hi",100);
Intent intent=new Intent(getApplicationContext(),as.class);
PendingIntent contentintent=PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), "Hi","date", contentintent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba);
myNotificationManager.notify(NOTIFICATION_ID,notification);
}
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
if defined DEFAULT_SOUND, then the default sound overrides any sound
R.raw.kalimba is an integer resource ID; you want the name of the sound resource in that Uri. So try:
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/raw/kalimba");
Try this:
Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/notifysnd);
notification.setSound(sound);
You should replace this line:
notification.sound = Uri.parse("android.resource://com.example.serviceproject/" + R.raw.kalimba);
with this:
notification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba"));
or in some cases:
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/kalimba");
I make this static function in HelperClass.java
public static void givenotification(Context context,String message) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, DesireActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new Notification();
NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(context);
notification = builder.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker(context.getString(R.string.app_name)).setWhen(System.currentTimeMillis())
.setAutoCancel(true).setContentTitle(context.getString(R.string.app_name))
.setContentText(message).build();
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.kalimba);
notificationManager.notify(1, notification);
}
Then any Activity like in MainActivity
HelperClass.givenotification(MainActivity.this,"Your Notification Message");
OR in fragment
HelperClass.givenotification(getActivity(),"Your Notification Message");
Hope this help someone.
I have implemented this way to set custom Notification sound in my app.
int notificationID=001;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.room);
mBuilder.setContentTitle("Room Rent , Pay Room Rent");
mBuilder.setContentText("Hi, Its time to pay your Room Rent!");
mBuilder.setAutoCancel(true);
mBuilder.setColor(getResources().getColor(R.color.colorViolet));
mBuilder.setSound(Uri.parse("android.resource://com.roommanagement.app/" + R.raw.notification_sound));
if (mNotificationManager!=null){
mNotificationManager.notify(notificationID, mBuilder.build());
}
Hop this will help you.Thanks...

Custom notification sound not playing

I'm trying to make a custom sound play on a status bar notification. The .mp3 file is in res/raw/. But when I notify the user the sound is not played. I've tryied with MediaPlayer, and it works, but I dont want to make it play with MediaPlayer.
Here is my method:
public void showNotification()
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.feedback; // icon from resources
CharSequence tickerText = mContext.getString(R.string.statusbar_notification); // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = mContext.getString(R.string.statusbar_notification); // message title
CharSequence contentText = mContext.getString(R.string.statusbar_notificatione_detailed); // message text
Intent notificationIntent = new Intent(mContext, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
//notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/R.raw.notificationsound");
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
}
Thanks.
From the documentation for ContentResolver:
The Uri should be one of the following formats:
android.resource://package_name/id_number
You are passing the String "R.raw.notificationsound" which means nothing.
Instead try this:
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound );

Categories

Resources