Notifications not showing android API 29 - android

Ok, guys, idk why but notifications is not showing, on phone with API 29. On Api 21 for example everything is ok. Yes, I tried to read other posts on SO and found there nothing usefull, tried all advices from those posts too.
Min sdk 24, max sdk - 29 in gradle
receiver in manifest:
<receiver android:name=".ui.more_content.receivers.RemindersReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
Receiver class:
class RemindersReceiver: BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
// Идентификатор уведомления
if(p1?.action.equals("android.intent.action.PACKAGE_ADDED")||p1?.action.equals("android.intent.action.PACKAGE_REMOVED")
||p1?.action.equals("android.intent.action.PACKAGE_UPDATED")) {
showNotificationWith("qwqwdqwqs", p0)
}
}
private fun showNotificationWith(message: String, context: Context?) {
val channelId = "com.example.notif.channelId"
val channelName = "App status"
val contentTitle = "Title"
val notificationManager =
context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val activityIntent = Intent()
val contentIntent: PendingIntent = PendingIntent.getActivity(
context,
0, activityIntent, PendingIntent.FLAG_CANCEL_CURRENT
)
val largeIcon: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.info)
val notificationBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.info)
.setContentTitle(contentTitle)
.setContentText("asdasdasdasdas")
.setStyle(
NotificationCompat.BigTextStyle()
.bigText(context.getString(R.string.long_dummy_text))
.setBigContentTitle("Big content title")
.setSummaryText("Summary is this Text")
)
.setLargeIcon(largeIcon)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(Color.RED)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(channelId, channelName, importance)
notificationBuilder.setChannelId(channelName)
notificationManager.createNotificationChannel(notificationChannel)
notificationManager.notify(
message.hashCode(), notificationBuilder
.setContentIntent(contentIntent).setAutoCancel(true).build()
)
} else {
notificationManager
.notify(
message.hashCode(), notificationBuilder
.setContentIntent(contentIntent).setAutoCancel(true).build()
)
}
}
}
Appreciate any help, thx in a forward

Those broadcasts have not been delivered to manifest-registered receivers since Android 8.0.

Related

Broadcast receiver being called fine, but not showing notification

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

FCM Notification are not showing in foreground in android

Notifications are showing in the background but when the app is in the foreground notifications are not showing. I applied many solutions but they do not work for me. Can anyone tell me where is my mistake? thanks in advance
Here is Manifest
<service
android:exported="false"
android:name=".services.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/cute" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/design_default_color_on_primary" />
Here is My ServicesClass
const val cannelId = "notification_channel"
const val channel_name = "com.dextrologix.dham.rfms.resident.services"
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.notification != null) {
genrateNotification(
remoteMessage.notification!!.title!!,
remoteMessage.notification!!.body!!
)
}
}
#SuppressLint("RemoteViewLayout")
fun getRemoteView(title: String, message: String): RemoteViews {
val remteViews = RemoteViews(
"com.dextrologix.dham.rfms.resident.services",
R.layout.pushnotification_layout
)
remteViews.setTextViewText(R.id.notification_title, title)
remteViews.setTextViewText(R.id.notification_message, message)
remteViews.setImageViewResource(R.id.notification_image, R.drawable.cute)
return remteViews
}
fun genrateNotification(title: String, message: String) {
var intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
var builder: NotificationCompat.Builder =
NotificationCompat.Builder(applicationContext, cannelId)
.setSmallIcon(R.drawable.person_icon)
.setAutoCancel(true)
.setVibrate(longArrayOf(1000, 1000, 1000, 1000))
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent)
builder = builder.setContent(getRemoteView(title, message))
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel =
NotificationChannel(cannelId, channel_name, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(notificationChannel)
}
notificationManager.notify(0, builder.build())
}
}
It seems like you are missing notification channel. They are required for android notifications.
docs link

Have is the best way for redirection when user click notification by firebase?

fcm/send
{
"registration_ids": ["futj4oisZk5rvmU3mlX9Sq:APA91bGECOqPhLS95XOraN4NOhAuT9WAZ18vXM5dYtf8nSY-EUkgomTghFVvAaqL46q_-T6NMIMXQzegRlo3majiJDyntRS0qWmzihP-vZJ4RdJqBPqIeDueI3NxxxhUKItGJymVbjazv"],
"notification": {
"body": "VI VỆ SINH",
"title": "Thanh Toán",
"badge": "1",
"idata": "hello",
"sound": "sound.mp3",
"android_channel_id": "Skycic",
"click_action": "NotificationNavActivity"
},
"data": {
"solution": "dms",
"menu": "dealer",
"id": "ODLA8300002"
}
}
In my way, I have activity for intent-filter and FirebaseMessagingService
<activity android:name=".main.activity.NotificationNavActivity">
<intent-filter>
<action android:name="NotificationNavActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
private fun sendNotification(messageBody: RemoteMessage.Notification, solution: String, menu: String, id: String) {
val intent = Intent(this, NotificationNavActivity::class.java)
intent.putExtra("solution", solution)
intent.putExtra("menu", menu)
intent.putExtra("id", id)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val channelId = resources.getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.icon_app)
.setStyle(NotificationCompat.BigTextStyle().bigText(messageBody.body))
.setContentTitle(messageBody.title)
.setContentText(messageBody.body?.trim())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
val channel = NotificationChannel(channelId,
resources.getString(R.string.default_notification_channel_id),
NotificationManager.IMPORTANCE_DEFAULT)
channel.setSound(soundUri, audioAttributes)
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(1000, 1000, 1000, 1000, 1000)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
playNotificationSound()
}
NotificationNavActivity
class NotificationNavActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notification_nav)
val solution = intent?.getStringExtra("solution") ?: ""
val menu = intent?.getStringExtra("menu") ?: ""
val id = intent?.getStringExtra("id") ?: ""
val notificationData = NotificationFBData(
solution = solution,
menu = menu,
id = id
)
val intent = Intent(this, MainActivity::class.java)
intent.putExtra(Constants.NOTIFICATION_FB, notificationData)
startActivity(intent)
finish()
}
}
Well, In my NotificationNavActivity, I will redirect to screens I want but I worry when the app in the foreground it will have many instances of NotificationNavActivity and others after each click notification.
Is there any other better way?

Android FCM Notification Cannot Be Clicked Second Time

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

How to create a time triggered Notification that works on all Android Devices

I'm trying to make a notification that get triggered after a certain amount of time has passed. In an attempt to achieve this I have this AlarmReceiver class that inherits from BroadCast receiver and works on devices running up to about API 23. It doesn't work on my emulator currently running API 27. Any clue what I'm doing wrong?
class AlarmReceiver : BroadcastReceiver() {
companion object {
val PRIMARY_CHANNEL = "dj"
}
override fun onReceive(context: Context, intent: Intent) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationIntent = Intent(context, NotificationActivity::class.java)
val stackBuilder = TaskStackBuilder.create(context)
stackBuilder.addParentStack(NotificationActivity::class.java)
stackBuilder.addNextIntent(notificationIntent)
val pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT)
if (android.os.Build.VERSION_CODES.O <= android.os.Build.VERSION.SDK_INT){
//I create the notification channel on the next line, but it doesn't seem to work
val notificationChannel = NotificationChannel(PRIMARY_CHANNEL,
"DailyJokes", NotificationManager.IMPORTANCE_DEFAULT)
notificationChannel.lightColor = Color.GREEN
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
notificationManager.createNotificationChannel(notificationChannel)
val notification = Notification.Builder(context, PRIMARY_CHANNEL)
.setContentIntent(pendingIntent)
.setChannelId("dj")
.setContentText("KDW")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("CCC")
.build()
notificationManager.notify(1,notification)
} else {
var builder = NotificationCompat.Builder(context, "dj")
val sound = Uri.parse("android.resource://" + context.packageName + "/" + "raw/drumroll")
builder = builder
.setSmallIcon(R.mipmap.ic_launcher_round)
.setColor(Color.BLUE)
.setContentTitle("Content Title")
.setTicker("TICKER Text")
.setContentText("KDW setContentText")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setChannelId("dj")
.setSound(sound)
notificationManager.notify(1, builder!!.build())
}
}
}
Here's the code that triggers the notification, anything off here?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notification)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val notificationIntent = Intent("android.media.action.DISPLAY_NOTIFICATION")
notificationIntent.addCategory("android.intent.category.DEFAULT")
val broadcast = getBroadcast(this, 100, notificationIntent, FLAG_UPDATE_CURRENT)
val cal = Calendar.getInstance()
cal.add(Calendar.SECOND, 5)
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.timeInMillis, broadcast)
}
}
I also have a receiver in my Manifest:
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Even with all this the bug still lurks. I'm new to Kotlin, today makes a week since I started. My swift experience prepared me well but I just can't seem to get a hold on notifications :/
From what I can tell from your code , you need to register the receiver in either the Manifest or a context.
https://developer.android.com/guide/components/broadcasts.html
is your channel even created ?
your comparison is in the wrong direction ->
android.os.Build.VERSION_CODES.O <= android.os.Build.VERSION.SDK_INT it should be >= instead
and only the channel creation needs to be guarded against O, not the notification creation.

Categories

Resources