How to add onesignal push notification to android app ?

onesignal

Implementing a push notification service to your android application is a good idea to increase user engagement and thus better app ranking in playstore.

There are a many push notification services available for android.

  • Firebase  –  SERVICE BY GOOGLE
  • Urbanship
  • PushWoosh
  • Pushbots
  • Onesignal

etc……..

I had experience with pushbots and  onesignal push notification

Onesignal push notification service is completely free and pushbots has limited free service.It will ask you to move to paid plans once you reached 10000 api requests.

I’m going to recommend onesignal and firebase . And in this article we concentrate on onesignal push notification service, as i said earlier its a free service.


Main feature of onesignal push notification service are

  • Unlimited Devices
  • Delivery Automation
  • Localization
  • Full API
  • Unlimited Segments
  • A/B Testing
  • Delivery Scheduling
  • Import & Export Your Data

They also offer premium support options if you want. And they have services for other platforms like iphone, web, amazon, unity, phonegap, corodova, corona etc. So its a one stop location for all your push notification needs, if you port your application in future for other platforms.

Free service !  then how do they make money ? . The business model that followed by onesignal push notification service is a bit different  they collect user information and sell it to advertisers and analytical companies

PS : Make sure you have a good privacy policy regarding  third party data sharing

Implementation  onesignal push notification using Android studio :

Step 1

First you have to generate a Google Server API Key and Google Project Number which are used to send push notifications to ANDROID devices.

For that Go to  this link Google Services Wizard.

home

Enter your application name and package name.

Click Continue.

ujyiyui

Click – “Enable google cloud messaging”

Now google generate a Server API Key and Sender ID for your app

Save them safe – We need them in our code

uiyiy

Step 2

Now Move to onesignal.com and register an account.

After successfully login to your account Click “Add a new app”

Enter your app name then select android platform

rtretret

Click next

fdfsdfsf

Now enter

Google Server API Key and  Google Project Number we generated earlier

Click save

hjghjgj

Select native android and hit next

tyutu

 

Now you get a window like this.  Save your app id.

Hold it there and now move to our code in Android studio.

Open your build.gradle (Module: app) file and insert these dependencies.

 

dependencies {
    compile 'com.onesignal:OneSignal:[email protected]'
    
    // Required for OneSignal, even if you have added FCM.
    compile 'com.google.android.gms:play-services-gcm:+'
    
    // Required for geotagging
    compile "com.google.android.gms:play-services-location:+"
    
    // play-services-analytics is only needed when using 8.1.0 or older. 
    // compile 'com.google.android.gms:play-services-analytics:+'
}

 

This is what instructed by onesignal  but i personally ignore this line

// Required for geotagging
    compile "com.google.android.gms:play-services-location:+"

when my apps not requires location services

Again in your build.gradle (Module: app) add these codes

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"]
    }
 }

Replace “PUT YOUR ONESIGNAL APP ID HERE” line with  the App id generated by   Onesignal    (Looks like this — 8f8a37ad-cde9-4e00-95d1-2b1a7b97f891)

And

"REMOTE" with your google project number (Sender ID) you generated in Google developer console (Looks like this - 821236548745

Step 3

Add the following code to the onCreate method in your Applications “Main activity”.

import com.onesignal.OneSignal;

public class YourAppClass extends Application {
   @Override
   public void onCreate() {
      super.onCreate();
      OneSignal.startInit(this).init();
     
      // Sync hashed email if you have a login system or collect it.
      //   Will be used to reach the user at the most optimal time of day.
      // OneSignal.syncHashedEmail(userEmail);
   }
}

Step 4

Now build and run your application (Make sure it is connected to internet)

fhyhryht

Now click “Check subscribed users”. if it shows your device, Then you are good to go.

Click  Done.

My recommendations 

From Android Lollipop 5.0 onwards  android only support small white icons for push notification. If you want to use icon with color then change applications  Targeted SDK to pre lollipop that is <21

targetSdkVersion 20

it is only recommended if you don't use lollipop specific features
And don't forget to add a small icon with name "ic_stat_onesignal_default"

to these folders ( i usually add a (72×72)  icon to res/drawable-mdpi/ folder and skip others. )

res/drawable-mdpi/   -  (24×24)    resolution

res/drawable-hdpi/   - (36×36)     resolution

res/drawable-xhdpi/  - (48×48)     resolution

res/drawable-xxhdpi/ - (72×72)      resolution

res/drawable-xxxhdpi/- (256×256) (Large Icon)  resolution