ROR Push Notification Engine - ruby-on-rails

I have recently been assigned a task to develop a notification engine. For the notifications we are going to use Push Notification. I am looking for the best possible solution for the engine because in future we have to scale the application to other devices also. Following are some details of the project
Backend:
Backend of the application is developed in Ruby on Rails as webservices
Devices that will have push Notification
iPhone, Android, Pebble (smart watch), Web application
Current Solution:
Currently, we are thinking to make a back-end database table for notifications. A worker class in Rails will run after 1 minute and it will push all the notifications to the devices stored in the database. From the webservice methods, we will insert the data in the notification table.
For pushing notications we do not want to use services like UrbanShip. We are only going to implement them using Ruby Gems. Currently, we made a small demo based on GCM gem for android push noticiations.
Questions: Is my approach to the solution is correct ? or is there any better solution for this kind of problem.
EDIT:
I think that my previous description of the problem was a little confusing.
Ultimately we are going to use GEMS in Ruby to send push notifications. Forexample for iOS we are going to use Houston or Grocer gem and for Android GCM.
Problem: We need some database tables where we will store notifications so that the GEMS (mentioned above) can use them to send the notification to users. Now, to fill the database tables we need to write the logic somewhere so that we can insert the notification in the table.
Forexample, lets say that when a user first registers in the application we send him a notification. Now, to do this we need to write the code for adding the notification in the Register function.
like
public void Register()
{
//Registration logic
//Add a notification in the notification table
}
Now, this is a problem because we need to add the notification logic in all the functions that need to send notification. Is there any other good solution in ROR or in general ?
Some design pattern ?

I've spent a fair amount of time looking at Ruby based push notification solutions. The best one is RPush https://github.com/rpush/rpush. RPush is very well tested at this point (We use it to send millions of notifications), and handles a lot of difficult edge cases well. I wouldn't recommend building your own from scratch since there are so many potential pitfalls and edge cases. RPush doesn't support Pebble or Web App notifications, but could be extended to do so.
If you decide to explore other alternatives, make sure they:
Handle closed connections gracefully for APNS - In many cases, Apple may close the connection to their server, and your push notification library must handle this correctly otherwise thousands of subsequent notifications can go undelivered
Communicate with Apple's feedback service - Apple requires you to poll one of their endpoints for a list of devices to stop sending notifications to. If you fail to do this, you can get rate limited.
Can send notifications at a fast enough rate for your requirements.
Outside of Ruby, the best push notification libraries seem to be PushSharp (C#), and Node-Apn (NodeJS, iOS only)
Finally, it sounds like you have specific needs that require you to do this yourself. But for others, I would strongly encourage you to use a 3rd party services. Reliably sending push notifications at a high volume is difficult and there are many 3rd party services that will do it for you at low cost. For instance, UrbanAirship, Parse, and OneSignal (My service) are all great 3rd party solutions.
Update to address revised question:
The best design pattern is to have a a second daemon process or Cron Job that handles message delivery. It's not practical to try to do this inside of a Ruby on Rails application.
The RoR application can insert rows into the Notification table as a queue like you describe. Then the daemon process or cron job can fetch notifications from the queue and deliver them.
If you use RPush, this is the pattern that it follows. It comes with both a Gem to load into your Rails application that inserts notifications onto a database queue, as well as a daemon that you keep running on your server that periodically checks for new notifications to send and delivers any that get queued up.

I wonder if this question is still open or you already have a solution, but I'd like to propose this gem I've recently published: https://github.com/calonso/ruby-push-notifications
It's really simple to use, flexible enough to fit your architecture and works!
At the moment is just the gem itself, but I'm working on building a whole Rails plugin around it, with all the tables structure and stuff. You can see some work in progress here: https://github.com/calonso/rails-push-notifications

This article describes a very simple but fairly comprehensive approach to handling push notifications on your Rails backend.
In a nutshell:
Implement a Device model together with some controller actions to record users's device tokens in your DB.
Create a Notification model, which in the article is a Redis list but you can also use ActiveRecord if you don't really want to use Redis.
Create a background worker that is actively running through the incoming notifications and sending them as push notifications. The article mentions grocer and GCM to send them to iOS and Android respectively but you can also use other useful gems such as rpush, houston, etc.
Having a separate worker processing notifications is a good idea because you don't want to be constantly opening and closing connections to Apple and Google's servers with the risk of getting locked out, since it might look like a Denial of Service attack (depending on the frequency of your notifications).

Related

Automatic/Scheduled iOS Push Notification

I'm very new to iOS development. I currently have a fair chunk of the app I want to make done but I'm just now trying to figure out how to implement remote push notifications. I essentially need these to happen automatically and if a certain condition is met.
Basically, I'm using a weather api to do some stuff in my app, and eventually I want to send each user a notification depending on the weather for their location+some other factors (those other factors will be stored in the database, which has not been set up yet) while the app is closed. Would I need server side code to do something like that? And would it simply have to execute once every 24 hours (or some interval of time) for each user?
I've looked at several tutorials about how to set up push notifications but I can't figure out what I need to do to make this work the way I need it to so some explanation would be great. And what service would be best for this (like aws/lambda, firebase, etc)?
Push notifications can be very tricky and are more on the advanced side of iOS Development, it can be a long and complicated process. However, fear not! raywenderlich has an excelent tutorial that can help you implement Apple Push Notifications.
As for sending push notification out at regular intervals, it depends on your type of server. I would normally recommend using a cron job to schedule regular intervals for your push notifications, as I have had good experiences with using them for just this purpose but since you are using firebase which has no cron jobs or any type of scheduling options, you will probably have to use a third party api like Zapier.
Otherwise, Google's Cloud Platform can provide you with ability to schedule tasks for whatever you want to accomplish!

Available options to fetch data from a server while on background in react-native

I want to make an app with react-native (that will probably be running on an android phone) that will fetch messages from a server and will send them as SMS to a contact. The thing is that it needs to keep watching for new messages from the server, and that got me wondering how people deal with this kind of situation, specially when the app is running on background.
1 - Should I do something like setInterval on the background (with this) and keep fetching from the server to look for new messages, or is that too inefficient? If no, is there a specific case where I should use these background setInterval, or setTimeout or something similar?
2 - Should I use something like OneSignal with push notifications to handle this? When should I do that?
3 - I am using rails as back-end in another project, and rails 5 added ActionCable which enables using WebSockets. Is it possible to implement good and efficient notification feature for mobile apps using WebSockets instead of using OneSignal?
4 - Any other tips on notifications, running code on background, when to do something, when not do something, etc?
You should use GCM(Google Cloud Messageing)to receive messages from the server, not otherwise.
On iOS, use APNS.
You should use FCM(Firebase Cloud Messageing)to receive messages from the server. GCM is no longer maintain by google.use use this package to this functionality

How to update app when new data is available

I'm fairly new to app development and am having trouble figuring this out. One part of my app involves a messaging platform. Right now I am using a php web server to connect to MySQL backend. I have all the functionality of a messaging app, but I am unsure how to push new messages when they are available.
One basic solution would be to call my function to check for new messages every x amount of seconds, but obviously that's not a good solution. I have looked into Apple Push Notification Service and am unsure if this would fit my needs. When I looked into it this seems to be for sending notifications to the user remotely. However, rather than the user getting these messages displayed I would like the app to call a function instead (the function would load the new messages). Is this possible with push notifications?
Also this app is on both iOS and Android, so if there is a (possibly third party) solution that would cover both it would be ideal.
Any insight into this problem is greatly appreciated!
When you send a push notification and the app is in the foreground, the AppDelegate has a method called which allows you to execute whatever method you want.
This is only for iOS though, I don't know know it's handled in Android.

iOS - Push notifications and background threading

I have a service that allows user to enter the type of events they like, and whenever a new event that fits those criteria is available in my database, I want them to get a notification.
I have been looking around at the best way to handle it and I have found two possible solutions, but I'm not very clear with which one I should use and how.
First, a solution that looked great was the didReceiveRemoteNotification method and the usage of remote silent notifications to tell the app that new content was available. But my questions remains: how can I send this remote notification to the user if I don't know which criteria he has. I mean, how can I send this notification using PHP? I'm a bit lost here.
So I found another possible solution that does look a lot like a hack (iPhone - Backgrounding to poll for events), to be able to make your app execute a method every XX minutes while it is in background. This would be way more battery consuming and I'm not even sure it would be accepted by Apple, but at least it is clear as to how it works: the app downloads data from a link with the parameters that fit the special criteria, and if there is new data, it sends a notification.
How could I combine both these methods?
EDIT
I think the main issue on my side is that I don't understand how I could check a certain PHP file whenever new data is added into mysql and make sure that it fits the criteria of the user and then send the notification. That is the part that I don't understand in the backend PHP usage.
Your flow should be like this -
Mobile -> BackendServer(PHP) -> APNS server -> Notifications->Back on device.
User will submit her/his criteria to server then server will process on that and send request to APNS server.
The APNS server will send remote notification on her/his device based on criteria requested.

Does RestKit supporting long polling and what would I do if app run in background

I Googled around and I can't find many discussions on this. I want to develop an iOS program that would use access a REST service, and I want to get notified of updates so I am thinking of long polling. Does RestKit deal with this?
Another questions is what if I want to run in the background? It seems like the proper way to do is to set up an Push Notification Service and notify the user to open the app to receive the latest message?
Doing a job in background is only possible with special APIs like Music and Location, so you won't even be able to do queries if the user is not using your app.
Instead, you should do all the heavy work on a web service, and setup an APNS server to notify the user when something happens. That way, it won't drain all the battery of your users and use technology in place exactly for that purpose.
There's also a lot of service to send out push notifications, if you don't want all the heavy setup. Take a look at http://parse.com or http://urbanairship.com/.

Resources