Hi custom FCM notifications have stopped showing up on Android. they used to work before but after I updated all the gradle libraries notifications have stopped showing up on the deivce.
The code is executed with breakpoints and no errors are shown but notifications are not shown.
I am clueless on how to investigate this.
any pointers would be very very useful.
This is the code that should show custom notifications with a two layouts for small and large notifications sizes.
private fun showCustomPushNotification(message: RemoteMessage, chargerId: String) {
val notificationLayout = RemoteViews(
packageName,
R.layout.pgin_requires_approval_notification_small
)
val notificationLayoutExpanded = RemoteViews(
packageName,
R.layout.pgin_requires_approval_notification_large
)
val title = message.data[MSG_TITLE]
val subTitle = message.data[MSG_SUB_TITLE]
notificationLayout.setTextViewText(R.id.tvTitle, title)
notificationLayout.setTextViewText(R.id.tvSubTitle, subTitle)
notificationLayoutExpanded.setTextViewText(R.id.tvTitle, title)
notificationLayoutExpanded.setTextViewText(R.id.tvSubTitle, subTitle)
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val customNotification = NotificationCompat.Builder(
this,
CarInfoProcessingService.APPROVE_EACH_PGIN_NOTIFICATION_CHANNEL_ID
)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.build()
val approveIntent = Intent(this, CustomNotificationListener::class.java)
approveIntent.putExtra("onClickListener", "approve")
approveIntent.putExtra("chargerId", chargerId)
val pendingApproveIntent = PendingIntent.getBroadcast(
this,
0,
approveIntent,
PendingIntent.FLAG_IMMUTABLE
)
notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnApprove, pendingApproveIntent)
val denyIntent = Intent(this, CustomNotificationListener::class.java)
denyIntent.putExtra("onClickListener", "deny")
denyIntent.putExtra("chargerId", chargerId)
val pendingDenyIntent = PendingIntent.getBroadcast(
this,
1,
denyIntent,
PendingIntent.FLAG_IMMUTABLE
)
notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnDeny, pendingDenyIntent)
val notificationManager =
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(CustomNotificationListener.WHAT_NOTIFICATION_ID, customNotification)
}
Only change that I can think of is updating libraries in gradle but as it does not show any error I am clueless.
Thanks for your help in advance.
R
You say before update it was working. So after update i think you need to check versions for your code :
Define your pending intent like below :
val pendingIntent: PendingIntent? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_IMMUTABLE)
} else {
PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT)
}
You might wanna set priority for your notification, Build.VERSION_CODES.O and above will ignore that but older devices uses this:
customNotification.priority = NotificationCompat.PRIORITY_MAX
And for Build.VERSION_CODES.O and above notification channel is needed:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
customNotification.setChannelId(Constants.NOTIFICATION_CHANNEL)
val channel =
NotificationChannel(
Constants.NOTIFICATION_CHANNEL,
Constants.NOTIFICATION_NAME,
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
}
For the constans i used they are just strings:
const val NOTIFICATION_NAME = "YourApp"
const val NOTIFICATION_CHANNEL = "YourApp_channel_01"
Just change your code regarding to this and it will work. If you can not manage to do that please let me know.
Related
I want to show the app name on top and all the notifications of the app clubbed below it. I am receiving the notification from firebase and using this code:
val messagingStyle = Notification.MessagingStyle(person)
val remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).also {
it.setLabel(context.getString(R.string.type_to_reply))
}.build()
val mChannel =
NotificationChannel(channelId, newMessage.text, NotificationManager.IMPORTANCE_HIGH)
mChannel.description = newMessage.text.toString()
mChannel.enableVibration(true)
val notifManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notifManager.createNotificationChannel(mChannel)
val statusBarNotification = notifManager.activeNotifications.firstOrNull {
it.id == channelId.toInt()
}
if (statusBarNotification == null) {
val builder: Notification.Builder = Notification.Builder(context, channelId)
val pendingIntent: PendingIntent =
PendingIntent.getActivity(
context,
channelId.toInt(),
navIntent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
)
builder.setContentTitle(newMessage.text)
.setSmallIcon(R.drawable.my_logo) // required
.setContentText(newMessage.text) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.addAction(
getNotificationIntent(context, channelId.toInt(), remoteInput)
)
.setWhen(newMessage.timestamp)
.setStyle(messagingStyle.addMessage(newMessage))
.setContentIntent(pendingIntent)
.setGroup(MY_NOTIFICATIONS)
.setGroupSummary(true)
val notification = builder.build()
notifManager.notify(channelId.toInt(), notification)
} else {
var notificationBuilder = recoverBuilder(context, statusBarNotification.notification)
notificationBuilder.also {
val messageStyle = it.style as Notification.MessagingStyle
messageStyle.addMessage(newMessage.text, newMessage.timestamp, person)
it.style = messageStyle
}
notifManager.notify(channelId.toInt(), notificationBuilder.build())
}
This is what I want(like whatsapp)
[![whatsapp screenshot]]
[1]: https://i.stack.imgur.com/1j3Xf.jpg
But this is how it is appearing right now. all the notifications are seperate.
[![screenshot]]
[2]: https://i.stack.imgur.com/UdhrY.jpg
There are similar posts here about merging notifications. They offer different approaches so check them out to see which one is applicable to your use case:
Firebase merge similar notifications in Android
How to merge push notifications like whatsapp does in FirebaseMessagingService
Can't get grouped/bundled notifications with
FirebaseMessagingService
I want to make Android Notifications to show even when the app is closed like any chat app
i tried using this->
fun createnotfictionchannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "Title"
val desc = "desc"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("id", name, importance).apply {
description = desc
}
val notificationManager : NotificationManager? = requireActivity().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
notificationManager!!.createNotificationChannel(channel)
}
}
fun sendNote(title:String, description:String){
val pendingIntent: PendingIntent =
Intent(requireContext(), MainActivity::class.java).let { notificationIntent ->
PendingIntent.getActivity(requireContext(), 0, notificationIntent,
PendingIntent.FLAG_IMMUTABLE)
}
val builder =
NotificationCompat.Builder(requireContext())
.setSmallIcon(R.drawable.ic_baseline_notifications_24)
.setContentTitle(title)
.setContentText(description)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setCategory(Notification.CATEGORY_MESSAGE)
.setAutoCancel(true)
with(NotificationManagerCompat.from(requireContext())){
notify(1, builder.build())
}
}
But once the app is closed there isn't any notifications,
What can i use that has as little code possible?
Thanks
First of all, I am sorry if there is a topic on this but I didn't found the exact solution.
The problem is that, I am setting a notification but when I close the app, the notification is removed automatically by the system. You might think it is relevant to onGoing attribute but as far as I know it is required user interaction. In my case, notifications disappear by itself.
Here is the code:
class NotificationHelper {
private val CHANNEL_ID: String = "Default Channel ID"
private var context: Context
private var notificationManager: NotificationManager
private var notificationBuilder: NotificationCompat.Builder
constructor(context: Context) {
this.context = context
this.notificationManager =
this.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// NotificationChannel is required on Oreo and newer
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.notificationManager.createNotificationChannel(
NotificationChannel(
CHANNEL_ID,
this.context.getString(R.string.channel_name),
NotificationManager.IMPORTANCE_HIGH
)
)
}
notificationBuilder = NotificationCompat.Builder(this.context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_set_notification_black)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setNotificationSilent()
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(
PendingIntent.getActivity(
this.context, 0, Intent(
this.context,
MainActivity::class.java
), 0
)
)
}
fun setNotification(notification: Notification) {
val deleteIntent = Intent(context, BootOrUpdateBroadcastReceiver::class.java)
deleteIntent.action = GlobalInfo.ACTION_NOTIFICATION_DELETE
deleteIntent.putExtra(GlobalInfo.NotificationEntry.COLUMN_ID, notification.id)
val notificationToBeShown: android.app.Notification = this.notificationBuilder
.setContentTitle(notification.title)
.setContentText(notification.content)
.setStyle(NotificationCompat.BigTextStyle().bigText(notification.content))
.setAutoCancel(notification.removeByClick)
.setOngoing(!notification.removeBySwipeAway)
.setDeleteIntent(
PendingIntent.getBroadcast(
context,
0,
deleteIntent,
PendingIntent.FLAG_CANCEL_CURRENT
)
)
.build()
with(NotificationManagerCompat.from(this.context)) {
// notificationId is a unique int for each notification that you must define
notify(notification.id, notificationToBeShown)
}
}
fun clearNotification(notificationId: Int) {
this.notificationManager.cancel(notificationId)
}
fun clearAllNotifications() {
this.notificationManager.cancelAll()
}
}
Also, I thought to use a sticky service but it doesn't make sense to me. Services are also being destroyed as the application is closed. There should be another way.
Thanks in advance :)
I'm trying to set background color (white) to my custom notification view, but on android X notification comes with black background. Below is my code to generate notification:
context?.let {
val notificationLayout = RemoteViews(it.packageName, R.layout.custom_notification_collapsed)
val notificationLayoutExpanded = RemoteViews(it.packageName, R.layout.custom_notification)
notificationLayout.setImageViewResource(R.id.iv_icon, R.drawable.custom_notification_icon)
notificationLayout.setTextViewText(R.id.tv_app_name, it.getString(R.string.app_name))
notificationLayout.setTextViewText(R.id.time_stamp, it.getString(R.string.just_now))
notificationLayoutExpanded.setImageViewResource(R.id.iv_icon, R.drawable.custom_notification_icon)
notificationLayoutExpanded.setTextViewText(R.id.tv_app_name, it.getString(R.string.app_name))
notificationLayoutExpanded.setTextViewText(R.id.tv_popup_content, it.getString(R.string.class_meeting_for_class_name_is_beginning_please_check_in_if_you_are_present))
notificationLayout.setTextViewText(R.id.time_stamp, it.getString(R.string.just_now))
// Create an explicit intent for an Activity in your app
val intent = Intent(it, SplashActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
val fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT)
var builder = NotificationCompat.Builder(it, AppConstants.CHANNEL_ID)
.setSmallIcon(R.drawable.custom_notification_icon)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContent(notificationLayoutExpanded)
.setAutoCancel(true)
.setColor(resources.getColor(R.color.mdtp_white))
.setFullScreenIntent(fullScreenPendingIntent, true)
.setCustomContentView(notificationLayoutExpanded)
.setCustomBigContentView(notificationLayoutExpanded)
var customNotificationCompat: NotificationManagerCompat = NotificationManagerCompat.from(it)
customNotificationCompat.notify(0, builder.build())
}
I'm making an app that i schedule notifications for a day say morning , evening and night - the problem is if i opened the app at night the old notifications of morning and evening shows . i use below code
val alarmMgr = MyApplication.appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager?
val intent = Intent(MyApplication.appContext, MyAlarmReceiver::class.java)
intent.putExtra("Title",title)
intent.putExtra("Body",body)
intent.putExtra("Id",id)
intent.putExtra("Sound",sound)
val pendingIntent = PendingIntent.getBroadcast(MyApplication.appContext, id, intent, 0)
alarmMgr!!.setRepeating(AlarmManager.RTC_WAKEUP,time,AlarmManager.INTERVAL_DAY,pendingIntent)
With this part inside onReceive of alarm
private fun sendNotification( intent: Intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationID = intent.extras!!.getInt("Id")!!
val title = intent.extras!!.getString("Title")
val body = intent.extras!!.getString("Body")
val sound = intent.extras!!.getString("Sound")
val channelID = "com.ebookfrenzy.notifydemo.news"
val resultIntent = Intent(MyApplication.appContext, MainActivity::class.java)
resultIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(
MyApplication.appContext,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
val notification = Notification.Builder(MyApplication.appContext,
channelID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setChannelId(channelID)
.setContentIntent(pendingIntent)
.build()
sound?.let {
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + MyApplication.appContext.getPackageName() + "/raw/$it")
}
var notificationManager = MyApplication.appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager?.notify(notificationID, notification)
} else {
}
}