Can I change push notification sound when app is closed? - android

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.

Related

How to send a notification without waking the screen on Android

I have a foreground service in my app, whose persistent notification has a timer in it, which means that the notification is sent once per second to update the timer that is shown in the notification. This means that on some devices, where notifications are set to either wake the screen entirely or show a dark version on the screen briefly, the screen is constantly awake.
Is there a way to send the notification in a way that it won't wake up the screen? Setting it as a silent notification on the device fixes this, but the point of a foreground service notification is that it's prominent on the device, so this isn't a great solution, and not all users would know to do that.
This is how I'm building the notification:
NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Currently Reading")
.setSound(null)
.setContentIntent(TaskStackBuilder.create(this).run {
addNextIntentWithParentStack(timerIntent)
getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
})
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
Have you tried updating the notification instead? And use setOnlyAlertOnce()
"You can optionally call setOnlyAlertOnce() so your notification interupts the user (with sound, vibration, or visual clues) only the first time the notification appears and not for later updates."
Check this link
https://developer.android.com/training/notify-user/build-notification.html#Updating
setPriority(NotificationCompat.PRIORITY_DEFAULT)?
lower it, so wont be shown on lockscreen (android 7 and up)
and here we go with a small fix for this part:
setPriority(NotificationCompat.PRIORITY_LOW)
you can also add:
setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
but it'll also remove notification from lockscreen...
docs:
https://developer.android.com/training/notify-user/build-notification.html#lockscreenNotification
https://developer.android.com/training/notify-user/channels#importance
hope this helps =]

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.

Handle Received message firebase notifications

I want to trigger a specific event (like ordinary vibration) when the device gets an notification from the Firebase Notifications.
All I discovered so far is that one can handle the on_click of a notification that was sent with display-messages in the background of the app.
Is it possible to let the device vibrate in the very moment the notification arrives? I would love to get the users attention to participate on my field study by answering the question sheet in the moment, the notification comes in.
Thanks alot!
There are two types of messages:
data messages (with a data property in the JSON)
notification messages (with only a notification property in the JSON)
If a notification/data message arrives while your app is active, you can handle it in onMessageReceived and do whatever you want.
If a data message arrives while your app is inactive, you can handle it in onMessageReceived and do whatever you want.
To make the phone vibrate in these cases, see the excellent example from Wizard.
If a notification message arrives while your app is inactive, it is automatically handled by the system and you can't control what happens.
Also see the documentation on message types as there are some more nuances.
Is it possible to let the device vibrate in the very moment the
notification arrives?
Yes, you can define it while generating Notification using NotificationManager class -
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
....
..
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

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

Play multiple different notification sounds from app

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

Categories

Resources