Notification not appearing Notification Tray

136 Views Asked by At

In my application I have a LocationTester Service implements LocationListener which is responsible for retrieving the Reminders from sqlite Db and comparing their distances with current location in onLocationChanged() method and then present the information if the distance is less than 1km and then throw notification related to that particular reminder.

The problem is that when I check for a certain location with a successful response every thing goes well even the toast is displaying where it meets the condition but I am not getting any sort of notification.

LocationTester Service:

public void onLocationChanged(Location location) {
        dataSrc.open();
        ArrayList<Reminders> rems = (ArrayList<Reminders>) dataSrc.findALL();

        for(Reminders x:rems){
            Location locs = new Location("");
            locs.setLatitude(x.getLat());
            locs.setLongitude(x.getLng());

            double dis = location.distanceTo(locs);
            if(dis<=1000){
                String title = x.getTitle();
                String des = x.getDescription();
                Intent intent = new Intent(this,ActivityShowReminders.class);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
                builder.setContentTitle(title);
                builder.setContentText(des);
                noteManag = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                noteManag.notify(noteId,builder.build());
                noteId += 1;
                Toast.makeText(this, title+"("+des+")", Toast.LENGTH_LONG).show();
            }
        }

datasrc:

public List<Reminders> findALL(){
        List<Reminders> reminder = new ArrayList<Reminders>();
        Cursor cursor = database.query(ReminderDBOpenHelper.TABLE_REMINDER, allColumns, 
                null, null, null, null, null);
        Log.i(LOG_TAG,cursor.getCount()+" rows");
        if(cursor.getCount()>0){
            while(cursor.moveToNext()){
                Reminders reminders = new Reminders();
                reminders.setId(cursor.getLong(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_ID)));
                reminders.setTitle(cursor.getString(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_TITLE)));
                reminders.setDescription(cursor.getString(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_DESCRIPTION)));
                reminders.setLat(cursor.getDouble(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_LAT)));
                reminders.setLng(cursor.getDouble(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_LONG)));
                reminder.add(reminders);
            }
        }
2

There are 2 best solutions below

2
On BEST ANSWER

you are missing builder.setSmallIcon(R.drawable.remindericon);the notification will not appear without this. And for illegalargumentexception contentintent required notifiction try attaching pending intent.

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
                    Intent notificationIntent = new Intent(this,YourTargetClass.class );
                    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
                    notification.contentIntent = contentIntent;
                }
0
On

Use this for generate notification

    static int uniqueID = 0 ;
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    builder.setContentTitle(title);
    builder.setContentText("text");
    builder.setTicker("Ticker");
    builder.setSmallIcon(R.drawable.icon);
    builder.setVibrate(new long[]{100, 1500});
    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    uniqueID = uniqueID + 1;
    notificationManager.notify(uniqueID, notification);