I am creating an app that will have permission to read user notifications.
So I have implemented NotificationListenerService as shown below
class NotifListener : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification?) {
var context = this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
var packageName = sbn?.packageName ?: ""
var notifId=sbn?.id?:0; //doesn't seem to be unique
val notification: Notification = sbn!!.notification
var extras: Bundle? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
extras = notification.extras
for (key in extras!!.keySet()) {
var title: String = extras.get("android.title").toString()
var text: String = extras.get("android.text").toString()
CoroutineScope(Dispatchers.IO).launch {
NotificationDBBuilder.getInstance(context).notificationDao()
.insertNotification(
NotificationEntity(notifId, title, text,packageName,Date().time)
)
}
}
}
extras?.get("android.title")
// Log.d(TAG, "onNotificationPosted: " + extras?.get("android.title")+" "+extras?.get("android.text")+" "+extras?.get("android.infoText"))
}
}
}
and I have added it in manifest
<service android:name=".services.NotifListener"
android:label="Notif"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Now when I logged the notifications that I received, it seems that the onNotificaitionPosted is being called multiple times for the same notification for some applications or some situations. I want to list only Unique notifications. Which parameter should I Use from the notifications as a Unique Key?
Or is there some way to prevent multiple calls for the same notification?
or is it possible that the listener is getting registered multiple times?
Related
Hi i´m working in app that have to notificate when the user starts driving, i used Neura Api but it needs a fixed notification, so i´m trying it with Awareness Api.
I need the broadcast in the AndroidManifest.xml because i want to trigger the event even the app is not in background.
The Fence is registered fine, the broadcast is triggered but i can´t get the fenceKey and fenceStatus , i´m trying with different events for test.
In the AndroidManifest.xml i added permissions, api key and declared the broadcast.
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<receiver
android:name=".usescase.receivers.FenceReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.safycab.safycab.FENCE_RECEIVER_ACTION" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.awareness.API_KEY"
android:value="#string/awareness_key" />
This is my FenceReceiver.kt, here is the problem when i got the event of headphones fence i try to get the fenceKey and fenceStatus but i got fenceKey = null and fenceStatus = 0
class FenceReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val fenceState = FenceState.extract(intent)
context.showLocalNotification("fence key " + fenceState.fenceKey + " fence state" + fenceState.currentState)
}
}
Here is where i register the Fences, here all permissions are checked and accepted, the register is working good
class FenceApiUtils(var activity: BaseActivity<*, *>) {
var drivingFence = DetectedActivityFence.starting(DetectedActivityFence.IN_VEHICLE)
var walkingFence = DetectedActivityFence.starting(DetectedActivityFence.ON_FOOT)
val headPhoneFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN)
fun createFences() {
val intent = Intent(activity, FenceReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(
activity.applicationContext, 0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
Awareness.getFenceClient(activity).updateFences(
FenceUpdateRequest.Builder()
.addFence(VEHICLE_FENCE_KEY, drivingFence, pendingIntent)
.addFence(WALKING_FENCE_KEY, walkingFence, pendingIntent)
.addFence(HEADPHONE_FENCE, headPhoneFence, pendingIntent)
.build()
).addOnSuccessListener {
log("Fence was successfully registered.")
}.addOnFailureListener {
log("Fence could not be registered: ${it.message}")
}
}
}
If i do this method i can check that the fence is correctly registered
fun queryFence(key: String) {
Awareness.getFenceClient(requireActivity())
.queryFences(FenceQueryRequest.forFences(listOf(key))).addOnSuccessListener {
val map = it.fenceStateMap
for (fenceKey in map.fenceKeys) {
val fenceState = map.getFenceState(fenceKey)
requireContext().showLocalNotification(
"Fence " + fenceKey + ": "
+ fenceState?.currentState
+ ", was="
+ fenceState?.previousState
)
}
}.addOnFailureListener {
log(it.message)
}
}
And if i do this i got the user activity correctly
Awareness.getSnapshotClient(requireActivity()).detectedActivity.addOnSuccessListener {
val act = it.activityRecognitionResult
val dtc = act.mostProbableActivity
val conf = dtc.confidence
val activityStr = dtc.toString()
requireContext().showLocalNotification("Activity: $activityStr, Confidence: $conf/100")
}.addOnFailureListener {
log(it.message)
log(it.localizedMessage)
}
Change the pending intent flag from FLAG_IMMUTABLE to FLAG_MUTABLE:
val pendingIntent = PendingIntent.getBroadcast(
activity.applicationContext, 0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
Unfortunately the documentation doesn't show how to create the mPendingIntent in https://developers.google.com/awareness/android-api/fence-register, but for me this did the trick.
I am using FirebaseMessagingService to send push notifications to my Pocophone X3 NFC. However, I will send a notification token and erase the notification token from my Firestore database first when logging in and logging out happens. This is to ensure that the notification is sent to the correct user.
To do the above, I have the following code.
#AndroidEntryPoint
class MessagingService :
FirebaseMessagingService(),
FirebaseAuth.AuthStateListener {
#Inject
lateinit var notificationRepository: NotificationRepository
override fun onCreate() {
super.onCreate()
FirebaseAuth.getInstance().addAuthStateListener(this)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
val title = remoteMessage.notification?.title
val message = remoteMessage.notification?.body
if (message != null && title != null) {
sendPushNotification(title, message)
}
}
override fun onNewToken(token: String) {
saveTokenToSharedPreferences(token)
authRepository.getUser()?.let {
val uid = it.uid
sendTokenToFirestore(uid, token)
}
}
override fun onAuthStateChanged(auth: FirebaseAuth) {
auth.currentUser?.let {
val uid = it.uid
val savedRegistrationToken =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(getString(R.string.fcm_token_shared_pref_key), "")
savedRegistrationToken?.let { token -> sendTokenToFirestore(uid, token) }
}
}
private fun sendPushNotification(title: String, messageBody: String) {
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT)
val channelId = getString(R.string.general_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.mlearn_logo)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(
channelId,
getString(R.string.general_notification_channel_name),
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build())
}
private fun saveTokenToSharedPreferences(token: String) {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this)
with (sharedPref.edit()) {
putString(getString(R.string.fcm_token_shared_pref_key), token)
apply()
}
}
private fun sendTokenToFirestore(uid: String, token: String) {
GlobalScope.launch(Dispatchers.IO) {
notificationRepository.sendNotificationToken(uid, token)
}
}
companion object {
const val REQUEST_CODE = 0
const val NOTIFICATION_ID = 0
}
}
I also registered the service to the manifest like so.
<service
android:name=".service.MessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
The code is working fine when I install the app for the first time and if I don't do any device restarts or app killing. However, when I either
turn off my phone and turn it back on; or
kill the app from my recent apps list
the FirebaseMessagingService code will no longer run. As a result, when I do log in using a new account, the new account does not have the notification token associated with it because the service isn't running.
How do I make FirebaseMessagingService keep running even on app kills and phone turning off and turning on (i.e. restarts)? Note that the app installed is not signed yet.
add this to your service
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
also, it takes some time to receive FCM messages after restart, that doesnt mean your service isnt running. If you start receiving messages after like 5 mins of restart that means your service is working fine, it just takes some time for new tokens to register
if all else fails, check out this project's implementation of FCM, its solid, restarts automatically and saves tokens to firebase
https://github.com/yahyakhan234/PharmaGO2-master
I am making a library that can be used to incorporate breaking and entering detection into any application
There is an arduino set to the alarm of the house which sends an SMS to a specific phone upon trigger
Within my sdk I register an sms receiver which upon receiving an sms with a specific text, should show a full screen activity (on top of the lockscreen too) that will alert the user
I created an application to test this behaviour
the application's package is : com.example.demo
the library's package is : com.example.sdk
the sms receiver looks like this:
class SMSReceiver : BroadcastReceiver() {
companion object {
const val TAG = "SMSReceiver"
}
private val logger by lazy { Injections.logger }
override fun onReceive(context: Context?, intent: Intent?) {
logger.log(TAG) { "Got sms" }
val ctx = context ?: return
val bundle = intent?.extras ?: return
val format = bundle.getString("format") ?: return
val pdus = (bundle["pdus"] as? Array<*>) ?: return
for (idx in pdus.indices) {
val pdu = pdus[idx] as? ByteArray ?: continue
val msg = SmsMessage.createFromPdu(pdu, format)
if (msg.messageBody.startsWith("theft event", true)) {
logger.log(TAG) { "Got theft event" }
abortBroadcast()
showTheftActivity(ctx, msg.messageBody)
break
}
}
}
private fun showTheftActivity(context: Context, messageBody: String) {
val intent = Intent(context, TheftActivity::class.java)
intent.addFlags(Intent.FLAG_FROM_BACKGROUND)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
// .addCategory(Intent.CATEGORY_LAUNCHER)
val location = messageBody.split(" ").getOrNull(2)
if (location != null) {
val coords = location.split(",")
if (coords.size == 2) {
val x = coords[0].toBigDecimalOrNull()
val y = coords[1].toBigDecimalOrNull()
if (x != null && y != null) {
intent.putExtra(TheftActivity.X, x.toString())
intent.putExtra(TheftActivity.Y, y.toString())
}
}
}
context.startActivity(intent)
}
}
the activity that should show on top of everything is this :
class TheftActivity : Activity() {
companion object {
const val X = "locationX"
const val Y = "locationY"
}
private val x: String? by lazy { intent.getStringExtra(X) }
private val y: String? by lazy { intent.getStringExtra(Y) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_theft)
val location = findViewById<Button>(R.id.locate)
if (x != null && y != null) {
location.setOnClickListener { Toast.makeText(applicationContext, "Going to $x , $y", Toast.LENGTH_SHORT).show() }
location.isEnabled = true
finish()
} else {
location.isEnabled = false
}
findViewById<Button>(R.id.cancel).setOnClickListener {
finish()
}
turnScreenOnAndKeyguardOff()
}
private fun turnScreenOnAndKeyguardOff() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true)
setTurnScreenOn(true)
} else {
window.addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
)
}
with(getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requestDismissKeyguard(this#TheftActivity, null)
}
}
}
override fun onDestroy() {
super.onDestroy()
turnScreenOffAndKeyguardOn()
}
private fun turnScreenOffAndKeyguardOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(false)
setTurnScreenOn(false)
} else {
window.clearFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
)
}
}
}
and the sdk's android manifest contains this:
<application>
<activity
android:name=".ui.TheftActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:excludeFromRecents="true"
android:label="#string/title_activity_theft"
android:showOnLockScreen="true"
android:theme="#style/Theme.Sdk.Fullscreen" />
<receiver
android:name=".receivers.SMSReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
when testing this on an emulator I send the sms to trigger the theft event
if the activity for testing (the one in the com.example.demo package) is not closed then it is brought to the front , but if it is closed nothing happens (though I do see the log messages from the receiver)
how can I make my sms receiver open the TheftActivity instead of the main activity from the main package?
edit: if it helps, the theft activity seems to start and then get immediately destroyed
It looks like the system can't bring the activity to the foreground due to the restrictions implemented in Android Q
With Android Q, it is impossible to start an activity from the background automatically if your app does not include those exceptions listed in the link below.
https://developer.android.com/guide/components/activities/background-starts
For possible solutions :
https://stackoverflow.com/a/59421118/11982611
I have successfully implemented SMS retriever in my application using the guideline here . My Code is working fine and otp is auto populated in many devices but some of the devices like vivo v15 pro, redmi note 4 it is not working (BroadcastReceiver's onReceive() not get triggered) . i have attached my code here. check and let me know if you have any solution for this. Thanks
Manifest.xml
<receiver
android:name=".sms.SMSRetrieverBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</receiver>
SMSRetrieverBroadcastReceiver.kt
class SMSRetrieverBroadcastReceiver : BroadcastReceiver() {
companion object{
private var otpReceiver: OtpReceiver? = null
fun initOTPListener(receiver: OtpReceiver) {
this.otpReceiver = receiver
}
}
override fun onReceive(context: Context, intent: Intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
val extras = intent.extras
val status = extras!!.get(SmsRetriever.EXTRA_STATUS) as Status
when (status.statusCode) {
CommonStatusCodes.SUCCESS -> {
// Get SMS message contents
var otp: String = extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as String
Timber.d("OTP_Message "+otp)
if(otpReceiver != null) {
otpReceiver!!.onOTPReceived(otp)
}
}
CommonStatusCodes.TIMEOUT -> {
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
if(otpReceiver != null) {
otpReceiver!!.onOTPTimeOut()
}
}
}
}}
MainActivity.kt
fun startListeningForSMS() {
SMSRetrieverBroadcastReceiver.initOTPListener(this)
startSmsListener()
}
private fun startSmsListener() {
val client = SmsRetriever.getClient(mContext)
val task = client.startSmsRetriever()
task.addOnSuccessListener {
Timber.d("Success")
}
task.addOnFailureListener {
Timber.d("Failed")
}
}
override fun onOTPReceived(otp: String) {
setReceivedOtp(otp)
}
override fun onOTPTimeOut() {
setReceivedOtp(null)
}
Go to Messeges-click on (:) on top left ->Go to setting->Disable prevention from Otp verification.It worked fine in my case.
Can you check with this intent-filter action:
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
Edit : I forgot to tell, i already try to make a breakpoint on the onRefreshToken and make a Log in that function, but when i start the apps, none of them is running (the breakpoint doesn't start, and the Log doesnt showing in AS's console log). So IMO its pure that the service itself doesn't start.
I make a code where my apps gonna show a notification if there is a data incoming in onMessageReceived. Its can run well, no problem at all, until i try it in Android Jelly Bean.
The notification doesn't showing (i am sure the notification is sent, because the other non JellyBean device is show the notification at the same time.) and then i trying it in KitKat Device, the result is same.
I then try to debug it and no error showing in Android Studio ( I bet because the device got Chinese rom and doesn't have Google play service built in).
Later i change my method and try it in Emulator, and the E/FirebaseInstanceId: Token retrieval failed: PHONE_REGISTRATION_ERROR is showing. I already try it in emulator that have KitKat and JellyBean image installed, the result is same, the same message appear.
This is my code:
AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.appname">
///Some activity and meta data here
<service
android:name=".fcm.MyFirebaseInstanceIdService"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".fcm.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</manifest>
MyFirebaseInstanceIdService
class MyFirebaseInstanceIdService : FirebaseInstanceIdService() {
val TAG = "PushNotifService"
lateinit var name: String
override fun onTokenRefresh() {
val token = FirebaseInstanceId.getInstance().token
}
}
MyFirebaseMessagingService
class MyFirebaseMessagingService : FirebaseMessagingService() {
private val session = SessionManagement(this)
#SuppressLint("LongLogTag")
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.data != null){
val data = remoteMessage.data
val title = data["title"]
val body = data["body"]
showNotification(title, body)
}
}
#SuppressLint("LongLogTag")
override fun onNewToken(token: String?) {
session.updateFCMToken(token)
}
fun subscribeTopic(topic: String?){
//the topic in here is a param send by other activity when the apps lunch
FirebaseMessaging.getInstance().subscribeToTopic(topic).addOnCompleteListener { task ->
if (!task.isSuccessful) {
} else {
}
}
}
//This sendMessageTrainer() is called and run from other activity
fun sendMessageTrainer(){
val contentType = "application/json"
val authorizationKey = ServerHelper.FCMServerKey
val data = "{\"to\": \"/topics/sometopic\",\"data\": {\"title\":\"Request Update\",\"body\":\"New Request.\"}}"
Fuel.post(ServerHelper.FCMServer).header("Content-Type" to contentType, "Authorization" to "key=$authorizationKey").body(data).responseJson{
_, _, result ->
result.failure {
sendMessageTrainer()
}
result.success {
}
}
}
//showNotification() is run if there is a new data/notification from onMessageReceived
private fun showNotification(title: String?, body: String?) {
val intent = Intent(this, LauncherActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val channelName = getString(R.string.app_name)
val channelID = "default"
val alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notifyID = 1
val notification = NotificationCompat.Builder(this, channelID)
.setSmallIcon(R.drawable.ic_logo)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(alarmSound)
.setContentIntent(pendingIntent)
.build()
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(channelID, channelName, importance)
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.createNotificationChannel(mChannel)
mNotificationManager.notify(notifyID , notification)
}
Build.VERSION.SDK_INT <= Build.VERSION_CODES.N -> {
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.notify(notifyID, notification)
}
}
}
}
Thanks and regards,
vl14b