We're using a jobScheduler to run a periodic weather network call whose results are posted to an ongoing weather notification.
Here's how we create the notification inside the jobService:
private fun createNotification(selectedLocation: City) {
val resultIntent = Intent(context, SplashActivity::class.java)
resultIntent.putExtra(AppConstants.IS_ONGOING, true)
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
val resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0)
val notificationView = getComplexNotificationView(selectedLocation) ?: return
val notification = NotificationCompat.Builder(context, context.packageName)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(getSmallIconResource(context,
if (settings.isFahrenheitEnabled())
selectedLocation.currentObservation!!.tempF!!
else
selectedLocation.currentObservation!!.tempC!!))
.setVibrate(null)
.setWhen(System.currentTimeMillis())
.setCustomContentView(notificationView)
.setContentIntent(resultPendingIntent)
.setOngoing(true)
.setAutoCancel(false)
.setGroup(AppConstants.NOTIFICATION_GROUP_ONGOING)
.build()
NotificationManagerCompat.from(context).notify(ONGOING_NOTIFY_ID, notification)
}
In our app's settings, the user can disable the ongoing notification. Here's how we're trying to cancel it:
val jobScheduler: JobScheduler? = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
jobScheduler?.cancel(OngoingNotificationJobService.ONGOING_JOB_ID)
NotificationManagerCompat.from(context).cancel(OngoingNotificationJobService.ONGOING_JOB_ID)
The problem:
The cancel call is not clearing the notification. What am I doing wrong?
You have to "NotificationManagerCompat.from(context).cancel()" your previous ONGOING_NOTIFY_ID and not your ONGOING_JOB_ID ;)
Related
I have implemented a broadcast receiver to ask the user to restart the app if it is killed. I have confirmed that the broadcast receiver is being called fine, and it runs the below line but for some reason, I am not getting any notification.
Here is the code,
class ForegroundLocationServicesRestarter : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
if (intent != null) {
if (intent.action != ForegroundLocationService.ACTION_RESTART_LOCATION_UPDATES) {
return
}
}
val notificationChannelId = "restartDeliveryApp"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(notificationChannelId, "location_notif_chan", NotificationManager.IMPORTANCE_MAX)
val manager = context.getSystemService(LifecycleService.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(notificationChannel)
}
val fullScreenIntent = Intent(context, DeliveryManActivity::class.java)
fullScreenIntent.putExtra("RESTART_TRIGGERED", true)
val fullScreenPendingIntent = PendingIntent.getActivity(
context, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
NotificationCompat.Builder(context, notificationChannelId)
.setSmallIcon(R.drawable.ic_truck_red)
.setContentTitle(context.getString(R.string.restarter_title))
.setContentText(context.getString(R.string.restarter_message))
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(fullScreenPendingIntent, true)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.build()
}
}
The notification channel is unique, the app has notification permission and also, full intent permission in the manifest. Any help is highly appreciated.
Plus there is already one service notification, does that impact this in any way?
was not pushing notifications into the system to display! Just need to do this!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(notificationChannelId, "location_notif_chan", NotificationManager.IMPORTANCE_MAX)
val manager = context.getSystemService(LifecycleService.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(notificationChannel)
}
val dmIntent = Intent(context, DeliveryManActivity::class.java)
dmIntent.putExtra("RESTART_TRIGGERED", true)
val dmPendingIntent = PendingIntent.getActivity(
context, 0, dmIntent, PendingIntent.FLAG_UPDATE_CURRENT
)
// Prepare a notification with vibration, sound and lights
val builder = NotificationCompat.Builder(context, notificationChannelId)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_truck_red)
.setContentTitle(context.getString(R.string.restarter_title))
.setContentText(context.getString(R.string.restarter_message))
.setLights(Color.RED, 1000, 1000)
.setVibrate(longArrayOf(0, 400, 250, 400))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(dmPendingIntent)
Pushy.setNotificationChannel(builder, context)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(7, builder.build()) // same notification id to override
I'm creating a chat app that sends and receives notifications through Firebase Cloud Functions. Let's suppose I have 2 friends, and each friend sends me 5 messages(5 notifications). When I open a chat in my chat list, I want all notifications for that chat to be dismissed and the others not.
Currently I can only dimiss 1 notification. If I get more than one notification the notificationManager.cancel(notificationId) doesn't work. I don't want to use notificationManager.cancelAll() as I just want to dismiss some notifications.
override fun onMessageReceived(remoteMessage: RemoteMessage) {
remoteMessage.notification.let { notification ->
val type = remoteMessage.data[NOTIFICATION_TYPE]
val notificationChannelId = getNotificationChannelId(type)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val intent = Intent(this, HomeActivity::class.java)
val notificationIntent = setIntent(remoteMessage.data)
val stackBuilder: TaskStackBuilder = TaskStackBuilder.create(this)
stackBuilder.addParentStack(HomeActivity::class.java)
stackBuilder.addNextIntent(intent)
stackBuilder.addNextIntent(notificationIntent)
val pendingIntent: PendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationBuilder = NotificationCompat.Builder(this, notificationChannelId)
.setContentTitle(notification?.title ?: "")
.setContentText(notification?.body ?: "")
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(notification?.body ?: "")
)
.setSmallIcon(R.drawable.ic_stat)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(soundUri)
.setColor(ContextCompat.getColor(applicationContext, R.color.colorNotification))
.setVibrate(vibrationPattern)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
notify(remoteMessage.data, notificationManager, notificationBuilder)
}
}
private fun notify(
data: Map<String, String>,
notificationManager: NotificationManager,
notificationBuilder: NotificationCompat.Builder
) {
when (data[NOTIFICATION_TYPE]) {
CHAT_MSG -> {
if (data[NotificationUtils.CHAT_ID] != ChatActivity.currentChat) {
notificationManager.notify(
CHAT_MSG_NOTIFICATION_ID,
notificationBuilder.build()
)
}
}
FRIEND_REQUEST -> {
notificationManager.notify(
FRIEND_REQUEST_NOTIFICATION_ID,
notificationBuilder.build()
)
}
}
}
OnStart():
fun dismissNotifications(notificationId: Int) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(notificationId)
}
My thoughts is:
Save all notificationIds in map where:
key is userName/userId in message.
value is List of notificationId
When you need to clear all notifications you just ask you map and clear all that notificationIds for that userName/userId
I'm trying to create the incoming call push notification. When a call event occurs the foreground service with notification starts. I create a channel for it and notification. Here is the code:
The channel settings:
private fun createCallChannelChannel(): NotificationChannel {
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
val importance = NotificationManager.IMPORTANCE_HIGH
return NotificationChannel(CALL_CHANNEL_ID, CALL_CHANNEL_NAME, importance).apply {
description = CALL_CHANNEL_DESCRIPTION
setSound(
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE),
attributes
)
enableLights(true)
enableVibration(true)
}
}
The notification settings:
private fun buildIncomingCallNotification(payload: VoIpCallResponse): Notification {
val remoteView = RemoteViews(packageName, R.layout.notification_call_view)
remoteView.setOnClickPendingIntent(R.id.declineBtn, getDeclinePendingIntent())
remoteView.setOnClickPendingIntent(R.id.acceptBtn, getAcceptPendingIntent(payload))
return NotificationCompat.Builder(this, CALL_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteView)
.setAutoCancel(false)
.build()
}
It works. The notification is being shown. But the problem is notification minimizes to the notifications bar after a few seconds. The goal is to prevent the notification minimization until user decline/ accept the call or the end call event occurs. For example WhatsApp. The incoming call notification stays at the top of the screen for an infinite time. How can I make the same? The importance of my channel is NotificationManager.IMPORTANCE_HIGH and the notification priority is NotificationCompat.PRIORITY_MAX
I have found this page and it has worked
https://developer.android.com/training/notify-user/time-sensitive
val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationBuilder =
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
// Use a full-screen intent only for the highest-priority alerts where you
// have an associated activity that you would like to launch after the user
// interacts with the notification. Also, if your app targets Android 10
// or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
// order for the platform to invoke this notification.
.setFullScreenIntent(fullScreenPendingIntent, true)
and of course use with foregroundService
val incomingCallNotification = notificationBuilder.build()
I Have problem with my notification.
First way - I open my app, then I push notification from FCM first time, I click the notif, my code executed, I push again for second time, I click my notif, my code still execute, I try more more time is worked well.
Second way - My app still close, I push notif from FCM first time, then I Click the notif, so my app will open and my code executed, then I push notif again for second time, my code NOT executed, I try more more time still NOT working.
until I swipe my app, and try again, the problem still happen.
this is my code
class MyFirebaseMessagingService : FirebaseMessagingService() {
var userInformation = AccountTableDao()
val sp: SharedPreferences by lazy { SharedPreference.instance }
val gad: GetAccountData by lazy { AccountDatabase.instance }
override fun onNewToken(token: String) {
super.onNewToken(token)
Log.d("firebase", "INSTANCE ID ===> $token")
}
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
remoteMessage?.let {
sendNotification(it.data)
}
}
private fun sendNotification(messageData: Map<String, String>) {
var status = messageData.get("status")
val uuid = messageData.get("uuid")
val restaurantId = messageData.get("restaurantId")
val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val titleText = messageData.get("title").toString()
val contentText = messageData.get("body").toString()
val intent = Intent(applicationContext, RestaurantMenuActivity::class.java)
intent.putExtra(Argument.NOTIF_UUID, uuid)
intent.putExtra(Argument.NOTIF_RESTAURANT_ID, restaurantId)
intent.putExtra(Argument.NOTIF_STATUS, status)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(applicationContext,System.currentTimeMillis().toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val notificationChannel = NotificationChannel("appety", "appety_notification", NotificationManager.IMPORTANCE_HIGH)
notificationManager .createNotificationChannel(notificationChannel)
notificationManager.notify(System.currentTimeMillis().toInt(), NotificationCompat.Builder(this, "1")
.setChannelId(System.currentTimeMillis().toString())
.setContentTitle(titleText)
.setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
.setContentText(contentText)
.setSmallIcon(R.drawable.ic_check_black_24dp)
.setLargeIcon( BitmapFactory.decodeResource(applicationContext.resources, R.drawable.logo_appety))
.setContentIntent(pendingIntent)
.setOngoing(false)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setColor(ContextCompat.getColor(applicationContext, R.color.colorBonAppety))
.build())
}
else{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationManager .notify(System.currentTimeMillis().toInt(), NotificationCompat.Builder(this, System.currentTimeMillis().toString())
.setContentTitle(titleText)
.setStyle (NotificationCompat.BigTextStyle().bigText(contentText))
.setContentText(contentText)
.setSmallIcon(R.drawable.ic_check_black_24dp)
.setLargeIcon( BitmapFactory.decodeResource(applicationContext.resources, R.drawable.logo_appety))
.setContentIntent(pendingIntent)
.setOngoing(false)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setColor(ContextCompat.getColor(applicationContext, R.color.colorBonAppety))
.build())
} else {
notificationManager .notify(System.currentTimeMillis().toInt(), NotificationCompat.Builder(this, "1")
.setContentTitle(titleText)
.setStyle (NotificationCompat.BigTextStyle().bigText(contentText))
.setContentText(contentText)
.setContentIntent(pendingIntent)
.setOngoing(false)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.build())
}
}
}
companion object {
private val TAG = "token"
}
}
and manifest
<service
android:name=".service.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Why Pending Intent not working and how to fix that.
Sorry for my english and regard.
You should try setAutoCancel() method when creating the notification.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder summary = new NotificationCompat.Builder(this);
summary.setAutoCancel(true);
And if you passing an intent to the notification, then use this
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
I have an application that allows the user to interact with notifications. Here is a simple use case: when the user taps on "Action," the app does some processing and updates the notification to show progress and updates it again to show whether the action was successful or not.
Prior to 26, I was able to set the sound/vibration on individual notifications so once the user click on "Action", the transition to the progress state would not make a sound/vibrate (the behavior that I want) but with 26, it seems like those arguments are no longer respected and the sound/vibration settings are only respected on the channel level.
My initial notification is supposed to make a sound/vibrate but if I am updating an existing (i.e. changing to progress state) then it should not make a sound/vibrate. Is there a way to accomplish that on API 26 and above?
Here is the code for setting up the initial state:
private fun sendNotification() {
val builder = NotificationCompat.Builder(this, "channel_id")
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val intent = Intent(this, MyIntentService::class.java)
val pIntent = PendingIntent.getService(this, ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val action = NotificationCompat.Action.Builder(
R.drawable.ic_lock_open_white_24dp,
"Action",
pIntent
).build()
builder.setSmallIcon(R.drawable.ic_home_teal_600_24dp)
.setContentTitle("My Title")
.setContentText("My content text")
.setSound(defaultSoundUri)
.addAction(action)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelName = "My Channel"
val description = "Channel Description"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("channel_id", channelName, importance)
channel.description = description
notificationManager.createNotificationChannel(channel)
}
val manager = NotificationManagerCompat.from(this)
manager.notify(ID, builder.build())
}
And the transition to the progress state (using same id)
private fun updateNotification(notificationId: Int, title: String) {
//This should NOT make sound or vibrate but it does on 26
val builder = NotificationCompat.Builder(this, "channel_id");
builder
.setSmallIcon(R.drawable.ic_home_teal_600_24dp)
.setContentTitle(title)
.setProgress(0, 0, true)
.setContentText("Processing...")
val manager = NotificationManagerCompat.from(this)
manager.notify(notificationId, builder.build())
}
On all API levels, you can disable sound and vibration for notification updates by use setOnlyAlertOnce():
Set this flag if you would only like the sound, vibrate and ticker to be played if the notification is not already showing.
builder.setOnlyAlertOnce(true)
This will ensure that updates to an existing notification won't sound/vibrate.