Forground service notification get replaced by another notification - android

I have two forground service FS1 and FS2 , with each one i create notification , if FS1 is running and i start FS2 the old notification get replaced , i want both to stay ,
this is how i create notification
private fun createNotification(): Notification {
val notificationChannelId = "123"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(
notificationChannelId,
"Notification service channel",
NotificationManager.IMPORTANCE_HIGH
).let {
it.description = "Notification Service channel"
it.enableLights(true)
it.lightColor = Color.RED
it.enableVibration(true)
it.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
it
}
notificationManager.createNotificationChannel(channel)
}
val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent ->
PendingIntent.getActivity(this, 0, notificationIntent, 0)
}
val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder(
this,
notificationChannelId
) else Notification.Builder(this)
return builder
.setContentTitle("title")
.setContentText("Content")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.notification_icon)
.setTicker("Ticker text")
.setPriority(Notification.PRIORITY_HIGH)
.build()
}
and in the onCreate methode
val notification = createNotification()
startForeground(1, notification)
and in the other service i create notification with the same way !

Ok my bad , i found out why , at onCreate methode , in both services i was using id 1 , i should use different id

Related

setFullScreenIntent is not coming to foreground in samsung Android OS 11

I am trying to show Call notification like whatsup with audio
I had implemented full screen notification for call using setFullScreenIntent and pending intents as below but in other devices I am getting notification to foreground except in Samsung device with OS 11.
var notificationBuilder: NotificationCompat.Builder? = null
val ringUri: Uri = Settings.System.DEFAULT_RINGTONE_URI
var contentView = RemoteViews(packageName, R.layout.call_notification_layout)
contentView.setOnClickPendingIntent(R.id.imgCallYes, receiveCallPendingIntent)
contentView.setOnClickPendingIntent(R.id.imgCallNo, cancelCallPendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = NotificationCompat.Builder(
applicationContext,
CHANNEL_ID
)
.setSmallIcon(R.mipmap.ic_launcher)
.setContent(contentView)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setSound(ringUri)
.setFullScreenIntent(callDialogPendingIntent, true)
val notificationChannel = NotificationChannel(
CHANNEL_ID,
"My Notifications",
NotificationManager.IMPORTANCE_HIGH
)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// Configure the notification channel.
val att = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
notificationChannel.setSound(ringUri, att)
notificationChannel.description = "body"
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
} else {
notificationBuilder =
NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContent(contentView)
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setSound(ringUri)
.setFullScreenIntent(callDialogPendingIntent, true)
}
var incomingCallNotification: Notification? = null
if (notificationBuilder != null) {
incomingCallNotification = notificationBuilder.build()
}
startForeground(NOTIFICATION_ID, incomingCallNotification)
IN Android 12 I added PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT for pending intent, now notification is coming

Notification doesn't vibrate

const val channelId = "notification_channel"
const val channelName = "com.deskmateai.t2chaiwala"
val vibration = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200)
class MyFirebaseMessagingService: FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
generateNotification(remoteMessage.notification!!.title!!, remoteMessage.notification!!.body!!)
}
// generating notification
private fun generateNotification(title: String, description: String){
val builder: NotificationCompat.Builder = NotificationCompat.Builder(applicationContext, channelId)
.setContentTitle(title)
.setSmallIcon(R.drawable.tea_notify_logo)
.setAutoCancel(true)
.setContentText(description)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setVibrate(longArrayOf(500, 500))
val v = applicationContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
v.vibrate(1000)
val manager: NotificationManagerCompat = NotificationManagerCompat.from(applicationContext)
manager.notify(1, builder.build())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
channel.enableLights(true)
channel.enableVibration(true)
channel.vibrationPattern = vibration
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
manager.notify(1, builder.build())
}
}
I am making an app in android for that i have integrated firebaase push notification, but my hone is not vibrating when notification come .
I have also added vibration permission in android manifest file. and as you can see in code i have done everything to vibrate my phone on notification but it is not.
You need to set the vibration when you are creating the channel. Also, make sure to reinstall your app to apply channel changes.
private fun createCallNotificationChannel(): NotificationChannelCompat {
val channel = NotificationChannelCompat.Builder(
INCOMING_CALL_CHANNEL_ID,
NotificationManagerCompat.IMPORTANCE_HIGH
).setName("Incoming notification")
.setDescription("Incoming notification alerts")
.setVibrationEnabled(true)
.build()
return channel
}

subsequent notifications does not reload the Main Activity

My App creates a notification and when the user taps on the notification, it opens the Main Activity with some EXTRA data.
Everything works fine when the user opens the App manually or the first time the notification pops up.
But the problem is that, when the App is first opened by the notification, subsequent notifications does not reload the Main Activity. It only show it(without reload) if the App is hidden.
This is how I create the notification
fun createNotification(title: String, message: String, intent: Intent?) {
val mBuilder = NotificationCompat.Builder(
context,
NOTIFICATION_CHANNEL_ID
)
mBuilder.setSmallIcon(R.drawable.notification_ico)
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
if (intent != null) {
Log.e(TAG, "Intent dey")
val resultPendingIntent = PendingIntent.getActivity(
context,
0 /* Request code */,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
mBuilder.setContentIntent(resultPendingIntent)
} else {
Log.e(TAG, "Unavailable dey")
}
val mNotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Enterprise Advantage",
importance
)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.enableVibration(true)
notificationChannel.vibrationPattern =
longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
mNotificationManager.createNotificationChannel(notificationChannel)
}
mNotificationManager.notify(0 /* Request Code */, mBuilder.build())
}
Okay so from the comment by #Rahul Shukla,
I set an action on the intent
val intent = Intent(this, MainActivity::class.java)
intent.action = System.currentTimeMillis().toString()
and then changed the Flag of the pending intent to PendingIntent.FLAG_ONE_SHOT
val resultPendingIntent = PendingIntent.getActivity(
context,
0 /* Request code */,
intent,
PendingIntent.FLAG_ONE_SHOT
)

Notification Not Showing In Android Studio

I'm using Kotlin and Android Studio to try and push a Notification in a test app. I followed the instructions in the Android Developers site on How to create a Notification (https://developer.android.com/training/notify-user/build-notification) but I seem to be doing something wrong as my Notification is not showing anywhere. Here's my code:
val intent = Intent(context, Profile::class.java)
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);
val builder = NotificationCompat.Builder(this.context)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_MAX)
builder.setContentIntent(pendingIntent).setAutoCancel(true)
val mNotificationManager = message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(mNotificationManager) {
notify(123, builder.build())
I really don't know what I'm missing so I would be grateful for any help.
According to your code, you are not creating a Notification channel.
Notification channel is necessary from Android Oreo and above
So if you are running the app Android O and above devices without a notification channel, your notification won't show up.
Create Notification Channel
fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "all_notifications" // You should create a String resource for this instead of storing in a variable
val mChannel = NotificationChannel(
channelId,
"General Notifications",
NotificationManager.IMPORTANCE_DEFAULT
)
mChannel.description = "This is default channel used for all other notifications"
val notificationManager =
mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
}
Then create a notification using the same channelId while creating a notification.
createNotificationChannel()
val channelId = "all_notifications" // Use same Channel ID
val intent = Intent(context, Profile::class.java)
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);
val builder = NotificationCompat.Builder(this.context, channelId) // Create notification with channel Id
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_MAX)
builder.setContentIntent(pendingIntent).setAutoCancel(true)
val mNotificationManager =
message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(mNotificationManager) {
notify(123, builder.build())
Hope it helps.
try something like
val intent = Intent(context, Profile::class.java)
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0)
val mNotificationManager =
message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Don't forget this check
val channel = NotificationChannel (
channelId,
"my_notification",
NotificationManager.IMPORTANCE_HIGH
)
channel.enableLights(true)
channel.lightColor = Color.GREEN
channel.enableVibration(false)
val builder = NotificationCompat.Builder(this.context, "channel_id")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_MAX)
builder.setContentIntent(pendingIntent).setAutoCancel(true)
mNotificationManager.createNotificationChannel(channel)//Notice this
mNotificationManager.notify(123, builder.build())
}
else {
//Create Notifcation for below Oreo
}
Does your Logcat say something like:
E/NotificationManager: notifyAsUser: tag=null, id=123, user=UserHandle{0}
Also have a look at your line:
val intent = Intent(context, Profile::class.java)
IF all else fails create a test Actvity with hello world.
And then make the line look like:
val intent = Intent(context, testActivity::class.java)
Remember to add the activity to your Manifest
The following line
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);
should not have a semi colon. Kotin doesn't need this.
Is Profile your Class or System Class?
Try out this method and you can pass message and title dynamically :
private fun showNotification(title: String?, body: String?) {
val intent = Intent(this, Profile::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notificationBuilder.build())
}

Why my notification not work in background when my app is kill and close in android

When my app is open, notification works perfectly but when in background it doesn't work .I want the notification to be displayed even when my app is killed or closed in my phone. But it is not working. Here is my code :-
1) MyFirebaseMessagingService.kt :-
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
showNotification(remoteMessage!!)
Log.e("fcm", "HElloo Call")
}
#SuppressLint("WrongConstant")
private fun showNotification(message: RemoteMessage) {
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val i = Intent(this, MainActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT)
val androidChannel = NotificationChannel("1",
"sdccscsc", NotificationManager.IMPORTANCE_DEFAULT)
androidChannel.enableLights(true)
androidChannel.enableVibration(true)
androidChannel.lightColor = Color.GREEN
androidChannel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
val builder = NotificationCompat.Builder(this, "1")
.setAutoCancel(true)
.setContentTitle(message.data["title"])
.setContentText(message.data["message"])
.setSmallIcon(R.drawable.main_logo)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
manager.createNotificationChannel(androidChannel)
}
val i = Intent(this, MainActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT)
val builder = NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle(message.data["title"])
.setContentText(message.data["message"])
.setSmallIcon(R.drawable.main_logo)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
manager.notify(0, builder.build())
}
Check your logcat if you are receiving the notification. IS your notification being displayed in the system tray?
There are two types of Message types
Notification message
FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.
Data message
Client app is responsible for processing data messages. Data messages have only custom key-value pairs.
as per my comment
You need to send notification data in Data messages
SAMPLE FORMAT
{
"message":{
"token":"your token",
"data":{
"key1" : "value1",
"key2" : "value2",
"key3" : "value4",
}
}
}
EDIT
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Intent intent = new Intent(this, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
intent.putExtra("ID",object.getString("data"));
} catch (JSONException e) {
e.printStackTrace();
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "Your_channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("Description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setColor(ContextCompat.getColor(this, R.color.colorDarkBlue))
.setContentTitle(getString(R.string.app_name))
.setContentText(remoteMessage.getNotification().getBody())
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(1000, notificationBuilder.build());
notificationManager.cancelAll();

Categories

Resources