I got a problem with Android Notification while developing my Messaging App.
My App need to clear notification on Status Bar, so I call Notification#cancel(id)
But this function also close the Chat Bubble relate to this Notification.
I tried hundreds way to clear Notification on Status Bar & keep Bubbles on Screen but can't
Any one here face this problem and has the solution yet?
Instead of cancelling the notification, you can republish it while calling setSuppressNotification(true) on it's bubble metadata & setOnlyAlertOnce(true) on its builder. It will keep the bubble and remove the notification along with the bubble badge and launcher shortcut badges, if any.
Beware that for this to work, your app must be visible on the screen.
Related
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.
Actually the thing I want to achieve is, normally the shortcut icon badge count in android got cleared once the push notification popup is acknowledged by an user from notification panel, but instead clearing the count as above it has to be cleared only after the corresponding notification is acknowledged by an user within the app(for eg: if an user received a text message then the badge count will increased to one and once the user cleared the push notification popup form notification panel then the badge will also got cleared, but instead of that badge should be cleared only after the user viewed the message).
I tried the following packages.
https://www.npmjs.com/package/react-native-app-badge
https://www.npmjs.com/package/react-native-android-badge
Looks like you can use ShortcutBadge.setCount(0); to clear the badge counter!
I dont know what you use to display a notification on the android device itself, probably react-native-push-notification.
If that's the case, they also have a function for it:
PushNotification.setApplicationIconBadgeNumber(0)
You just need to run one of these functions when the event of a dismissed notification triggers, which may or may not need some java coding for achieve it!
I'm trying to create a custom notification in my android app with an image that will also be visible when the phone is locked.
Right now, Im building push notifications which are visible on the notification bar only, in which only my app icon is visible and on clicking which a certain pending intent is executed.
Is there a way through which i can show, my notifications with an image and a headline with it and also it gets visible on locked screen .
Also is there any widgets available or particular libraries through which i can implement my custom notifications?
Try using alertdialog with a flag. The flag_show_when_locked is the thing you trying to do I suppose. Please refer to this answer here.
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()
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'