Wednesday, 6 August 2014

facebook whatsapp notification എങ്ങനെയുണ്ടാക്കാമെന്ന് നോക്കാം( in android)


firstly നമുക്ക് ഒരു activity ഉണ്ടാക്കണം

ActivityNotification.java
-------------------------------
notification ഉണ്ടാക്കാന്‍ വേണ്ട രണ്ടു മഹാ സംഭവങ്ങളാണ് :p
1.Notification
2.NotificationManager

ഈ രണ്ടു ക്ലാസ്സുകളും വെച്ച് രണ്ടു object ആദ്യം ഉണ്ടാക്കണം

private Notification notificationobj;
private NotificationManager notificationmngr_obj;

ആദ്യം notification service ആക്റ്റീവ് ആക്കണം അതിനു വേണ്ടി യുള്ള കോഡ് ആണ്
notificationmngr_obj=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

ഇനി notification കൊടുക്കുമ്പോള്‍ വേണ്ട കാര്യങ്ങള്‍ പറഞ്ഞു കൊടുക്കാം
അതിനു notification initialize ചെയ്യണം
അതിനു വേണ്ടി
notificationobj=new Notification(R.drawable.ic_launcher, "hello", 0);

ഇതില്‍ കൊടുത്ത ഭാഗങ്ങള്‍ ഓരോന്നായി പഠിക്കാം

Notification(icon, tickerText, when)
--------------------------------------
notification നില്‍ നമുക്ക് ഒരു icon സെറ്റ് ചെയ്യാം
notification നില്‍ tickerText അതായത് ഒരു heading കൊടുക്കാം
notification നില്‍  when അതായത് എപ്പോള്‍ notification വേണമെന്ന് പറഞ്ഞു കൊടുക്കാം ഞാന്‍ ഇവിടെ 0(പൂജ്യം)ആണ് കൊടുക്കുന്നത് അതായത് 0second ആകുമ്പോള്‍ notification കിട്ടും

ഇത് കൂടാതെ notification നമ്പറും കൂടെ കൊടുക്കാം

notificationobj.number=10;

Intent intent=new Intent(context,Activity2.class);
PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, 0);
ഈ രണ്ടു ക്ലാസുകള്‍ Intent ഉം PendingIntent ഉം
ഉപയോഗിക്കാം Intent എന്താണെന്ന്  മുന്നേ പറഞ്ഞിട്ടുണ്ട് ആവശ്യമുള്ളവര്‍ ഇവിടെ ക്ലിക്ക് ചെയ്യുക
PendingIntent എന്താണെന്നു അടുത്ത പോസ്റ്റില്‍ പറയാം

ഇനി വേണ്ടത് ഈ കൊടുത്തിരിക്കുന്ന ഇന്‍ഫര്‍മേഷന്‍ തമ്മില്‍ connect ചെയ്യാം

notification heading
String hello="hello";
String msg="mamstricks.com";

notificationobj.setLatestEventInfo(context, hello, msg, pIntent);

ഇനി NotificationManager വെച്ച് notification വിളിക്കാം

notificationmngr_obj.notify(0, notificationobj);

ഇനി notify കൊടുത്ത 0 എന്താണെന്ന് പറയാം അതാണ്‌ notification id ഇത് വെച്ച് വേണമെങ്കില്‍ notification cancel ചെയ്യാം അല്ലെങ്കില്‍ destroy ചെയ്യാം
notify(0, notificationobj);
 ഇനി notificationഇല്‍ ക്ലിക്ക് ചെയ്താല്‍ അത് cancel ചെയ്യുവാനും തിരിച്ചു ആപില്‍ എത്തുവാനും ഈ കോഡ് ഉപയോഗിക്കാം

 notificationobj.flags |= Notification.FLAG_AUTO_CANCEL;

full code
******************************************

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class Activity2 extends Activity {

private Notification notificationobj;
private NotificationManager notificationmngr_obj;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
context=this;
}


public void viewNotify(View v){
String hello="hello";
String msg="messsage";
Intent intent=new Intent(context,Activity2.class);
PendingIntent pIntent=PendingIntent.getActivity(context, 0, intent, 0);
notificationmngr_obj=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


notificationobj=new Notification(R.drawable.ic_launcher, "hellook", 0);

notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);


 
   notificationobj.flags |= Notification.FLAG_AUTO_CANCEL;

  notificationmngr_obj.notify(0, notificationobj);
 
}
}
***********************************************************************
മറ്റു രീതികല്‍  കൂടെ നോക്കാം
ഇവിടെ കൊടുത്ത കോഡ്
എങ്ങനെ പല രീതിയില്‍ ഉപയോഗിക്കാമെന്ന് നോക്കാം
1.
/**************/
notificationobj=new Notification(R.drawable.ic_launcher, "hellook", 0);

notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);
/******************/
2.
/**************/
Notification notificationobj= new Notification.Builder(this)
       .setContentTitle("my title " + "mamstricks")
       .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
       .setContentIntent(pIntent)
       .addAction(R.drawable.ic_launcher, "ok", pIntent)
       .addAction(R.drawable.ic_launcher, "more info", pIntent)
       .addAction(R.drawable.ic_launcher, "And more info", pIntent).build();
   noti.number=10;
/**************/
3.
/**************/
notificationobj=new Notification();

notificationobj.icon=R.drawable.ic_launcher;
notificationobj.tickerText="hellook";
notificationobj.number=10;
notificationobj.setLatestEventInfo(context, hello, msg, pIntent);
/**************/


No comments:

Post a Comment