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)
Related
I'm using https://github.com/zo0r/react-native-push-notification for local push notifications. It also includes an option to have some buttons on the notification("actions"), which I can press on.
However, when I'm getting a notification, to respond to that button(noti. action), the app should open(the only way I could work it around) and only then I can run my logic according to the action.
Because I have some troubles using react-native-push-notification for this(It simply doesn't work), I would like to implement this by myself.
This is the process I wish to have:
Push notification is showing up
The user clicks on the snooze button(same as an alarm)
The application is still in the background
The notification will be pushed again after some time.
What is the proper way to do such a thing? I have to run a background task that is fired when the user presses the snooze button. How can I use HeadlessJS for this? It is very easy to implement the snoozing logic with JavaScript, the problem is to fire it.
I have multiple apps that kind of work together to do the same job and they all belong to the same developer. Each app runs a long-running service in the background and keeps processing user's input. The problem is that those services cannot run in the background for a long time because Android system will kill them. So I want to use foreground services instead of background ones to prevent the system from killing them. However, I don't want to annoy the users with multiple different notifications in the notification drawer.
I found out that creating a notification without assigning a channel in Android O, will let the system start the foreground service without showing a notification. Something like the following:
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle(......);
builder.setTicker(......);
builder.setContentText(......);
builder.setSmallIcon(......);
builder.setLargeIcon(......);
Notification notification = builder.build();
startForeground(111, notification);
So I was thinking of showing a notification by creating a notification with a channel from one app and create a notification without a channel for the other apps as I described earlier. In that case, the user will see one notification for all my apps.
That solution works well for me. But I am wondering if it is an unintended use of the notification in the foreground services. I am afraid that Google will suspend my apps for doing that!!
Do you guys know if it is okay to implement that solution?
Or is there any way to stack the notifications together in a group even though they are different apps?
My goal is just to make the notification less annoying to the user.
Also, I am aware of JobScheduler & JobIntentService solutions. But they don't do the job in my case because I want to keep the service running. Not like do one job and stop the service...
You can create notification channel with IMPORTANCE_LOW (https://developer.android.com/training/notify-user/channels#importance).
There shouldn't be sound.
Or you can also use setOnlyAlertOnce() (https://developer.android.com/training/notify-user/build-notification#Updating) and the sound will be only once.
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
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'
We want to send messages to app users either through notifications, dialog box or image opening up on their screen every 24 hours telling them that our app is running on their phone.
We were looking to use Notification builder but it has limitation that it only works for api 11 and above and half of all app installations today are for earlier api versions. We are trying to find out which would be the best way to go with this.
I'm not sure what "Notification builder" is, but you can certainly use Notification and NotificationManager in any API you want.
So, putting it all together, I would use AlarmManager to fire off an alarm every 24 hours. Set up this alarm when your application runs, and in a BroadcastReceiver which is configured to receive BOOT_COMPLETED. The BOOT_COMPLETED notification allows you to quietly restart the alarm if the device reboots.
The alarm triggers another BroadcastReceiver which puts the notification up. If the user selects the notification, then your application is launched. Mostly, the presence of the notification will be all the reminder your user will need.
My notes say that NotificationManager can pop a View up onto the screen, which could be a dialog. However, I think a simple icon in the status bar would be best, since you're just reminding the user that the application is present.
Oh, p.s., if your application is a service that's running in the background 24/7, then you should also remember to restart it in the BOOT_COMPLETED broadcast.