how to receive message in SignalR iOS background? - ios

I have implement the SignalR in iOS app it receive the events in Foreground but app enters into background the receive events not working..
How can I receive signalR message in App Background

SignalR in a mobile app will only work when your app is in foreground.
Once the app is moved to background, SignalR connections are closed, and therefore your user can't receive any messages.
A way to go around this would be to use Push Notifications, so that as soon as your user get's disconnected from your SignalR endpoint, you can send him notifications to let him know that new messages arrives for him.
Don't forget to save messages that a given user didn't receive, as SignalR only buffers messages for temporary disconnects.

Please add this code in didFinishLaunchingWithOptions
You will surely get called the SignalR events
var bgTask = UIBackgroundTaskIdentifier()
bgTask = UIApplication.shared.beginBackgroundTask { () -> Void in
UIApplication.shared.endBackgroundTask(bgTask)
}

Related

how to know that a data message was received when the app was in background?

When the app is in background and receive a data message, the delivery of the data message is delayed and the app will receive this message when it's will become foreground via messagingDidReceiveMessage. When the app is foreground then it's will receive the data message directly on messagingDidReceiveMessage
My problem: how to know in messagingDidReceiveMessage that the data message was received when the app was in background and was waiting the app go foreground ?
messagingDidReceiveMessage is used to receive messages that bypass APNs and are sent directly to your app from FCM. This is only available when a connection is open between your app and FCM, this is only available when the app is in the foreground.
To receive data messages when your app is in the background you need to use the APNs callbacks. You can still send data messages through FCM when your app is in the background but you can't use the messagingDidReceiveMessage callback in that case.

How to make a server call on receiving push notification in swift?

I am developing a messenger app like whatsapp, it has to notify the server that the message is received, I am sending the status to server when didReceiveRemoteNotification is called. But when the app is not in foreground, it is calling only when the user taps on the notification. How to make server calls even when the app is in background or not running?
To receive a remote notification when your app is in background or suspended mode first your server should send special parameter in notification payload called content-available = 1. to learn more about that look the documentation: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW5
Also turn on this switch in the project settings:
With this parameters when your app will receive a push notification it will be waken up for some small time to background mode and call didReceiveRemoteNotification. Don't forget to call completionHandler(<some enum parameter>) in the end of your task, or your process can be killed by iOS unexpectedly, for example when you're trying to perform async server requset. So when your async method completes call completionHandler closure argument of didReceiveRemoteNotification.
Actually there is no managed code can be run when your app is completely thrown away from the app switcher. As far as I know to perform that you should use VOIP push messages - but that kind of messages is only for apps with voice calls, like Skype or WhatsApp. This kind of apps has some code to perform even if they are completely turned off. iOS gives this ability to them to prepare to voice call. That's why all these messengers use Voice calls - to make instant delivery status for example. If you want that kind of interaction you should google more about VOIP. But your app should really have some voice calls function or it will be rejected by the app store.

Notify video chat application when app is terminated

Is there any way to notify my video chat application that there is an incoming video call when the app is in the terminated state? (Like an Android background service.)
I notify of incoming calls via socket when the application is in the running and background states.
App that are using VoIP are treated differently by apple and have more privilieges. In the past, that meant keeping a permanent connection to the server to get notified when a call occurs.
This was not very efficient in terms of energy saving.
Since iOS8 it is recommanded to use PushKit (the push notification API) to notify your user when a call occurs. Apps with VoIP privileges will be notified on the spot and that can wake up your app whatever the state it's in.
Here are the world from apple :
Instead of persistent connections, developers should use the PushKit
framework—APIs that allows an app to receive pushes (notifications
when data is available) from a remote server. Whenever a push is
received, the app is called to action. For example, a VoIP app could
display an alert when a call is received, and provide an option to
accept or reject the call. It could even begin taking precursory steps
to initiate the call, in the event the user decides to accept.
https://developer.apple.com/library/ios/documentation/Performance/Conceptual/EnergyGuide-iOS/OptimizeVoIP.html
Get pushkit payload
Implement pushkit at ios code side
Once you receive silent notification
Schedule local notification
Keep important information in local notification's userinfo
Also keep local notification object in NSUserDefault
If your device gets restart then you can retrieve localnotification userinfo from NSUserDefault object
Once you get silent notification your socket will get active upto your local notification sound plays ( Max 30 seconds )
If you want incoming call ringing more than 30 seconds then server has to send silent notification at each 30 seconds
Delete previous object and reschedule latest on local notification
Let me know if you need any help in push kit implementation.

How to send a silent push notification if the user disables them?

I'm working on a chat app and I need to know when a push notification is received to reload the conversation view with the new message. It's working well if the user allows the app to send push notifications to his device (with the notification dialog). However, if he taps "No" in this dialog, the following code, which registers the user device token to my database, is not executed :
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// Update the OneSignal id for this current installation
oneSignal!.IdsAvailable({ (userId, pushToken) in
if (userId != nil) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.setValue(userId, forKey: "oneSignalId")
installation.saveInBackground()
}
})
}
Then, how to get the user device token, if he taps "No" in the notification dialog, so I can still target him to send silent push notifications ?
Thanks!
Actually it is possible to do it but not with OneSignal.
Apple push notification does support push on the background, That means even if user doesn't grant access to notification it would just not show on his screen when he gets one.
With OneSignal the issue is that the system doesn't send a push to users who have disabled push notifications. That means that your notification won't ever reach your app because it won't ever be sent through Apple Push Service.
I suggest you use another vendor for that (it works with Parse-Server by the way).
If you want to fetch the data in background instead of using push I suggest you use "Background fetch", That would wake your app from time to time and let you run a bit of code to check if there is anything new.
Sorry mate, you can't send apple push notifications to a device if the user doesn't allow Push Notification in settings, in that case you need to follow other methods to detect change in data.
You can implement sockets. i.e UDP connection which listens to the server for any incoming messages. You need to start the connection in the chat ViewController and and if you receive a packet indicating there is a chat message, you can reload data. Also there is live messaging service such as xmpp which is kind of the same but more improved with much more chat support. Chat services like Whatsapp uses them.
Another option is to reload data every 4 or 5 seconds using a NSTimer object. Call the api in background, so the user won't know it is constantly refreshing and update if there is new data. Also invalidate the timer once you go out of scope of the chat. The one i used, used the same technique with calling the api service both in chat view controller and conversation view controller.

How can I handle unread push notifications in iOS?

I have a iOS 5.1 application that registers to the APNS service to receive notifications. The register is successful and I receive the notifications correctly. The problem comes when I try to handle the notifications.
Once the application is running, the method didReceiveRemoteNotification in the AppDelegate is called correctly and so the notification is handled as intended. This, however, only happens when the application is running on the foreground.
However, when the application is running on the background or is simply stopped, that method is not called. I've read that you should add some lines to the method didFinishLaunchingWithOptions method to obtain the notification from the userInfo dictionary, and handle it. This works just fine, but ONLY when the application is opened by clicking on the notification at the Notification Center. This means that if you open the application by clicking on its badge, or simply by changing context if you were running it on the background, the app never realises that a notification came in. Additionally, if more than one notification was received, we can only handle one of them at once by clicking on the Notification Center, which is a pain :-)
Is there any way to read the pending notifications in the Notification Center? I know there is a way to flush them using the method cancelAllLocalNotifications but I haven't found a way to just read them. And I really need to handle all of them. I thought of implementing a communication protocol with the third-party notification server to retrieve the information again when the application comes to the foreground, but since the information is already in the operating system I would find it strange if it's impossible to access it somehow.
So, does anybody know a way to do it? Thanks in advance.
When a push notification arrives and the user clicks 'cancel', your app has no way to read that push notification again. You have to implement a separate functionality (most probably on server-side) to fetch a list of notifications sent to this device.
For example, if a chat functionality is provided in your app and you send chat messages via push notifications then you should also keep chat messages on the server. If a user clicks 'Cancel' on any push notification then that chat message will not be displayed on the iOS device. In that case when a app comes in foreground later, you make a call to the server and fetch all the past chat messages (sent via push notification).
Ok, So a possible solution would be to have another database table with the messages in with a 'read' flag and a messageID field? Which by default the read flag is NO, then when the app successfully reads this and displays, it updates the flag to YES?
And with only 256 bytes to play with, what sort of ID field length would be necessary?
Edit,
Executed this plan and its working successfully.

Resources