Implement Local Notification in Android

Create class LocalService1.Java in src...


public class localservice1 extends Service {
    private Timer timer;
    ArrayList<MessageModel> arrMessage;
    static SharedPreferences prefs;
    CharSequence contentText = null;
    
    private TimerTask updateTask = new TimerTask() {

        @Override
        public void run() {
            Log.i("msg", "Timer task doing work!");

           

        }
    };

    private final Handler handler = new Handler();
    private final Runnable drawRunner = new Runnable() {
        public void run() {
            draw();
        }

    };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        // code to execute when the service is first created
        Log.i("Service created..", "yes");
    }

    @Override
    public void onDestroy() {
        // code to execute when the service is shutting down
       
     
        handler.removeCallbacks(drawRunner);

    }

    @Override
    public void onStart(Intent intent, int startid) {
        // code to execute when the service is starting up
       
        handler.post(drawRunner);

    }

    public void draw() {

       sendNotification("You have new message");
        handler.postDelayed(drawRunner, 60*60*24 * 1000);

    }


    public void sendNotification(CharSequence message) {

        // Execute Check and Notify
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = message;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "Title";
        contentText = message;
        Intent notificationIntent = new Intent(this, NotificationActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        int HELLO_ID = 1;
        mNotificationManager.notify(HELLO_ID, notification);
    }

}

add following tag in AndroidManifest.xml

 <service android:enabled="true" android:name=".localservice1" />


Average Rating (0)
by Prabodh   Kanani  in Android  on 4/1/2015 12:16:45 AM  


Post Your Comment
*