Push Notification Delivery Using One Signal In Android - Part Two

Read the first part of this article from the following link.

Introduction

 
One Signal notification is one hundred percent free and gives unlimited access for mobile and web applications. One Signal offers cross-platform support and is a secure and reliable push notification delivery service.
 
In this article, I will show you how to deliver push notifications using One Signal in Android application. Android is a kernel-based operating system in which users can modify the GUI components and source code.
 
Requirements
  • Android Studio
  • A little knowledge of XML and Java.
  • Android Emulator (or) Android mobile
  • Stable internet connection during execution.
  • Download link (Android Studio)
Steps to be followed
 
Carefully follow the below steps to enable push notification delivery using One Signal in your Android application. I have included the source code too for download.
 
Step 1
 
Go to app, open build.gradle, and add the below lines in your dependencies.
  1. compile 'com.android.support:appcompat-v7:25.3.1'  
  2. compile 'com.android.support.constraint:constraint-layout:1.0.2'  
  3. compile 'com.google.android.gms:play-services:8.1.0'  
  4. compile 'com.google.android.gms:play-services-ads:8.1.0'  
  5. compile 'com.google.android.gms:play-services-identity:8.1.0'  
  6. compile 'com.google.android.gms:play-services-gcm:8.1.0'  
  7. compile 'com.onesignal:OneSignal:2.+@aar'  
  8. compile 'com.google.android.gms:play-services-analytics:8.1.0'  
  9. compile 'com.google.android.gms:play-services-location:8.1.0'  
  10. testCompile 'junit:junit:4.12'  
Android
 
Step 2
 
Then, add the below lines to you defaultconfig in build.gradle below the versionName.
  1. manifestPlaceholders = [manifestApplicationId: "${applicationId}",  
  2.                        onesignal_app_id: "OneSignal App ID",  
  3.                        onesignal_google_project_number: "GCM Project ID"]  
  4. estInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  
Android
 
Step 3
 
Go to the below link and click Login button.
Link: https://onesignal.com/#pricing
 
Android
 
Step 4
 
Login via your Gmail Id.
 
Android
 
Step 5
 
Type your app and click "Create".
 
Android
 
Step 6
 
Open your project, go to "App Settings", and click "Configure" in Google Android.
 
Android
 
Step 7
 
Copy your Google server and sender key from Firebase account and paste that key in One Signal Settings; then click "Save".
 
Android
 
Step 8
 
Copy your One Signal app id and paste that into the build.gradle file. After that, click on "Synchronize" the project.
 
Android
 
Step 9
 
In the MainActivity.java file, copy and paste the below code. Do not replace your package name otherwise, the app will not run.
 
MainActivity.java code
  1. package ganeshannt.onesignal;    
  2.     
  3. import android.os.Bundle;    
  4. import android.support.v7.app.AppCompatActivity;    
  5. import android.util.Log;    
  6.     
  7. import com.onesignal.OneSignal;    
  8.     
  9. import org.json.JSONObject;    
  10.     
  11. public class MainActivity extends AppCompatActivity {    
  12.     
  13.     @Override    
  14.     protected void onCreate(Bundle savedInstanceState) {    
  15.         super.onCreate(savedInstanceState);    
  16.         OneSignal.startInit(this).setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())    
  17. .init();    
  18.         setContentView(R.layout.activity_main);    
  19.     }    
  20.     
  21.     // This fires when a notification is opened by tapping on it or one is received while the app is running.    
  22.     private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {    
  23. @Override    
  24. public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {    
  25. try {    
  26. if (additionalData != null) {    
  27. if (additionalData.has("actionSelected"))    
  28.  Log.d("OneSignalExample""OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");    
  29.     
  30. Log.d("OneSignalExample""Full additionalData:\n" + additionalData.toString());    
  31. }} catch (Throwable t) {    
  32. t.printStackTrace();    
  33. }    
  34. }}}  
Step 10
 
Click the "Make Project" option and run the app.
 
Android
 
Deliverables
 
Here, we have successfully created and executed the Push Notification Delivery, using One Signal in Android application.
 
Android
 
Android
 
Android
 
Android
 
Android
 
Thank you for reading my article.
 
Don’t forget to like and follow me. If you have any doubts, just comment below.