Play multiple different notification sounds from app - android

Is it possible for an iOS or android app to launch a system notification. And have the sound played by the notification change depending on some criteria?
If so can both platforms do so?

Both platforms can do so.
In iOS, as mentioned in the comment, you can send a push notification payload with a sound filename.
For example, the following payload would display an alert and play sound-file-name if this file is bundled with your application.
{"aps":{"alert":"my message","sound":"sound-file-name"}}
In Android, when you build a notification, you can set a sound to the notification using setSound(uri).
public NotificationCompat.Builder setSound (Uri sound)
Set the sound to play. It will play on the default stream.
Or you can use setDefaults(Notification.DEFAULT_SOUND) for playing the default sound.
For example :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_SOUND)
.setTicker (text)
.setSmallIcon(R.drawable.icon)
.setContentText (text)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(text))
.setAutoCancel(true).setNumber (4)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

Related

How to stop ringtone manager in firebase notification it is playing but not stoping

I have one FCM based notification when I am receiving notification I am playing custom mp3 from row folder. But once notification comes ringtone manager started playing mp3 but it doesn't stop.
You can set the sound to the notification whilst building it rather than using RingtoneManager:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSound(soundUri); // Sound to play
When you want to stop your ringtone then Release your media player.
I have solved this issue. Once I am getting notification from FCM I set the sound to null and play custom notification through Ringtone manager. Please note size and length of notification should be small.

Enable all notification settings by default in my android app

Is there any way I can enable all notification settings by default when my app gets installed ?
Users are receiving notifications but sound is disabled by default and we need to manually enable it on the device. Not all users can do this manually. It would be great to know if there is any way we can check all these things when our app gets installed like WhatsApp or Telegram (they have everything checked by default)
Try using with this below permission in AndroidManifest file
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
and set notification priority for both below and above Oreo versions IMPORTANCE_HIGH for Oreo and above, and PRIORITY_HIGH or PRIORITY_MAX for below Oreo versions
Reference link for priority note
Priority for version below Oreo
mBuilder.setSmallIcon(R.drawable.app_logo)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setSound(soundUri)
.setPriority(NotificationCompat.PRIORITY_HIGH) // prirority here for version below Oreo
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.app_logo))
.setContentText(message)
.build();
Priority for Oreo and above
Refer this link
WhatsApp, Telegram, Snapchat, and so on. These are all white-listed apps which means the package names are hardcoded at OS level to allow some of the permissions enabled by default. Our apps are not so. Hence, we need the user to enable those permissions manually.
You can check this by yourself. Create a new android application give the package name of the Telegram application(org.telegram.messenger) and just run it. Don't do any code at all, and no need to open the app too. Simply go to the notification settings of the newly created application, where you find all the permissions enabled by default.
Hope you got the answer.
Android 8 or higher you need to use NotificationChannel to enable sound, vibration, sound etc.
Uri notification_sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(notification_sound, attributes);//for enable sound
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
but in Redmi note 5 pro (MIUI 10.2.1.0) still notification sound is disabled. I think there is a bug in MIUI. Run this same code in mi A1(Android one mobile) everything fine. It works.
refer this link to know more about Notification Channel

No notification sound on the emulator

I am going nuts with a problem with notifications on Android: While I was developing my project, suddenly the emulator plays no notification sounds anymore for API 26 and higher,
e.g. the API which require a channel.
Of course I have set up a channel and it has worked great before! I have reinstalled the app, deleted the channel, even set up another AVD with a API 27, same result: no sound ! (the notification does pop up)
Obviously I have checked that notification sounds are enabled, also for this specific channel, all seems OK, just no sounds.
If I play a test using:
RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();
it works as it should, so no hardware problem.
On lower APIs pre 26 where you don't need a channel, the sound does play.
Anybody had the same problem?
//make the channel
//The Config class is imported and the constants resolved, not the problem
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(
Config.CHANNEL_1_ID,
Config.CHANNEL_1_NAME,
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(Config.CHANNEL_1_DESC);
channel.enableLights(true);
channel.enableVibration(true);
channel.setShowBadge(true);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
// send notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, Config.CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(false)
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat mNotificationMgr = NotificationManagerCompat.from(context);
mNotificationMgr.notify(1, mBuilder.build());
Seems I found the answer: I had to go through the "Finish setting up your Android SDK" wizard on the emulator. Clicked "skip" for everything, now it seems to work again.
Weirdly enough, I didn't do that initially and still the notifications worked as expected... duh !
On Pie 9.0 (api 28), you need to complete setup process. After doing it, the sound of notification channels will run well. It is same for both Emulator and physical devices.
If you have not done it, all notification on device will be run on silent, regardless of importance level.
You may need to check Tools->SDK Manager->Appearance & Bahaviour->Notifications "Play sound" checkbox for the "Android emulator" and "Android" to get notifications sounds (i.e. when other sounds work, but there's not notification sound).
For me the problem was the ring volume on the emulator. Apparently it was on zero by default and there was no indication for it on the top bar. Also when pressing the volume keys it would open the media volume and not the ring volume! I had to increase the ring volume via android settings -> sound.

Android notification icon issue

I have a strange issue. I have two way to send notifications in my Android app; one from the Android service and the other through FCM.
The scenarios are as follows:
Regardless of whether the app is running or not, the icon of the notification sent from the Android service appears correctly.
When the app is running, the notification icon appears still appears correctly if I send the notification via FCM.
But if the app isn't running and I send the notification via FCM, a white square is displayed instead of the notification icon.
My code in FCMService:
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Android App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
Most likely your problem is the difference between notification-messages and data-messages.
Please read: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Use notification messages when you want FCM to handle displaying a
notification on your client app's behalf. Use data messages when you
want to process the messages on your client app.
Currently the FCM Web Console only sends notification-messages
So all the messages sent via Web Console (or via API with a notification payload) will be have in this way:
if the app is closed or in background: FCM will display the notification. if you want to customize it you can, but you need to provide specific configuration (in the manifest or in the send API call) see https://firebase.google.com/docs/cloud-messaging/android/client#manifest
if the app is in foreground: FCM will call onMessageReceived()
.
If the behavior that you want is that onMessageReceived() is always called:
then you need to use a data-only (no notification) message
This is a FMC bug detailed in github fcm page.
https://github.com/firebase/quickstart-android/issues/4

Can I change push notification sound when app is closed?

When I send notifications to android phones via GCM i send a sound name which is played if the app is running, or when the user click the notification.
My question is can i change the sound of the notification ? not when the user click on notification but when the notification pop up in the phone. I know it's possible, Yo app plays sound "YO" when the notification push pop up.
Sorry for my english :s and thank you for help !
When you build a notification, you can set a sound to the notification using setSound(uri).
public NotificationCompat.Builder setSound (Uri sound)
Set the sound to play. It will play on the default stream.
Or you can use setDefaults(Notification.DEFAULT_SOUND) for playing the default sound.
For example :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_SOUND)
.setTicker (text)
.setSmallIcon(R.drawable.icon)
.setContentText (text)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(text))
.setAutoCancel(true).setNumber (4)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
As far as I know, this sound is played when the notification is displayed (though I haven't checked). But if it doesn't, you can play the sound regardless of the notification in the code that shows the notification (in your broadcast receiver or intent service) before calling mNotificationManager.notify.

Categories

Resources