Ionic 2 App currently used or not - ionic-framework

I am using ionic 2.
I am using cordova local notifications.
How to check my app is currently used or not.
Kindly advice me,
Thanks

You could use .on('click') and execute a function that sends you that your app is beeing used. Something like:
import { LocalNotifications } from 'ionic-native';
LocalNotifications.on('click', (notification, state) => {
// DO YOUR STUFF
});
You register this in the constructor or deviceReady of your app.component.ts so every time a user clicks on a notification it executes what's inside this block of code.
You could send to your phone a push, save something on a database, or anything you want. The state returns if the app was on background or foreground, with this you can manage even more doing a if/else.
If you need more events, look at this wiki.
Hope it helps :D

Related

PWA home screen "uninstall" DOM event

I am trying to keep track of whether a web app has been installed to the user's home-screen using a value in localStorage.
I know there is DOM event that fires when a web app has been installed into a user's home-screen, but is there an event for when it has been uninstalled?
The type of event I have in mind would ideally be scheduled in a manner similar to (and behave in a manner similar to) onunload. (ie. an uncancellable event that allows me to schedule some last bit of work before the app is destroyed)
eg:
window.addEventListener('appinstalled', function(e) {
console.log('onappinstalled', e)
localStorage.setItem('APP_INSTALLED', '1')
})
// given the above, is anything like the following possible?
window.addEventListener('appuninstalled', function(e) {
console.log('onappuninstalled', e)
localStorage.setItem('APP_INSTALLED', '0')
})
I realised that once a user has uninstalled the app from their home-screen, the browser will begin prompting to install the app to the home-screen, again, provided you have met the criteria.
So by using the onbeforeinstallprompt event, there is an opportunity to clear the 'APP_INSTALLED' key from localStorage, and perform other arbitrary work.
eg:
window.addEventListener('beforeinstallprompt', function(e) {
localStorage.removeItem('APP_INSTALLED')
})
Moreover, this localStorage key may have already been cleared if the user elected to delete all data associated with the app when uninstalling the app from their home-screen.

(Ionic) handleNotificationReceived does not get called if the app is killed

I am working on an ionic app and I am using onesignal for push notifications.
The problem I am facing is that the handleNotifiactionRecieved() does not get triggered upon receiving a push notification when the app is not killed (that is removed from the recent apps). Though it works as expected when the app's running but in the background(not inFocus).
setup code is something like this:
if (this.platform.is('cordova')) {
this.oneSignal.startInit('APP_ID');
this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.None);
this.openOneSignalMessage();
this.oneSignal.endInit();
}
my code is something like this:
openOneSignalMessage() {
this.oneSignal.handleNotificationReceived().subscribe((data) => {
this.MsgBody = data.payload.body
console.log('newMessageOneSignal MsgBody', this.MsgBody);
});
}
actual result: handleNotificationReceived() doesn't get called when the app is not running, that is killed.
expected result: handleNotificationReceived() should be called every time a push notification arrives even when the app is not open.
How do I trigger the method every time?
Thanks in advance.
Thanks for pointing out the issue in the docs. They have been updated with the correct info:
handleNotificationReceived (builder method)
Sets a notification received handler. Only called if the app is running in the foreground at the time the notification was received.
If you want to handle from a killed state, make sure to use the handleNotificationOpened method
I find this in the doc:
Sets a notification received handler. Only called if the app is running in the foreground or background at the time the notification was received.
Maybe your app is not working on background.
Or the problem is you need to call ur function after the startinit :
this.onesignal.startInit("YOUR_APPID")
this.onesignal.handleNotificationReceived()

Background notifications using Ionic like a mortal

I have been trying for several months to implement an app that receives notifications whose content I want to be stored in the database of my application. I am using the Firebase Messagging plugin and I am receiving the notifications correctly, both in Foreground and in Background.
However, in the background I am unable to execute my callback without the need to press the notification explicitly by the user. Likewise, there is no way to increase the badge of the application when it is not open.
Now, I do not understand how applications like WhatsApp, Telegram and any other application that is not made by mere mortals work. How is it possible that they can get the data in backgroud, manage the badges, synchronize messages, etc? Being that, supposedly the services like Firebase are limited in background. Even with plugins like Background Mode my application is suspended by Android when the user closes it.
The code that I am currently using to handle notifications is the following:
// In foreground (WORKS)
this.firebaseMessaging.onMessage().subscribe((notificacion) => {
// Insert in DB
...
});
// In background (DOESN'T WORK)
this.firebaseMessaging.onBackgroundMessage().subscribe((notificacion) => {
// Insert in DB
...
});
What alternative is left? The only thing that occurs to me is to use notifications in Foreground and background only as a warning. So every time I open the application I'll have to call a message synchronization callback with the server (with badge management included).
If someone has some better way, I would greatly appreciate it if you throw a little light on the subject.
From already thank you very much
Ok, after more than a year I think it's time to close this question. I solved the problem this way:
Suppose I need to get a list of messages. This action is made by a function called getMessages()
When a new message is created in the backend, I send a push notification through Firebase service.
If the push notification is received in foreground I refresh call the method directly:
this.firebaseMessaging.onMessage().subscribe((notificacion) => {
getMessages();
});
If the push notification is received in background and the user taps on it there isn't a problem as it's supported by the plugin:
this.firebaseMessaging.onBackgroundMessage().subscribe((notificacion) => {
getMessages();
});
If the push notification is received in background but the user DOES NOT tap on it and opens the app in another way than the notification, we need to excecute the corresponing function when the app is opened in app.component.ts file:
this.platform.ready().then(() => {
getMessages();
// Extra stuff like this.statusBar.styleDefault(); or this.splashScreen.hide();
});
That way all the cases are considered! You should keep in mind that apps like Whatsapp or Telegram are developed in official technologies like Java or Kotlin which not only have official native plugins, also its code are excecuted natively and not in a limited browser as Cordova or Capacitor frameworks do

Which app delegate function to send notifications in background?

I have searched this topic for a while but not found anything that does exactly what I want. I have this code that checks for when a new post is added to the database, and when that happens, I want to send the user a notification. Here is that code:
Database.database().reference().child("\(mySchool!)/posts").observe(.childAdded, with: { (snapshot) in
self.notifyNewPost()
})
My question is, which (if one) function should I put this inside of the app delegate to have this run in the background? Thanks!

How to use external file to handle push notifications in FirefoxOS?

I'm trying to get working Push Notifications on my FirefoxOS App. While it's working as expecte when the app is opened, when the app is closed it not works anymore. I thought that was the app is trying to execute the whole app instead only the handler so I'm trying to use the messages section on the manifest.webapp.
"messages": [
{ "push": "/push/push.html" },
{ "push-register" : "index.html" },
{ "notification": "index.html" }
],
My code on /push/push.html is:
<script>
navigator.mozSetMessageHandler('push', function () {
c = new Notification( 'testing 4' );
});
</script>
But it seems that's not loading the file when the push message arrives the device. For me, it doesn't seems to be a path problem, because I tried everything (launch path is /index.html, and I've tried moving push.html to the root and put /push.html).
Any clue?
Well, after hours trying it and a lot of research, seems that's not possible to get working the push in another page.
Remember: when a user selects an app, the lauch_path is displayed.
There’s a bug right now that restricts push notifications to the page
that calls register(). Right now, what’s under “messages” doesn’t
matter if it’s not the same as the page that calls register(). Keep
your eye on that bug if you want to know when it’s fixed. For now,
it’s probably a good idea to keep your app to one page and use dynamic
formatting to present info back to the user.
Firefox OS v1.1 only allows the page that registers for Push
Notifications to recieve them, so you cannot have / call
navigator.push.register() and have /push.html be the receiver that
uses navigator.mozSetMessageHandler().
More info about the bug:
https://bugzilla.mozilla.org/show_bug.cgi?id=800431 [^]
https://bugzilla.mozilla.org/show_bug.cgi?id=897112 [^]