npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

devise-onesignal

v4.0.1

Published

Implement user targeted cross-platform notifications in your Rails app

Downloads

3

Readme

devise-onesignal

Gem Version

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.