Saturday, 26 April 2014

Android Custom Push Notifications


Custom Push Notifications 


In Android Push Notifications, it often happens that the a complete message/text is not visible. For small message/text it is ok but in case of longer message/text it is necessary to show complete message/text on a single push notification. It can be easily done by creating custom push notification in android. By using custom notification it does not matter how longer message/text you want to display on a single notification.

For custom push notification, first you need to create a custom layout. Here is sample layout which you can use or create your own custom layout for push notification. Then you can use this custom layout for notification.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="100dp"
      android:gravity="center_vertical"
      android:background="#f0f0f0">
    <ImageView
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:id="@+id/image"
        android:layout_marginLeft="10dip"
        android:layout_centerVertical="true"
        android:contentDescription="icon" />
    <LinearLayout
        android:gravity="center_vertical"
        android:layout_height="match_parent"
        android:layout_width="fill_parent"
        android:layout_toRightOf="@+id/image"
        android:orientation="vertical">
          <TextView
            android:text="TextView" 
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:textSize="14sp"        
            android:id="@+id/title"
            android:textColor="#3D3A34"
            android:layout_marginLeft="10dip"/> 
          <TextView
            android:gravity="center_vertical"
            android:text="TextView" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:textSize="12sp"
            android:id="@+id/text"
            android:textColor="#3D3A34"
            android:layout_marginLeft="10dip"
            android:singleLine="false"
            android:maxLines="8"/>              
    </LinearLayout>
</RelativeLayout>

Save this layout with "custom_notification.xml". This layout will be used in GCMIntentService Class.
After creating custom layout, create a method in GCMIntentService class as given below:

    private static void generateCustomNotification(Context context,
            String message, String title) {
               
        try{            
           
  int icon = R.drawable.applogo; // this is the icon which you can set for notification, I set applogo as icon.
            long when = System.currentTimeMillis();
            RemoteViews contentView = new RemoteViews("com.yourpackname", R.layout.custom_notification);// custom_notification.xml layout used here

            contentView.setImageViewResource(R.id.image,icon);
            contentView.setTextViewText(R.id.title,  title); // title of push notification
            contentView.setTextViewText(R.id.text,  message); //// message/text of push notification

            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(icon, message, when);
          
            Intent notificationIntent = new Intent(context, YourActivity.class); //YourActivity will be activity of your project where you want to redirect user when he/she taps on push notification.
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // set intent so it does not start a new activity 
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
            PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
            notification.contentIntent = intent ;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.flags |= Notification.FLAG_SHOW_LIGHTS;
            notification.defaults |= Notification.DEFAULT_VIBRATE; //Whenever a push notification receives, phone vibrates. For this, you need Vibrate permission in Manifest file. Otherwise, app crashes.
            notification.defaults |= Notification.DEFAULT_LIGHTS;
            notification.contentView = contentView;
            notificationManager.notify(counter, notification); //static int counter = 0; this counter will be used if you send multiple push notifications so each notification displays as separate notification.
            counter ++;
        }catch (Exception e) {
           
        }
    }



That's all about Custom push notification. 


Thanks visiting my blog. Please don't forget to give your feedback.
I welcome your valuable suggestions.

Friday, 18 October 2013

Android Google Cloud Messaging: Register Android Project



Creating Android Key (API Key)


Before to start developing android push notification app, you need access to Google APIs and register your android project. Here is detailed procedure to register android project and creating android key, please follow as described below:

Step#1

Ente the URL: https://code.google.com/apis/console/‎, it asks for sign-in. Enter gmail account credentials and sign-in. As you sign-in, following screen will appear:
  








Step#2 

Click on API Project from left pane and then select ‘Create’, as: Enter the project name and click Create Project. 


Step#3

After creating project, Dashboard appeared as: Note the project Number; it will be used on GCM Client side (Android Side) as SENDER_ID.

 


Step#4

Click on Services tab from left pane, a list of services will be displayed. Most of services are Off by default. You can choose a number of service(s) according to your requirement and then switch ON. For push notification you need to ON Google Cloud Messaging Service as under:

 

Step#5

After activating GCM service,  click on API Access from left pane and click on Create new Android Key as under:
 




Step#6
 
A pop-up window will appear as under: enter the required parameters and then click on Create button.

 

Step#7


Finally, API key has been successfully generated as under: This API Key will be used on GCM Server Side code as APIKey.

 


This is all, what you need to do for API Key.