One Signal: pretty, simple, push notification, done right!

One Signal: pretty, simple, push notification, done right!

ยท

0 min read

In this article I want to share my experience and some code using OneSignal, an online platform that I use to simplify the management of Push Notifications within my applications and tracks all info about them, sending stats, received stats, custom metrics defined by me and errors.

DISCLAIMER: I am absolutely NOT associated with OneSignal and I do NOT derive any economic benefit from publishing this article, a.k.a this is not a sponsored article.

What's OneSignal?

OneSignal it's a service used to simplify the process of sending Push Notification without headaches of configuring services. During my experience I used it sending push through Node.js and receiving them on Android, iOS, Flutter Apps; the service configuration, both server and mobile, is super damn fast.

In other word I used OneSignal as a "wrapper" or a "PNaaS" (Push Notification as a Service ๐Ÿ˜…) between my mobile world e server world because I need a reliable way to send and deliver push notification without configuring any custom and/or proprietary service and managing all the stuffs with GCM/Firebase/APNs.

Platform Support

As stated on the website, platform support is very broad with 30+ Platform Integrations:

  • Mobile: Android, iOS, React, Flutter, Xamarin, Titanium, PhoneGap, Cordova, Ionic, etc
  • Integrations: Google Analitycs, Zapier, Google Analytics for Firebase, Amplitude, Blueshift, etc

In the following chapters I'll show you briefly how to configure OneSignal in an Android App (both native and Flutter), this is not a tutorial so I skip most of the part but you can find the complete documentation at this page: OneSignal Doc.

Android SDK (Doc)

All you have to do is to add the OneSignal dependencies in the app/build.gradle (Module: app)

buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/'}
    }
    dependencies {
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.4, 0.99.99]'
    }
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'

repositories {
    maven { url 'https://maven.google.com' }
}

Add this to the dependencies section

dependencies {
    implementation 'com.onesignal:OneSignal:[3.11.2, 3.99.99]'
}

Add in the android -> defaultConfig

android {
   defaultConfig {
      manifestPlaceholders = [
          onesignal_app_id: 'PUT YOUR ONESIGNAL APP ID HERE',
          // Project number pulled from dashboard, local value is ignored.
          onesignal_google_project_number: 'REMOTE'
      ]
    }
 }

Sync dependencies with Gradle and finally put the required code in the onCreate method of the Application class

//KOTLIN
import com.onesignal.OneSignal

class YourAppClass : Application() {
   override fun onCreate() {
      super.onCreate()

      // OneSignal Initialization
      OneSignal.startInit(this)
         .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
         .unsubscribeWhenNotificationsAreDisabled(true)
         .init()
   }
}

//Java
import com.onesignal.OneSignal;

public class YourAppClass extends Application {
   @Override
   public void onCreate() {
      super.onCreate();

      // OneSignal Initialization
      OneSignal.startInit(this)
        .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
        .unsubscribeWhenNotificationsAreDisabled(true)
        .init();
   }
}

That's it!

Flutter SDK (Doc)

For the Flutter SDK there are some preliminary configuration steps to do to support Android or iOS, very similar to the steps that you have to do with native apps; for the Flutter part you have to add the OneSignal Flutter SDK to your project in the pubspec.yaml

dependencies:
    onesignal_flutter: ^2.0.0

Run flutter packages get to install dependencies, add the library in the main.dart

import 'package:onesignal_flutter/onesignal_flutter.dart'

and add the required code

OneSignal.shared.init(
  "your_onesignal_app_id_here",
  iOSSettings: {
    OSiOSSettings.autoPrompt: false,
    OSiOSSettings.inAppLaunchUrl: true
  }
);
OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification);

That's it!

OneSignal Dashboard

Once you created a new App in the OneSignal Dashboard, you have to select the platform that you want to use, in this simple case Android

Edit App

and insert the Firebase Server Key and Firebase Sender ID that you can generate in the firebase console

Firebase Console

Configure Platform

With this configuration you're able to send notification from the OneSignal console and receive them in the Android App.

My Architecture

As stated early in this post I send notification from my Node.js app using the OneSignal Rest API and the architecture of pretty much my App&Web is

App Architecture

  • Mobile Apps: Android/iOS/Flutter app where I put the SDK and use them to register to the OneSignal Service. With the registration I also add a tag with this super simple API to target the user/device lately and send custom and targeted notifications;
  • OneSignal: my OneSignal account where I create apps, manage Firebase key and Apple Certificates. In the OneSignal Dashboard you can review sended notifications, Subscribed Users, Monthly Active Users, etc and create custom Segments to divide users based on some criteria, for example: language, device type, location, country, number of session, etc or combine them to have useful insights about your audience;
  • Web App: Node.js app with all the business logic where I send Push Notification to the clients (apps).

Bonus

On the OneSignal Dashboard there's also a Delivery service where you can track the status of the notification and see how many users received it, click on it, if there are some errors and other cool stuff

Lista Notifiche Stats send Schermata 2019-11-25 alle 13.19.26.png

Good push, Alberto