
This tutorial will show you how to add push notification functionality in your an Android app to send and receive push notifications.
Step -1 : Create New Android application and give name MainActivity to main activity and same name for layout file.
- minSdkVersion="11”
- targetSdkVersion="20"
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/padding_normal" android:paddingRight="@dimen/padding_normal" android:paddingTop="?attr/actionBarSize" tools:context="com.androprogrammer.tutorials.samples.PushNotificationDemo"> <EditText android:id="@+id/nd_etHeading" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:hint="Notification Heading" android:inputType="text"/> <EditText android:id="@+id/nd_notificationbody" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/nd_etHeading" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:hint="Notification Body" android:ems="10" android:inputType="textMultiLine"/> <Button android:id="@+id/nd_smallnotification" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="@dimen/padding_normal" android:text="Create Small Notification"/> <Button android:id="@+id/nd_bignotification" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/nd_smallnotification" android:layout_margin="@dimen/padding_normal" android:text="Create Big Notification"/> </RelativeLayout>
In the below class file i have take care of which android version it is so it won't get crashed in older Android version. i have used app compat library which allow to create big notification on any device. Now first of all you are require to get system service for creating notification. so get it using getSystemService() method of context.
on small notification button click simple style notification is get created and pending intent will add that intent in intent pool. so system will notify user with NotificationManager.notify() method.
In other case big notification will be created by second button. in which the big message which will be displayed in notification will be store in Notification.InboxStyle object. that object can store big message , big image (like Screen shot image) etc.
Step -2 : Hope it will be clear to you. So just now put the below code in Main.java file.
Main.java
package com.androprogrammer.tutorials.samples; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Build; import android.support.v4.app.NotificationCompat; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.androprogrammer.tutorials.R; import com.androprogrammer.tutorials.activities.Baseactivity; public class PushNotificationDemo extends Baseactivity implements View.OnClickListener { protected View view; protected Button notification_small, notification_big; protected EditText heading, message; protected String heading_text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setReference(); setToolbarSubTittle(this.getClass().getSimpleName()); setToolbarElevation(7); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public void setReference() { view = LayoutInflater.from(this).inflate(R.layout.activity_pushnotification_demo, container); // TO set Reference to the xml button & edit text object. notification_small = (Button) view.findViewById(R.id.nd_smallnotification); notification_big = (Button) view.findViewById(R.id.nd_bignotification); notification_big.setOnClickListener(this); notification_small.setOnClickListener(this); heading = (EditText) view.findViewById(R.id.nd_etHeading); message = (EditText) view.findViewById(R.id.nd_notificationbody); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: finish(); break; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), PushNotificationDemo.class); final PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification n; switch (v.getId()) { case R.id.nd_smallnotification: if (heading.getText().length() <= 0) { heading.setError("Please provide push notification title"); } else { heading.setError(null); heading_text = heading.getText().toString(); n = new NotificationCompat.Builder(this) .setContentTitle(heading_text) .setContentText(message.getText().toString()) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pending) .setDefaults(Notification.DEFAULT_SOUND) .setAutoCancel(true) .setWhen(System.currentTimeMillis()) .addAction(android.R.drawable.ic_menu_manage, "Close", pending) .build(); n.flags |= Notification.FLAG_AUTO_CANCEL | Intent.FLAG_ACTIVITY_SINGLE_TOP; notificationManager.notify(0, n); } break; case R.id.nd_bignotification: if (heading.getText().length() <= 0) { heading.setError("Please provide push notification title"); } else { heading.setError(null); heading_text = heading.getText().toString(); NotificationCompat.InboxStyle nc = new NotificationCompat.InboxStyle(); nc.setBigContentTitle(heading_text); String[] text = new String[5]; for (int i = 0; i < 5; i++) { text[i] = i + ". say hello..."; nc.addLine(text[i]); } n = new NotificationCompat.Builder(this) .setStyle(nc) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pending) .setDefaults(Notification.DEFAULT_SOUND) .setAutoCancel(true) .setWhen(System.currentTimeMillis()) .build(); n.flags |= Notification.FLAG_AUTO_CANCEL | Intent.FLAG_ACTIVITY_SINGLE_TOP; notificationManager.notify(100, n); } break; } } }
Step - 3 :Now run your app and write some text for heading and body part of notification. as you can see in below screen shot i have shown both notification type in it.
Screen Shot
if its still not clear to you or have some query ask me in below comment box.if you like it share this tutorial with your friends.
Get code from Github
Keep coding...
0 comments :
Post a Comment