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!
Related
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()
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
I implemented Sinch + PushKit + CallKit, everything works fine, but there is one script that does not work correctly. The first user uses the application and removes it from the device's memory, the second user calls for the first time, the first user does not receive the call, if the second user immediately calls the second time, the first user receives a call (later the first user also receives a call). If the first user opens the application (that is, becomes online for the system), the first user will not receive the call again. How can I fix it?
initialization of the sinch client
open func setup() {
guard sinch == nil else { return }
guard let userID = UserRealmManager().getUser()?.id else { return }
sinch = Sinch.client(withApplicationKey: key, applicationSecret: secret, environmentHost: host, userId: userID)
sinch?.delegate = self
sinch?.call().delegate = self
sinch?.setSupportCalling(true)
sinch?.enableManagedPushNotifications()
sinch?.setSupportPushNotifications(true)
sinch?.start()
sinch?.startListeningOnActiveConnection()
}
Update: I also found that if I restart the iPhone then calls through CallKit start to show up in 2-4 minutes, I decided to test it on the famous messangers such as What's app and Telegram and they have exactly the same behavior. Of course, I think it needs to be asked as an additional question.
My devices are iPhone 6 and 7.
This was my mistake, since I initialized SinchManager (this is the manager that manages SINClient) only in MainTabBarController viewDidLoad(), after I began to initialize it in AppDelegate didFinishLaunchingWithOptions, everything works fine.
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
I'm using pyapns to send notification to iPhone.
receiving notification when the app is running was success.
but, when app is not running or app is on background, it can't receive notification.
is it related with URL identifier or scheme? if not, what is the problem..?
Did you get called in
- (void)application:didReceiveRemoteNotification:
while you were in the app?
the problem from notification msg.
among the notify function args, there is notifications list arg.
note that, when you make this parameter, a dictionary of this list should contain below. so that the application can do alert reaction.
thePayLoad = {
'aps': {
'alert':'this option should be contained if you want to see the alert msg',
'sound':'k1DiveAlarm.caf',
'badge':42
},
'test_data': { 'foo': 'bar' }
}
In my case, I just sent msg not containing above. and in didReceiveRemoteNotification function, I made alert msg. but msg not containing alert information. so the device couldn't react.