Notifications in app only, not in notification center - android

is it possible to have notifications pop up, make a sound and be shortly visible at the top of the screen but not be logged via the notification center?
My idea is to have the user be notified of any important actions. But while the user is inside the app the notifications should not pool up inside the notification center. When the user is not actively using the app I will start a background service with a websocket and get further notifications for the user to be shown via the notification center.
So it it possible to differentiate between the two types. Similar to what whatsapp is doing?

One way to achieve the desired effect seems to be to just cancel the notification by ID once it has been passed to the notification manager:
https://developer.android.com/reference/android/app/NotificationManager#cancel(int)

Related

How do I remove a notification without removing its bubble while the app is not in foreground?

In my app I'm publishing notifications for chats, and whenever a chat is read by user, I want to remove the notificaton. If the user has opted to open a bubble, however, I want to preserve it until the user manuall dismisses it.
I can easily do this when the user opens the relevant chat in the bubble itself, or in the main activity, by republishing the notification with .setSuppressNotification(true) called on its bubble metadata. The effect is what I want exactly; the notification is gone, the bubble stays, and the badge is cleared on the bubble.
The problem is, the user can also read the chat elsewhere, not on the device. In this case .setSuppressNotification() might not work:
This flag has no effect if the app posting the bubble is not in the foreground. The app is considered foreground if it is visible and on the screen, note that a foreground service does not qualify.
Is there a way to remove a notification without removing its bubble while the app is in background?
P.S. What do the other apps do?
Telegram seems to be preserving the bubble, only removing relevant notifications when you open the bubble itself. The notifications will stay even if you read the chat in main activity and dismiss the bubble. I don't think this is acceptable;
Signal, on the other hand, will kill the bubble whenever you open the relevant chat in the app. This is perhaps reasonable for one-to-one chats, as the bubble will reappear on a new message anyway, but for group chats I would like to preserve the bubble;
People (the sample app) will preserve the bubble while removing the notification. Being not a real messenger, it doesn't have to deal with my specific use case, however.
You can try NotificationListenerService
Request user to enable permission Notification access
Cancel Notification using NotificationListenerService
do not use NotificationService#cancel
I found a Github sample here (not mine): https://github.com/Chagall/notification-listener-service-example
You can follow the sample here and implement some more code to make it happen
I created an issue about this on Google's tracker and they marked it as fixed, saying
We would like to inform you, setSuppressNotification will work regardless of the app being in the foreground and this change has been introduced in Android S.

Cancel notification without using Notification Manager

I have an application in which it receives the notification when fired from Backend. Now what I want to achieve is that the notification should disappear after 2 minutes if user has not clicked on it (even if my app is killed or background). I know that this can be achieved by using the Notification manager's setTimeoutAfter() but that will work only if am making my custom notification using Notifcation Manger.But i want to dismiss the Notification generated by System after 2 minutes.
Any kind of suggestion or help will be welcomed.
It is not possible to do that. The default system notification builder is pretty bare bones and will not handle it. The best you can do is set a delivery timeout so if the device was off it won't get the notification once it expires.
See https://firebase.google.com/docs/cloud-messaging/concept-options#when_to_use_platform_specific_keys (only works on Android and Web)

Right way to capture push notifications when the application is open in Android

This is the first time I am integrating notifications into my application. I am using Firebase. Setup was extremely simple and I am able to view the notification in the tray.
So, when the application is open, and if it receives a notification. I would like to display the notification in the activity itself.
How should I go about this?
You can look at Gmail approach. If there is new mail in current thread, they show SnackBar with notification.
You need to determinate connected parts of your app. And if notification connected to part where current user is - show SnackBar, and if there is something completely different - show heads-up notification.
Guide how to do Heads-Up notifications here
Guide how to do SnackBar notifications here
Have you ever tried Pushbots , its an infra- Structure for Notifications , it has more interesting Features than Firebase. Give it a shot.
Pushbots link

In Android how do I get the currently displayed notification for a given notification id?

My intention is to display a notification with a personalised icon. However, if a notification with the same id is already displayed and then I need to add a new notification with the same id, my program will instead prepare a "stacked" version of the notification where the icon is changed to some generic icon and the notification content shows excerpts from the last and current notifications. Similar to how Gmail does when there are multiple emails.
To implement that I need to check if there are notifications of my app, currently displayed. I do not see any API to retrieve my own notifications.
I cannot simply cache the notification details that I have displayed till now, since in that case I need to know when they will be dismissed by the user, and update my cache accordingly. I also do not see any API to listen for dismiss events.
If you observe the gmail app notification behaviour properly u will notice that even when u dismiss one notification gmail shows you the same notification again in the list when a new notification comes. It looks to me that gmail is relying on total unread/unopened messages rather than keeping a cache of notifications.
There is no direct api for ur suggested SDK version. You need to fallback to ur own implementation. However, there is a way to know if a already showed notification was dismissed - How to know when my notification is cleared via Clear button?
This is possible with android 4.3 upwards now
See http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

How to receive Parse push notification only when app is opened, and show it in modal dialog form

I was using https://github.com/ParsePlatform/PushTutorial sample code for a while.
Current example behavior is
Push notification will be received, either the app is opened or closed.
Push notification is in the form of showing icon in status bar with sound.
I cannot find a way to specific notification icon image. Parse is using app icon as notification icon by default. By Android design guideline requires us to have different style and size for notification icon.
When the app is opened, and notification received. Clicking on the notification icon will cause 2nd instance of the same app is launched. That's mean, there will be 2 instances of same app. (Personally, I don't feel this is a correct behavior)
I was wondering
How can I only receive the push notification when app is opened, but not closed?
How can I show it in the form of modal dialog, instance of showing icon in status bar with sound?
I am not sure about the 4th point, it didn't work that way for me. It opened the same instance for me. I think you are using a different application identifier and have two different application with same name and icon, but different identifier.
Now, for your use case, I think Push notification is not the ideal solution. Depending on what and how frequent you need to show this, you may opt for a repeating pull from the server or if you still want to use Push Notification then subscribe/unsubscribe from a Push Notification Channel when the app is pulled to foreground or background.
ie, when the application is in foreground (onStart() / onResume()), subscribe to a channel:
PushService.subscribe(context, "foregroundPush", YourActivity.class);
and when the application is moving to background (onStop() / onResume() / onDestroy() ), unsubscribe from the same channel:
PushService.unsubscribe(context, "foregroundPush", YourActivity.class);
Whenever you need to send a push notification to the devices with your application in foreground, use the channel 'foregroundPush'

Categories

Resources