Mar 13, 2014

Notification



Notification is useful class in android.

You can see easily notification in your phone when if you received SMS or email, etc...

This is my code.

  1. NotificationManager nm=(NotificationManager)mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
  2. int icon=setIcon(weathericon);
  3. Notification notification =new Notification(icon,"날씨가 업데이트 되었습니다",System.currentTimeMillis());
  4. PendingIntent pi=PendingIntent.getActivity(mContext, 0new Intent(mContext,MainActivity.class),0);
  5. if(dbnotify.equals("notifyUse")){
  6.     notification.flags=Notification.FLAG_AUTO_CANCEL;
  7. }else if(dbnotify.equals("notifyOngoing")){
  8.     notification.flags=Notification.FLAG_ONGOING_EVENT;
  9. }
  10. notification.setLatestEventInfo(mContext, name, weather, pi);
  11. nm.notify(notifyId,notification);

I not use vibration& sound. If you will use vibration& sound that add this code

  1. notification.flags=Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;


 You can see to above notification bar "날씨가 업데이트 되었습니다." 

  1. Notification notification =new Notification(icon,"날씨가 업데이트 되었습니다",System.currentTimeMillis());
The code making that notification bar.

Notification was deprecated but possibly to use. 

If you down scroll notification bar that can see this picture.

  1. PendingIntent pi=PendingIntent.getActivity(mContext, 0new Intent(mContext,MainActivity.class),0);
  2. notification.setLatestEventInfo(mContext, name, weather, pi);
Pending intent is set will moving activity when you touched notification.

setLatesetEventinfo is notification data.

  1. notification.flags=Notification.FLAG_AUTO_CANCEL;
This flag is notification property.

If you want ongoing mode that need this code

notification.flags=Notification.FLAG_ONGOING_EVENT;







No comments:

Post a Comment