Skip to content

OneSignalDevelopers/onesignal-android-sample

Repository files navigation

Quickstart   •   Website   •   Docs   •   Examples

OneSignal

OneSignal Android Sample

OneSignal makes engaging customers simple and is the fastest, most reliable service to send push notifications, in-app messages, SMS, and emails.

This project demonstrates integrating the OneSignal SDK into an Android project built with Kotlin. You can use this project as a boilerplate or reference to start your project.

🚦 Getting started

This project assumes that you already have a OneSignal app created with push notifications setup. If you don't yet have a OneSignal app, create one first, then follow the steps below to integrate the OneSignal SDK into your Android or iOS app.

Requirements

  • Android Studio Chipmunk.
  • OneSignal account and project.
  • Firebase Server Key.
  • Physical or virtual device.

Usage

To test this example, clone this repository as follows:

git clone https://github.com/OneSignalDevelopers/OneSignal-Android-Push-Sample

In Android Studio:

  1. Open the project.
  2. Click the green play icon to run the app.

Initialize OneSignal

class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
OneSignal.Debug.logLevel = LogLevel.VERBOSE
OneSignal.initWithContext(this, ONESIGNAL_APP_ID)
}
}

Enable push subscription

findViewById<Button>(R.id.enable_push).setOnClickListener {
OneSignal.User.pushSubscription.optIn()
}

Send push notification

## Send notification
# https://documentation.onesignal.com/reference/create-notification
curl -X "POST" "http://notifications" \
    -H 'Content-Type: application/json' \
    -u '<Your REST API Key>:' \
    -d $'{
  "headings": {
    "en": "🥳🍾🎊"
  },
  "included_segments": [
    "Subscribed Users"
  ],
  "app_id": "<Your APP ID>",
  "contents": {
    "en": "You recieved your first push!"
  },
  "url": "https://onesignal.com"
}'

Trigger In-App Message

1. Create a new In-App Message

Navigate to Messages and select In-App from the submenu...

e3e65fc86de0dfb1d34a26a343f82416

Choose New Message button...

901a597c23b287d4bb2f5239548e0e80

Choose New In-App from the dropdown.

2. Configure In-App Message

Name the In-App Message "Sample IAM"...

ca0c5009187761be3f10950ddd31ca0c

Configure the In-App Message to be presented to All Users...

ef8bcd8f47903fa17f81551cbe7129c0

Use the block editor to create the content of your In-App Message...

2aec0445942d753e151f455e0814c30e

Configure the In-App Message to trigger when a certain condition is met i.e., show_sample_iam is 1...

26969694a431ea236309c9ec0dca6f4e

Schedule the In-App Message to be presented immediately, to show until dismissed by the user, and to present itself everytime the trigger contions is met. Then choose Make Message Live...

3d409d47880a9fd0fb7b63f4f89bacfe

Review configuration and confirm when correct.

3. Launch In-App Message

Tap the Present In-App Message button in the sample app to present In-App Message.

Support

Ask a question about OneSignal

You can ask questions about the OneSignal xxx SDK and related topics in the onesignal-xxx-sdk repository.

🙋‍♂️ Ask a question

Create a bug report

If you receive an error message or get blocked by an issue, please create a bug report!

🪳 Create bug report

❤️ Developer Community

For additional resources, please join the OneSignal Developer Community.

Get in touch with us or learn more about OneSignal through the channels below.

Show your support

Give a ⭐️ if this project helped you, and watch this repo to stay up to date.