Android SDK Setup

Instructions for adding the OneSignal Android Mobile App SDK to your Android or Amazon Kindle Fire Apps using Android Studio.

Step 1. Requirements

Step 2. Add OneSignal Gradle Plugin and SDK

2.1 Open your root build.gradle (Project: name) file, add the following.

  • Under buildscript > repositories add (before jcenter)
    • mavenCentral()
    • gradlePluginPortal()
  • Under buildscript > dependencies add
    • classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'
  • Under allprojects > repositories add (before jcenter)
    • mavenCentral()
buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        jcenter()
    }
    dependencies {
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
}
1233

2.2 Open your App build.gradle (Module: app) file, add the following under buildscript {...} and above 'com.android.application'.

plugins {
    id 'com.onesignal.androidsdk.onesignal-gradle-plugin'
    // Other plugins here if pre-existing
}

2.3 Add the following to your dependencies section.

dependencies {
    implementation 'com.onesignal:OneSignal:[4.0.0, 4.99.99]'
}
1379

👍

Sync Gradle

Make sure to press "Sync Now" on the banner that pops up after saving!

Step 3. Add Required Code

3.1 Add the following to the onCreate method in your Application class.

If you don't have an Application class follow our Create Application Class Guide.

Note: The ONESIGNAL_APP_ID can be found in the dashboard Settings > Keys & IDs

import com.onesignal.OneSignal;

public class MainApplication extends Application {
    private static final String ONESIGNAL_APP_ID = "########-####-####-####-############";
  
    @Override
    public void onCreate() {
        super.onCreate();
        
        // Enable verbose OneSignal logging to debug issues if needed.
        OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
        
        // OneSignal Initialization
        OneSignal.initWithContext(this);
        OneSignal.setAppId(ONESIGNAL_APP_ID);
    }
}
import com.onesignal.OneSignal

const val ONESIGNAL_APP_ID = "########-####-####-####-############"
  
class MainApplication : Application() {
   override fun onCreate() {
      super.onCreate()
        
      // Logging set to help debug issues, remove before releasing your app.
      OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE)
      
      // OneSignal Initialization
      OneSignal.initWithContext(this)
      OneSignal.setAppId(ONESIGNAL_APP_ID)
   }
}

Step 4. Customize the default notification icons ( strongly recommended )

By default, notifications will be shown with a small bell icon in the notification shade. Follow the Customize Notification Icons guide to create your own small and large notification icons for your app.

744

Step 5. Run and test your app

Run your app on a device or the Android emulator to make sure your device is subscribed to notifications and can receive notifications sent from the OneSignal dashboard.

📘

Troubleshooting

If you run into any issues please see our Android troubleshooting guide

You can see a fully implemented example project on Github.

Step 6. Customize what your app does when a notification is clicked or received (Optional)

Notification Handlers

setNotificationWillShowInForegroundHandler - This is called before the notification is displayed while the app is in foreground.

setNotificationOpenedHandler - This is called after the notification is opened.

NotificationServiceExtension

Set up the NotificationServiceExtension if you want to do one of the following:

  • Receive data in the background with or without displaying a notification.
  • Override specific notification settings depending on client side app logic such as custom accent color, vibration pattern, or other any other NotificationCompat options available.

Step 7. Implement other OneSignal features

  • Notification Delivery Settings - Learn about the many customizations and methods you can use to send notifications to your users in our Sending Push Messages guide.

  • Automated Messages - Set up Automated Messages to automatically re-engage app users who have lapsed or abandoned their cart. Learn more in our Automated Messages guide.

  • Segments and Tags - OneSignal supports simple and powerful tagging and segmentation to send messages to relevant users through our dashboard and API. Learn more in our Segments guide.

  • In-App Messages - OneSignal supports In-App Messaging in order to display rich content to your users or to present permission prompts, surveys, promotions, announcements, and more. Learn more in our In-App Messaging Overview.

  • E-Mail Support - OneSignal supports the delivery and automation of e-mail in addition to push notifications. Learn more in our Email Overview.

  • Android SDK API - Explore other methods available in our Android SDK in Android Native SDK API Documentation.

  • Remote API - Send notifications, import data, and export data using our simple and powerful API. Learn more in our OneSignal API overview..