Can a flutter app run code on receiving a push notification while Not Running - flutter

I have push notifications working and have both a data and notification part in them. In the data part is a tag that tells the app what updates are available for the user.
The problem is that I would like the app to fetch those updates even if the user does not tap on the notification.
With other apps, eg Whatsapp, a notification is shown. Without tapping this notification, and even when the app is not running, the app downloads the data. For example if the user switches to flight mode right after receiving the notification and then opens whatsapp, the message and/or picture is there, already downloaded and displayed in the app.
So the question is how to get an app written in Flutter to download something in response to getting a push notification, regardless of whether the app is in the foreground, background, or not running.

Checkout isolate mentioned in Flutter background processes with a link to Medium Executing Dart in the Background with Flutter Plugins and Geofencing
Checking with GitHub issue Flutter should provide an abstraction for background execution and issue comments/references after that you may find some other examples. Bottom line is people need more examples.

Related

Get all notification messages (if any) when the app is opened using app icon

I was working with flutter and I managed to implement FCM push notification in the app. The FLUTTER_NOTIFICATION_CLICK click_action works fine.
But, I need to perform that similar action when the app is launched by clicking the app icon. Is there any method to do so?
Ultimate goal is to perform similar action when the app is launched by clicking the notification or by the app icon (from the app drawer). Please help me with this.
As far as I know, there is no way to get the received notification content when the app is opened using app icon in Flutter. I tried to find a method, but as of now, I am at zero.
Flutter with FCM supports only onLaunch and onResume click_action events for notifications. If you are able to write some native code, I think you may be able to do this. A simple work around I can give is that :
You can clear the notifications as soon as the app is opened using NotificationManager in Android. Here is the reference.
After that you can fetch the actual notification messages from your server(if there is one) and show it to the user.
This is not an accurate answer. But this is what I finally did. Not sure if this helps.
If you click and launch from notification
You can listen that 'onLaunch' method in firebaseMessaging.configure
But from 'app icon' is no way as far as I know.

Flutter best way to store App Notifications?

Is there a way to store app notifications in a flutter app. Most of the time this app will be closed and the app notifications will not directly be hitting the app. I would like to store app notifications in a notifications received section. I know I can get the notifications if the app is opened, but for if the app is closed and it just shows as a status, I can't get them in the device unless the user clicks on the notification message. and it opens the app. What options do I have and is what I am wanting to do even possible?
First interpretation of your question: If I read your question correctly, you're saying its not possible to have text in your notifications when the app is not open. This is definitely possible, almost all your apps create notifications on the device with more than just 'status'.
Another interpretation of your question: In response to a firebase cloud messaging message, you might want to save data or do some other background task, instead of just creating a notification the user sees. This is not enabled by default. There's lots more instructions on enabling and using this in the README
By default background messaging is not enabled. To handle messages in the background:
The golden nugget of information is _firebaseMessaging.configure(onBackgroundMessage: yourBackgroundMessageHandler) which is not listed under the receiving messages section.
Let me know if I misread. What do you mean by status?
You can use background Fetch to make the app stay in the background. The package will awaken an app in the background about every 15 minutes.

An alert like whatsapp when app is in background in ionic

I am using One Signal push notifications in my ionic app. I want to show an alert when i receive a notification when my app is in background.
I tried using the platform pause and remuse functionality to check for background and foreground and then generate an alert when app was in background using ionic alert controller but was not able to produce the desired result.
I want a pop up dialog box like one in whastapp when u receive a message.
Can anyone please suggest a solution to this.
There is no way to achieve what you're trying to achieve through the use of OneSignal alone. The OneSignal plugin for Ionic has two calls to action:
handleNotificationReceived() & handleNotificationOpened()
The first one is called when the notification is received and the app is in the foreground.
And the second is called when the app is launched from the notification.
You would need some other service to record when notifications are sent to see if there were any when the app was launched by itself. If you use OneSignal's REST API to send notifications yourself, you can record when a notification is sent, and then when the app is launched by itself, you can fetch missed notifications or notification count from your own server. That's just one way you can do it.
But OneSignal is a push notification service, which send notifications. Even if you did FCM yourself and went through the scenario you mentioned, the results would be the same.
It's not a OneSignal issue. It's the way notifications are designed.

OneSignal - Sending silent notifications that only increment badge (app in background)

My app (iOS) is in background and I want to send a push notification that won't display anything (silent) and that will only increment badge count on the app's icon.
I am still not clear on how to manage that.
Is it a parameter in the message body or in my handler code?
Thanks
Try to send a push notification with content-available=1
Turn on the content-available (iOS) or silent-notification (Android)
fields. This will cause your application to be automatically woken
up in the background whenever a notification is received (even if
it's not clicked). Your custom code must be write with native code,
Java on Android and Swift or Objective-C on iOS. See Apple's
content-available for iOS and our Android Background Data guides for
details on receiving and processing the event.
In your app, we
provide an API that you can use to run custom code when the above
occurs. Your custom code can then save a copy of the notification
content on the device in order to be displayed in an activity feed
when the app is next launched. Or it could save a copy of it on your
servers.
https://documentation.onesignal.com/v3.0/reference

iPhone Push notifications via mobile safari

Is there a way to simulate push notifications by pushing data to mobile safari? Here are 2 scenarios.
I make a web app via phonegap and dont want to use APNS but rather make a web-socket connection and push data to the device myself. On the device end is there a "alert" function I can call to emulate a pop up when a user is not in the application?
Lets throw web app out the window. Is there a way I can do this in native mobile safari? Im not talking about a plain old JS alert window that would only come up if the user was in the app, but be able to do so with it backgrounded.
You cannot run background tasks with mobile safari so for #2 you can't do true push notifications or alerts. However you can send a user an SMS if you have the user's phone number. This can have a hyperlink to a part of your web site (which can contain some sort of payload). You can use a service such as Twilio to help you send SMS'es. However this costs money. APNS does not.
For scenario #1 I'm assuming you're talking about a native app using a phonegap solution. In this case when the app is backgrounded you cannot access any UI at all and wake up the app and show a UIAlert. In fact unless an app is registered for location updates or background music, the app is effectively not going to respond after a set period of time (it only can "finish" certain operations it had started before). So the websocket solution will only be effective if the user has the app opened.
You could register a local notification that runs at some predetermined time which will show an alert. But that is not being pushed from the server so its probably not what you want.
APNS is your best solution for scenario #1. Its not that hard to implement and its pretty inexpensive. Check out urban airship if you want to avoid building out your own server-side components for it.