Clear text message notification programmatically - android

I made an app that works in the place of the stock messages app. It's basically a popup that displays the text message when it comes in.
When a text message comes in, the phone obviously puts a notification in the notification tray. How do I clear this notification once the user has viewed my popup? When he views my popup, the message is marked as "Read" in the inbox, but the notification isn't cleared. How do I do this? Thanks!

In the notification builder use the setAutoCancel() method:
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setAutoCancel(true);

You can include NotificationListenerService in your project which allows you to listen to all the Notifications that are posted on the phone.
After that you can use cancelNotification() method to clear the notifications you want to dismiss.
Note that user needs to explicitly grant your application Notification Access Permission in order for your application to access the notifications.

Related

Push notification received while the app is in the foreground is different from push notification received when the app is in background

I have implemented FCM push notifications in an android app.
While I am logged in to the app. I get the notification in the form that I am expecting as below.
When the app is in background I receive the json response instead as below.
Following is the code I have added in onMessageRecieved()
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))/*Notification with Image*/;
How can I get the notification in the same manner in both cases.
Any help will be appreciated thank you
After setting up FCM in Android application, you can use Firebase console to send notifications. When foregrounded application receives a notification, onMessageReceived method is invoked. You should override this method to handle notification, but the problem is when the application is in the background and receives a notification, notification delivers to the device’s system tray and you can not handle notification with onMessageReceived method. When the user taps on a notification, it opens the app launcher by default. For example consider you want to do a specific task when a notification is received to the user or do something in background without the user realizing or don’t want to show notification dialog to the user, you can’t do these when the application is backgrounded.
Message Types
With FCM you can send two types of messages to clients :
1- Notification message, sometimes thought of a “display message”
2- Data message, which are handled by the client application
According to Google documents a notification message has a 2KB limit and a predefined user-visible keys. Data messages let developers send up to 4KB custom key-value pairs.
Solution:
If you want to handle notification when the application is backgrounded you should send data message and use onMessageReceived method.
Here is the complete article.
That is so because push notifications handles in foreground and background in different ways.
From the documentation
Notification messages delivered when your app is in the background. In
this case, the notification is delivered to the device’s system tray.
A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and
foreground. In this case, the notification is delivered to the
device’s system tray, and the data payload is delivered in the extras
of the intent of your launcher Activity.
Also you have to remember that there are 2 types of firebase pushes - Notification message and Data message.
Looks like you are using Notification messages, so they are working as expected.
Just use data type of messages and so you can always handle them as you need.

Handle Received message firebase notifications

I want to trigger a specific event (like ordinary vibration) when the device gets an notification from the Firebase Notifications.
All I discovered so far is that one can handle the on_click of a notification that was sent with display-messages in the background of the app.
Is it possible to let the device vibrate in the very moment the notification arrives? I would love to get the users attention to participate on my field study by answering the question sheet in the moment, the notification comes in.
Thanks alot!
There are two types of messages:
data messages (with a data property in the JSON)
notification messages (with only a notification property in the JSON)
If a notification/data message arrives while your app is active, you can handle it in onMessageReceived and do whatever you want.
If a data message arrives while your app is inactive, you can handle it in onMessageReceived and do whatever you want.
To make the phone vibrate in these cases, see the excellent example from Wizard.
If a notification message arrives while your app is inactive, it is automatically handled by the system and you can't control what happens.
Also see the documentation on message types as there are some more nuances.
Is it possible to let the device vibrate in the very moment the
notification arrives?
Yes, you can define it while generating Notification using NotificationManager class -
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
....
..
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

Android, access all notifications

In Android, if a user sets "Show Notifications" of a specific App to off, is the notification still created? If yes, is it somehow possible to access these created but not shown notifications?
As an example, consider that to create a Status bar Notification, below code is used:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
And to POST this notification, below code is used:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
But since the "Show Notification" is OFF, above code don't show the Notification on Status bar. So as you can see, although the NotificationCompat.Builder has CREATED a Notification object/instance and filled it with other details such as Title or Text etc. you can NOT access it as it is NOT Allowed to POST by System because of user's preference settings.
Also, Since Android 4.1, users can turn off notifications of an app from application manager, but there is NO API to acheive the same from Application code (on Non-rooted device,that is). And It is not possible to disable notifications from other apps, and you can only control notifications generated by your own app.
Few more pointers that you must have in mind:
1.As a developer we have no way to know whether a call to notify was effective or not. So if I really need to check if the notifications are disabled for the current application there is NO Such setting for that in the API.
2.You really shouldn't concern yourself with it. Just assume your notification was successful. If the user has explicitly disabled your notifications, then he/she probably had good reason to do so, and your application should not care whether the notification was displayed or not

How to know if an notification has been cancelled?

My little app sends some notifications. We get a callback via a Pendingintent when the notification is clicked on. However, when a notification is simply removed without being clicked on, I don't get any kind of notification and thus wouldn't know if a notification has been removed by the user.
My ultimate goal is to limit the number of active notifications sent by my app to no more than 3. But I haven't been able to find a way to enumerate or simply get the count of active notifications sent by my app. The number of methods available in NotificationManager is rather limited.
Any help will be appreciated.
You can set a PendingIntent with setDeleteIntent() which will be called when the notification is removed from the notification tray (such as when the user swipes to dismiss it).
Do note that the notification design guidelines state:
If a notification of a certain type is already pending when your app tries to send a new notification of the same type, combine them into a single summary notification for the app. Do not create a new object.
A summary notification builds a summary description and allows the user to understand how many notifications of a particular kind are pending.
I.e., don't do this:
Do this (this example uses an InboxStyle notification as is recommended):
Make sure you are not posting multiple notifications of the same type.
the method "Notification.deleteIntent" you can use to set a PendingIntent which the notification was removed by system will be called .And then you can do something you want .

how to clear notification from notification tray on click in nokia-x

Objective: Notification should be auto cancelled on click of it and should open my activity(Pending Intent)..
I have a running code which works perfect in Android Devices expect Nokia-X. Here is the code:
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle(MainActivity.this.getString(R.string.app_name))
.setContentText("text").setContentInfo("info").setTicker("Ticker text")
.setContentIntent(contentIntent).setSmallIcon(R.drawable.ic_launcher).setWhen(System.currentTimeMillis())
.setLights(Color.YELLOW, 1, 2).setAutoCancel(true).build();
NotificationManager nm = (NotificationManager)MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, notification);
In case of Nokia-x device, It send notification. It open my activity on click of notification but notification stays in notification tray. It does not get clear on click.
Please help my out!!
It is just like Dr.Jukka said: Notifications cannot be programmatically removed from the Fastlane - only the user can remove content from the Fastlane:
"Currently all notifications are stored in the Fastlane even if the auto cancel flag is used. Do note that if your notification has a command visible from which user can dismiss/remove the notification manually, the notification is not removed from Fastlane. Notifications can only be removed from Fastlane manually by enabling the edit mode."
Furthermore, it does not make sense to have items such as notifications suddenly disappearing from the Fastlane since the purpose of the view is to display the user's/app's past activities.

Categories

Resources