为什么通知不会出现? [英] Why the notification do not appear?

查看:129
本文介绍了为什么通知不会出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用SQLite数据库来存储数据的android应用。它的一个字段是 match_date 包含的日期与时间。

我要的是当前日期进行比较与存储日期的数据库,如果后两者匹配的通知必须出现创建的通知,其中包括球队的名字和日期。

该应用程序的工作完美,但没有得到任何通知,所以哪里是我的错误是如何解决这个问题?

我会AP preciate任何帮助。

这是我的数据库表

  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,
  [期] VARCHAR(50)NOT NULL,
  [match_date] DATETIME NOT NULL,
  [FLAGS1] CHAR(255),
  [flags2] CHAR(255),
  [group_team] CHAR(1));
 

FirstActivity.java

 公共类FirstActivity延伸活动{

    TextView的txtDate;

    私人SQLiteDatabase数据库;
    私人ExternalDbOpenHelper extDB;

    私有静态最后弦乐DB_NAME =world_cup.db;
    // *****表名*********** //
    私有静态最后弦乐TABLE_NAME =match_list;

    // ***** Tbale名******************************

    公共静态最后弦乐PLACE_ID =_id;
    公共静态最后弦乐STAD_NAME =stad_name;
    公共静态最后弦乐TEAM1 =team1;
    公共静态最后弦乐的Team2 =的Team2;
    公共静态最后弦乐MATCH_DATE =match_date;
    公共静态最后弦乐FLAG1 =FLAGS1;
    公共静态最后弦乐FLAG2 =flags2;

    公共静态最后的String [] ALL_KEYS =新的String [] {PLACE_ID,STAD_NAME,
            TEAM1,的Team2,MATCH_DATE,FLAG1,FLAG2,};
    // ***** Tbale名******************************

    ItemDetails listdetail;

    光标C;

    私有静态最终诠释myNotificationId = 1;


    ///////////////////////
    长diffSeconds;
    长diffMinutes;
    长diffHours;
    长diffDays;
    ///////////////////////

    //用于报警amanager和通知
    公共静态INT EVENTID = 1;

    日D1 =新的日期();

    SimpleDateFormat的格式;

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

    //私人DatabaseHelper dbHelper = NULL;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_first);
        getDiffDate();

        extDB =新ExternalDbOpenHelper(这一点,DB_NAME);
        数据库= extDB.openDataBase();

        showNotification();

         Log.e(下称里面的onCreate,里面的onCreate);


    }


    公共无效listTeams(视图v){
        意图=新的意图(getBaseContext(),MainActivity.class);
        startActivity(在);
    }

    公共无效listGroups(视图v){
        意图=新的意图(getBaseContext(),GroupList.class);
        startActivity(在);
    }

    公共无效DisplaySched(视图v){

        意图=新的意图(getBaseContext(),MatchScheduleList.class);
        startActivity(在);
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.first,菜单);
        返回true;
    }


      公共无效getDiffDate(){

        字符串2037 =二〇一四年六月一十二日23:00:00;

        // HH 24小时格式(0-23),天计算转换小时
        格式=新的SimpleDateFormat(MM / DD / YYYY HH:MM:SS);

        D1 =新的日期();


        日期D2 = NULL;

        尝试 {

            D2 = format.parse(2037);

            //以毫秒为单位
            长差异= d2.getTime() -  d1.getTime();

            长diffSeconds =差异/ 1000%60;
            长diffMinutes =差异/(60 * 1000)%60;
            长diffHours =差异/(60 * 60 * 1000)%24;
            长diffDays =差异/(24 * 60 * 60 * 1000);

            System.out.print(diffDays +天);
            System.out.print(diffHours +小时);
            System.out.print(diffMinutes +分钟);
            System.out.print(diffSeconds +秒。);

            txtDate =(TextView中)findViewById(R.id.txtDifDate);
            字符串resultDate = diffDays +天+ diffHours +小时
                    + diffMinutes +最小+ diffSeconds +S;
            txtDate.setText(resultDate);

        }赶上(例外五){
            e.printStackTrace();
        }

    }


    公共光标getAllRows(PendingIntent notificationIntent){
        字符串,其中= NULL;

        C = database.query(真,TABLE_NAME,ALL_KEYS,其中,NULL,NULL,NULL,
                NULL,NULL);
        如果(C!= NULL){

            c.moveToFirst();


            而(!c.isAfterLast()){
                listdetail =新ItemDetails();

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

                listdetail.setTeam2(c.getString(c.getColumnIndex(的Team2)));

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




                 //如果从数据库中拾取日期等于当前日期通知将弹出

                长I =(INT)(新日期()的getTime()/ 1000);

                 从互联网////
                 INT指数= c.getColumnIndex(match_date);
                 很长一段时间= c.getLong(指数);
                 ////////
                 Log.e(下称Shownotification内部,指数+指数);


                如果(时间== I){

                     Log.e(下称Shownotification内,时间+时间);

                    NotificationCompat.Builder建设者=新生成器(
                            getApplicationContext());
                    通知通知=建设者
                            .setSmallIcon(R.drawable.fifa_2014_4)
                            .setContentTitle(上正在添加匹配)
                            .setContentText(
                                    c.getString(c.getColumnIndex(match_date))
                                            +(c.getColumnIndex(team1))
                                            +(c.getColumnIndex(的Team2)))建立()。


                    displayNotification(通知);
            }

                c.moveToNext();
        }

      }
        返回℃;
    }

    //对于通知

    私人PendingIntent preparePendingIntent(){
        意向意图=新的意图(getApplicationContext(),FirstActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);

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

         Log.e(里面的preparePendingIntent,里面的preparePendingIntent);

        返回pendingIntent;

    }

    私人无效showNotification(){



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

        c.moveToFirst();




    }

    私人无效displayNotification(通知通告){
        Log.e(下称displayNotification里,内displayNotification);


        NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(myNotificationId,通知);
    }
}
 

解决方案

您比较日期的方式是错误的。

注销的值时间你比较只是在他们面前 - 我想他们也不会等于

而看看这个问题,它显示了更好的方法来比较日期在java中:


为什么是比较错误的方法吗?

  1. 首先,你现在比较精确到毫秒级
  2. 其次,你是通过转换成信息丢失(INT)

一个很好的方式,试图追查一个问题是这样的,首先做一个通知来自外部的任何 if..then 块,使用静态数据。一旦你能做到这一点,那么你知道你的通知技巧都很好。

您随后将能够找出是否通知的东西是错误的,或者在DB的东西。

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);
    }  
}

解决方案

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.

这篇关于为什么通知不会出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆