devise-onesignal
v4.0.1
Published
Implement user targeted cross-platform notifications in your Rails app
Downloads
8
Readme
devise-onesignal
Implement user targeted cross-platform notifications with OneSignal & Devise in your Rails app. Can also be used without Devise, but is primarily intended to be used along with some sort of user-management-system.
This gem works well together with notifications-rails and its notification-pusher-onesignal component which introduces a notifications handling & pushing API. To build a cross-platform notification solution also add the native gem to your app.
Table of Contents
Installation
devise-onesignal works with Rails 5 onwards. You can add it to your Gemfile
with:
gem 'devise-onesignal'
And then execute:
$ bundle
Or install it yourself as:
$ gem install devise-onesignal
If you always want to be up to date fetch the latest from GitHub in your Gemfile
:
gem 'devise-onesignal', github: 'jonhue/devise-onesignal'
Now run the generator:
$ rails g devise_onesignal
To wrap things up, migrate the changes into your database:
$ rails db:migrate
It is time to create your OneSignal app if you haven't already and set your application ID in the created initializer (config/initializers/devise-onesignal.rb
).
Define an association in those models whose objects are supposed to be associated with OneSignal players. For example User
in app/models/user.rb
.
has_devices
Now add the devise-onesignal
NPM package and import the neccessary javascript files in our webpack pack (apps/javascript/packs/application.js
):
import 'devise-onesignal/dist/OneSignalSDK';
import * as deviseOneSignal from 'devise-onesignal';
Finally initialize the javascript component when your assets load. If you are using Rails 5 with Turbolinks, this could look like this:
$(document).on( 'ready', function() {
deviseOneSignal.init( '<%= DeviseOnesignal.configuration.app_id %>', <%= DeviseOnesignal.configuration.auto_register %>, <%= DeviseOnesignal.configuration.persist_notification %> );
});
Note: Listen for the ready
event even if you are using Turbolinks, as the init()
function should only be called once for every full HTTP request.
Usage
Devise object
You most likely want to associate your devise object (e.g. current_user
) with your OneSignal integration. Now, if your Devise model is called User
and the current_user
method is available you don't have to worry about that.
Let's say our Devise model is named Admin
. Just add a method to your ApplicationController
:
def set_device_owner
current_admin if current_admin
end
Note: Essentially set_device_owner
has to return a class object or nil
.
Device methods
devise-onesignal introduces a Device
activerecord model. Every object of your devise class can have multiple devices, one for each device / browser they enabled OneSignal at least once.
d = Device.first
# Returns object associated with device. Can return `nil`.
d.owner
# Returns OneSignal player id
d.onesignal_id
# Returns OneSignal permission ( `granted`/`denied`/`default` )
d.onesignal_permission
# Some timestamps
d.last_used
d.created_at
d.updated_at
# If device is ready to receive notifications
d.onesignal_enabled?
# If device has been disabled to receive notifications
d.onesignal_disabled?
# If device has been set previously to receive notifications or not
d.onesignal_set?
has_devices
This method makes devices
available in your models. Let's say you have a user which has multiple devices he has enabled OneSignal on:
u = User.first
# All devices of a user
u.devices
# All OneSignal Player ID's of a user returned as a hash
u.onesignal_player_ids
current_device
In your controllers and views you can access a current_device
method that returns the Device
object of the current OneSignal subscription. If the user browsing your site is not subscribed to OneSignal, current_device
returns an uncommitted Device
object with some helpful information, like the permission
attribute, set.
Subscribing
Just call deviseOneSignal.subscribe(<%= DeviseOnesignal.configuration.subscribe_with_modal %>);
in your frontend code and OneSignal will ask your user for permission to send notifications. On the following request an existing Device
object will either get updated or a new one will get created.
You can check whether the current user has already enabled notifications by using the deviseOneSignal.subscribed();
function.
Unsubscribing
Note: You most likely don't want to let your users unsubscribe from receiving notifications, but instead allow them to manually disable receiving any new notifications. For that purpose use the notifications-rails gem, which adds a notification API and detailed user settings.
If you want to completely remove a user from OneSignal, call deviseOneSignal.unsubscribe();
in your javascript.
Configuration
You can configure devise-onesignal by passing a block to configure
. This can be done in config/initializers/devise-onesignal.rb
:
DeviseOnesignal.configure do |config|
config.app_id = ''
end
app_id
(required) Your OneSignal app id. Takes a string. Create one here: https://onesignal.com
auto_register
Automatically try to subscribe the user when loading a page. Takes a boolean. Defaults to false
.
persist_notification
Automatically dismiss the notification after ~20 seconds in Chrome. Takes a boolean. Defaults to false
.
subscribe_with_modal
Show a modal instead of a native browser window when subscribing a user to OneSignal. Takes a boolean. Defaults to false
.
To Do
Here is the full list of current projects.
To propose your ideas, initiate the discussion by adding a new issue.
Contributing
We hope that you will consider contributing to devise-onesignal. Please read this short overview for some information about how to get started:
Learn more about contributing to this repository, Code of Conduct
Contributors
Give the people some :heart: who are working on this project. See them all at:
https://github.com/jonhue/devise-onesignal/graphs/contributors
Semantic Versioning
devise-onesignal follows Semantic Versioning 2.0 as defined at http://semver.org.
License
MIT License
Copyright (c) 2017 Jonas Hübotter
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.