-4

I have an android application that use SQLite database to store data. One of its field is match_date that contain the date with time.

What I want is to create notification that include the team names and the date after comparing the current date with the stored date in the database and if they match a notification must appear.

The application work perfectly but without getting any notification so where is my error an how to fix this error?

I will appreciate any help.

This is my database table:

CREATE TABLE [match_list] (
  [_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
  [stad_name] VARCHAR(50) NOT NULL, 
  [team1] VARCHAR(50) NOT NULL, 
  [team2] VARCHAR(50) NOT NULL, 
  [stage] varchar(50) NOT NULL, 
  [match_date] DATETIME NOT NULL, 
  [flags1] CHAR(255), 
  [flags2] CHAR(255), 
  [group_team] CHAR(1));

FirstActivity.java:

public class FirstActivity extends Activity {

    TextView txtDate;

    private SQLiteDatabase database;
    private ExternalDbOpenHelper extDB;

    private static final String DB_NAME = "world_cup.db";
    // *****Tables name**************************//
    private static final String TABLE_NAME = "match_list";

    // *****Tbale name******************************

    public static final String PLACE_ID = "_id";
    public static final String STAD_NAME = "stad_name";
    public static final String TEAM1 = "team1";
    public static final String TEAM2 = "team2";
    public static final String MATCH_DATE = "match_date";
    public static final String FLAG1 = "flags1";
    public static final String FLAG2 = "flags2";

    public static final String[] ALL_KEYS = new String[] { PLACE_ID, STAD_NAME,
            TEAM1, TEAM2, MATCH_DATE, FLAG1, FLAG2, };
    // *****Tbale name******************************

    ItemDetails listdetail;

    Cursor c;

    private static final int myNotificationId = 1;


    ///////////////////////
    long diffSeconds;
    long diffMinutes;
    long diffHours;
    long diffDays;
    ///////////////////////

    //for the alarm amanager and the notification
    public static int eventID = 1;

    Date d1 = new Date();

    SimpleDateFormat format;

    //////////////////////////////////////////////

    // private DatabaseHelper dbHelper = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        getDiffDate();

        extDB = new ExternalDbOpenHelper(this, DB_NAME);
        database = extDB.openDataBase();

        showNotification();

         Log.e("inside the onCreate", "inside onCreate");


    }


    public void listTeams(View v) {
        Intent in = new Intent(getBaseContext(), MainActivity.class);
        startActivity(in);
    }

    public void listGroups(View v) {
        Intent in = new Intent(getBaseContext(), GroupList.class);
        startActivity(in);
    }

    public void DisplaySched(View v) {

        Intent in = new Intent(getBaseContext(), MatchScheduleList.class);
        startActivity(in);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.first, menu);
        return true;
    }


      public void getDiffDate() {

        String dateStop = "06/12/2014 23:00:00";

        // HH converts hour in 24 hours format (0-23), day calculation
        format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        d1 = new Date();


        Date d2 = null;

        try {

            d2 = format.parse(dateStop);

            // in milliseconds
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            System.out.print(diffDays + " days, ");
            System.out.print(diffHours + " hours, ");
            System.out.print(diffMinutes + " minutes, ");
            System.out.print(diffSeconds + " seconds.");

            txtDate = (TextView) findViewById(R.id.txtDifDate);
            String resultDate = diffDays + "days " + diffHours + "Hours "
                    + diffMinutes + "Min " + diffSeconds + "S";
            txtDate.setText(resultDate);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public Cursor getAllRows(PendingIntent notificationIntent) {
        String where = null;

        c = database.query(true, TABLE_NAME, ALL_KEYS, where, null, null, null,
                null, null);
        if (c != null) {

            c.moveToFirst();


            while (!c.isAfterLast()) {
                listdetail = new ItemDetails();

                listdetail.setTeam1(c.getString(c.getColumnIndex("team1")));

                listdetail.setTeam2(c.getString(c.getColumnIndex("team2")));

                listdetail.setDate_match(c.getString(c
                        .getColumnIndex("match_date")));




                 // if the pick up date from the database is equal to the current date the notification will pop up 

                long i = (int) (new Date().getTime()/1000);

                 //// from the internet 
                 int index = c.getColumnIndex("match_date");
                 long time = c.getLong(index);
                 ////////
                 Log.e("inside the Shownotification", "index is" + index);


                if(time == i){

                     Log.e("inside the Shownotification", "time is" + time);

                    NotificationCompat.Builder builder = new Builder(
                            getApplicationContext());
                    Notification notification = builder
                            .setSmallIcon(R.drawable.fifa_2014_4)
                            .setContentTitle("Up Comming Match")
                            .setContentText(
                                    c.getString(c.getColumnIndex("match_date"))
                                            + (c.getColumnIndex("team1"))
                                            + (c.getColumnIndex("team2"))).build();


                    displayNotification(notification);
            }

                c.moveToNext();
        }

      }
        return c;
    }

    // for the notification

    private PendingIntent preparePendingIntent() {
        Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(
                getApplicationContext(), 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

         Log.e("inside the preparePendingIntent", "inside preparePendingIntent");

        return pendingIntent;

    }

    private void showNotification() {



        PendingIntent notificationIntent = preparePendingIntent();
        getAllRows(notificationIntent);

        c.moveToFirst();




    }

    private void displayNotification(Notification notification) {
        Log.e("inside the displayNotification", "inside displayNotification");


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(myNotificationId, notification);
    }  
}
erakitin
  • 11,437
  • 5
  • 44
  • 49
  • 2
    Please strip your code to show only the relevant part. – nhaarman May 19 '14 at 07:57
  • 11
    are you the same person who posted this question: http://stackoverflow.com/questions/23441285/how-to-create-android-notification-using-sqlite-database?rq=1 ? – Richard Le Mesurier May 19 '14 at 08:17
  • @NiekHaarman I definitely see your point, but if OP knew which was the relevant part, he/she/it likely wouldn't need to ask this kind of question here. (i.e. what's off/what's missing) – Maple May 20 '14 at 13:55

1 Answers1

1

The way you are comparing dates is wrong.

Log out the values of i and time just before you compare them - I think they will not be equal.

Rather have a look at this question, which shows better ways to compare dates in java:


Why is the way of comparing wrong?

  1. Firstly you are now comparing down to the millisecond level
  2. Secondly you are losing information by casting to (int)

A good way to try and track down an issue like this is to first make a Notification from outside any if..then blocks, using static data. Once you can do that, then you know that your Notification skills are fine.

You would then be able to find out if the Notification stuff is wrong, or the DB stuff.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255