Friday, 5 December 2014

Push Notification

Main Java Class

Comman.GCMrequest(MainActivity.this);




Comman Java Calss

public class Comman {


public static String senderId = "736058361115";
public static String deviceID="";
public static Context contex;

public static void GCMrequest(Context context)
{
contex = context;
Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.putExtra("app",PendingIntent.getBroadcast(context, 0, new Intent(), 0));
// Sender id - project ID generated when signing up
intent.putExtra("sender", Comman.senderId);
intent.putExtra("os_type", "android");
        context.startService(intent);

}

}


Broadcast Java Class


public class MyBroadCastclass extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

Log.v("Brod Cast ", "##################################### BrodCast class #######################");
Log.v("Device Id", "11111  : "+ intent.getStringExtra("registration_id"));
MyIntentClass.runIntentService(context, intent);
setResult(Activity.RESULT_OK, null, null);

}
}


Massage Handling Java Class


public class MyIntentClass extends IntentService{




String TAG = "GCM";
private int NOTIFICATION_ID;
private String senderId=Comman.senderId;

public MyIntentClass()
{
super("My service");
}

public MyIntentClass(String name) {
super(name);
// TODO Auto-generated constructor stub
}


@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub


try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION"))
{
handleRegistration(MyIntentClass.this,intent);

}
else if (action.equals("com.google.android.c2dm.intent.RECEIVE"))
{
handleMessage(MyIntentClass.this,intent);
}
else if (action.equals("com.google.android.c2dm.intent.RETRY")) 
{
Comman.GCMrequest(MyIntentClass.this);
}
}
catch(Exception e)
{

}


}

private void handleMessage(Context context, Intent intent)
{

String data = intent.getExtras().getString("data");
createNotification(context,data);

}

public void createNotification(Context context, String data) {


NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"Demo Notification "+data, System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults|=Notification.DEFAULT_SOUND;
Intent intent  = new Intent(context, RemindActivity.class);
intent.putExtra("data",data);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
intent.setAction("actionstring" + System.currentTimeMillis());
int notif = NOTIFICATION_ID++;
Log.i("Notif==",""+notif);
PendingIntent pendingIntent = PendingIntent.getActivity(context, notif,intent, PendingIntent.FLAG_UPDATE_CURRENT |  PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context,"Demo Notification ",""+data, pendingIntent);
notificationManager.notify(notif, notification);

}


public static void runIntentService(Context context ,Intent intent)
{


Log.v("MyIntent ", "*********** runIntentService **************************************");
intent.setClassName(context, MyIntentClass.class.getName());
context.startService(intent);

}


private void handleRegistration(Context context, Intent intent)
{
String registration = intent.getStringExtra("registration_id");

if (intent.getStringExtra("error") != null)
{
// Registration failed, should try again later.
Log.d("GCM", "registration failed");

String error = intent.getStringExtra("error");

if(error == "SERVICE_NOT_AVAILABLE"){
Log.d("GCM", "SERVICE_NOT_AVAILABLE");
}else if(error == "ACCOUNT_MISSING"){
Log.d("GCM", "ACCOUNT_MISSING");
}else if(error == "AUTHENTICATION_FAILED"){
Log.d("GCM", "AUTHENTICATION_FAILED");
}else if(error == "TOO_MANY_REGISTRATIONS"){
Log.d("GCM", "TOO_MANY_REGISTRATIONS");
}else if(error == "INVALID_SENDER"){
Log.d("GCM", "INVALID_SENDER");
}else if(error == "PHONE_REGISTRATION_ERROR"){
Log.d("GCM", "PHONE_REGISTRATION_ERROR");
}else
{
Log.d("GCM", "REGISTRATION_ERROR");
}

}

else if (registration != null)
{
Log.d("GCM registration ! null", registration);
saveRegistrationId(context,registration);
sendRegistrationIdToServer(registration);
}
}


// Save Ragistation Id
private void saveRegistrationId(Context context, String registrationId) {

Log.v("GCM saveregistration id : ", registrationId);
Comman.deviceID = registrationId;
}


//Send Device token to server 
public void sendRegistrationIdToServer(String registrationId)
{

}

}


Remind Java Class


public class RemindActivity extends Activity{

TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.data);
}
}


ManiFest XML


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notificationdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />

    <permission
        android:name="com.credencys.consignmentapp.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.aditmicrosys.notificationdemo.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.aditmicrosys.notificationdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.aditmicrosys.notificationdemo.RemindActivity"></activity>
        <receiver android:name="com.aditmicrosys.notificationdemo.MyBroadCastclass"
             android:permission="com.google.android.c2dm.permission.SEND" 
            >
            
            
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.credencys.consignmentapp" />
            </intent-filter>
            <!-- Receive the registration id -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.credencys.consignmentapp" />
            </intent-filter>
            
        </receiver>
        
        <service android:name="com.aditmicrosys.notificationdemo.MyIntentClass" />
    </application>

</manifest>


No comments:

Post a Comment