How to create a push notification in Android studio
Hi friends, in this tutorial how to create push notification in Android for any android Apps. All of you know Push notification has become very important features for many latest Android Application. As you know parse is going to close soon we need to move to another backend website for sending push notification. One such alternatives are Onesignal.com. Using this we can send image notification and also give you the clear statistics about push notification.

Prerequisites:
- JDK 7.0 or Above
- Android Studio 2.0
- GCM Project ID and Server Api Key
- OneSignal.com Account (Signup Here Its free)
Steps to follow
Step 1: Create a New Android Studio project name Androidpush.
Step 2: Select minimum API level15 so that it can support maximum of Android Device available in the google play.
Step 3: select blank Activity andGive your layout xml name is activity_main And click finish .
Step 4: In project structure go to app and open build.gradle and add below lines in your dependencies.
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.android.gms:play-services-ads:8.1.0'
compile 'com.google.android.gms:play-services-identity:8.1.0'
compile 'com.google.android.gms:play-services-gcm:8.1.0'
compile 'com.onesignal:OneSignal:2.+@aar'
compile 'com.google.android.gms:play-services-analytics:8.1.0'
compile 'com.google.android.gms:play-services-location:8.1.0'
compile 'com.google.android.gms:play-services-ads:8.1.0'
compile 'com.google.android.gms:play-services-identity:8.1.0'
compile 'com.google.android.gms:play-services-gcm:8.1.0'
compile 'com.onesignal:OneSignal:2.+@aar'
compile 'com.google.android.gms:play-services-analytics:8.1.0'
compile 'com.google.android.gms:play-services-location:8.1.0'
Step 6: Then add following lines to you defaultconfig in build.gradle below the versionName.
manifestPlaceholders = [manifestApplicationId: "${applicationId}",
onesignal_app_id: "OneSignal App ID",
onesignal_google_project_number: "GCM Project ID"]
onesignal_app_id: "OneSignal App ID",
onesignal_google_project_number: "GCM Project ID"]
step 7: Now replace OneSignal App ID in above code with One Signal App ID
Also replace GCM Project ID with you ID .
Step 8:Then Open Your Main Activity.java and add below lines of under onCreate method below super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
Step 9: Add the following method to MainActivity
// This fires when a notification is opened by tapping on it or one is received while the app is running.
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
@Override
public void notificationOpened(String message, JSONObjectadditionalData, booleanisActive) {
try {
if (additionalData != null) {
if (additionalData.has("actionSelected"))
Log.d("OneSignalExample", "OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");
Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
}
} catch (Throwable t) {
t.printStackTrace();
}
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
@Override
public void notificationOpened(String message, JSONObjectadditionalData, booleanisActive) {
try {
if (additionalData != null) {
if (additionalData.has("actionSelected"))
Log.d("OneSignalExample", "OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");
Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
}
} catch (Throwable t) {
t.printStackTrace();
}
Step 10: rebuild the Project and make sure it rebuilds the project without any errors. Now setup backend in your OneSignal.com dashboard for sending push notifications to android app

You may also like
Comments ( 1 )

moha hashemi:
TNX :)
TNX :)

Admin:
its ok
its ok
Subscribe Latest Information
Categories
Most Popular Posts
How to Withdraw Money from ATM Machine 7steps 1178803 Views
How to Create Chat Application in Android Studio 151644 Views
How to Create a Shopping Cart Application in Android 114503 Views
You May Like Also
How to make Realtime chat Application in Android 24919 Views
Android Material Design Snackbar tutorial 4848 Views
How to Create Overflow Menus in Android 14122 Views