diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java b/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java index bdf05c1..93c7b08 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotificationPublisher.java @@ -46,81 +46,80 @@ public void onReceive(final Context context, Intent intent) { SQLiteDatabase db = mDbHelper.getWritableDatabase(); Cursor data = db.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE "+_ID+" = "+notificationId,null); - data.moveToFirst(); - String title = data.getString(data.getColumnIndex(NOTIFICATION_TITLE_TEXT)); - String content = data.getString(data.getColumnIndex(NOTIFICATION_CONTENT_TEXT)); - String rrule = data.getString(data.getColumnIndex(NOTIFICATION_RRULE)); - long dstart = data.getLong(data.getColumnIndex(NOTIFICATION_DSTART)); - String str_actions = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS)); - String str_actions_text = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_TEXT)); - String str_actions_dismiss = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_DISMISS)); - String str_actions_collapse = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_COLLAPSE)); - int led_color = data.getInt(data.getColumnIndex(NOTIFICATION_LED_COLOR)); - int small_icon = data.getInt(data.getColumnIndex(NOTIFICATION_SMALL_ICON)); - int large_icon = data.getInt(data.getColumnIndex(NOTIFICATION_LARGE_ICON)); - int color = data.getInt(data.getColumnIndex(NOTIFICATION_COLOR)); - String[] actions = NotifyMe.convertStringToArray(str_actions); - String[] actions_text = NotifyMe.convertStringToArray(str_actions_text); - String[] actions_dismiss = NotifyMe.convertStringToArray(str_actions_dismiss); - String[] actions_collapse = NotifyMe.convertStringToArray(str_actions_collapse); - data.close(); - db.close(); + if (data.moveToFirst()) { + String title = data.getString(data.getColumnIndex(NOTIFICATION_TITLE_TEXT)); + String content = data.getString(data.getColumnIndex(NOTIFICATION_CONTENT_TEXT)); + String rrule = data.getString(data.getColumnIndex(NOTIFICATION_RRULE)); + long dstart = data.getLong(data.getColumnIndex(NOTIFICATION_DSTART)); + String str_actions = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS)); + String str_actions_text = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_TEXT)); + String str_actions_dismiss = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_DISMISS)); + String str_actions_collapse = data.getString(data.getColumnIndex(NOTIFICATION_ACTIONS_COLLAPSE)); + int led_color = data.getInt(data.getColumnIndex(NOTIFICATION_LED_COLOR)); + int small_icon = data.getInt(data.getColumnIndex(NOTIFICATION_SMALL_ICON)); + int large_icon = data.getInt(data.getColumnIndex(NOTIFICATION_LARGE_ICON)); + int color = data.getInt(data.getColumnIndex(NOTIFICATION_COLOR)); + String[] actions = NotifyMe.convertStringToArray(str_actions); + String[] actions_text = NotifyMe.convertStringToArray(str_actions_text); + String[] actions_dismiss = NotifyMe.convertStringToArray(str_actions_dismiss); + String[] actions_collapse = NotifyMe.convertStringToArray(str_actions_collapse); + data.close(); - NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,notificationId); - if(small_icon != -1) { - mBuilder.setSmallIcon(small_icon); - }else{ - mBuilder.setSmallIcon(R.drawable.ic_check_circle); - } - if(large_icon != -1) { - Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), large_icon); - mBuilder.setLargeIcon(largeIcon); - } - mBuilder.setContentTitle(title); - mBuilder.setContentText(content); - mBuilder.setColor(color); - mBuilder.setVibrate(new long[] { 1000,1000,1000 }); - for (int i = 0; i < actions.length; i++) { - try { - Intent tent = new Intent(context,ActionReceiver.class); - tent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - tent.putExtra("_id",notificationId); - tent.putExtra("rrule",rrule); - tent.putExtra("dstart",dstart); - tent.putExtra("index",i); - tent.putExtra("action",actions[i]); - tent.putExtra("collapse",Boolean.parseBoolean(actions_collapse[i])); - tent.putExtra("dismiss",Boolean.parseBoolean(actions_dismiss[i])); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context,Integer.parseInt(notificationId)*3+i,tent,PendingIntent.FLAG_UPDATE_CURRENT); - mBuilder.addAction(R.drawable.ic_check_circle,actions_text[i],pendingIntent); - } catch (Exception e) { - e.printStackTrace(); + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,notificationId); + if(small_icon != -1) { + mBuilder.setSmallIcon(small_icon); + }else{ + mBuilder.setSmallIcon(R.drawable.ic_check_circle); } - } - Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - mBuilder.setSound(uri); - Intent deleteIntent = new Intent(context,DeletePendingIntent.class); - deleteIntent.putExtra("_id",notificationId); - deleteIntent.putExtra("rrule",rrule); - deleteIntent.putExtra("dstart",dstart); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context,Integer.parseInt(notificationId),deleteIntent,PendingIntent.FLAG_UPDATE_CURRENT); - mBuilder.setDeleteIntent(pendingIntent); - Notification notification = mBuilder.build(); + if(large_icon != -1) { + Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), large_icon); + mBuilder.setLargeIcon(largeIcon); + } + mBuilder.setContentTitle(title); + mBuilder.setContentText(content); + mBuilder.setColor(color); + mBuilder.setVibrate(new long[] { 1000,1000,1000 }); + for (int i = 0; i < actions.length; i++) { + try { + Intent tent = new Intent(context,ActionReceiver.class); + tent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + tent.putExtra("_id",notificationId); + tent.putExtra("rrule",rrule); + tent.putExtra("dstart",dstart); + tent.putExtra("index",i); + tent.putExtra("action",actions[i]); + tent.putExtra("collapse",Boolean.parseBoolean(actions_collapse[i])); + tent.putExtra("dismiss",Boolean.parseBoolean(actions_dismiss[i])); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context,Integer.parseInt(notificationId)*3+i,tent,PendingIntent.FLAG_UPDATE_CURRENT); + mBuilder.addAction(R.drawable.ic_check_circle,actions_text[i],pendingIntent); + } catch (Exception e) { + e.printStackTrace(); + } + } + Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + mBuilder.setSound(uri); + Intent deleteIntent = new Intent(context,DeletePendingIntent.class); + deleteIntent.putExtra("_id",notificationId); + deleteIntent.putExtra("rrule",rrule); + deleteIntent.putExtra("dstart",dstart); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context,Integer.parseInt(notificationId),deleteIntent,PendingIntent.FLAG_UPDATE_CURRENT); + mBuilder.setDeleteIntent(pendingIntent); + Notification notification = mBuilder.build(); - notification.ledARGB = led_color; - notification.flags = Notification.FLAG_SHOW_LIGHTS; - notification.ledOnMS = 300; - notification.ledOffMS = 1000; - NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + notification.ledARGB = led_color; + notification.flags = Notification.FLAG_SHOW_LIGHTS; + notification.ledOnMS = 300; + notification.ledOffMS = 1000; + NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel nc = new NotificationChannel(notificationId,notificationId,NotificationManager.IMPORTANCE_HIGH); - nc.enableLights(true); - nc.setLightColor(led_color); - mNotificationManager.createNotificationChannel(nc); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel nc = new NotificationChannel(notificationId,notificationId,NotificationManager.IMPORTANCE_HIGH); + nc.enableLights(true); + nc.setLightColor(led_color); + mNotificationManager.createNotificationChannel(nc); + } + mNotificationManager.notify(Integer.parseInt(notificationId), notification); } - mNotificationManager.notify(Integer.parseInt(notificationId), notification); + db.close(); } - - -} \ No newline at end of file +} diff --git a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java index 1770e0b..46afebc 100644 --- a/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java +++ b/notifyme/src/main/java/com/allyants/notifyme/NotifyMe.java @@ -124,12 +124,13 @@ public static void cancel(Context context,String key){ Notification.NotificationDBHelper mDbHelper = new Notification.NotificationDBHelper(context); SQLiteDatabase db = mDbHelper.getWritableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + Notification.NotificationEntry.TABLE_NAME + " WHERE custom_id = ? LIMIT 1", new String[]{key}); - cursor.moveToFirst(); - int notificationId = cursor.getInt(cursor.getColumnIndex(Notification.NotificationEntry._ID)); - db.delete(TABLE_NAME, com.allyants.notifyme.Notification.NotificationEntry._ID + " = " + notificationId, null); + if (cursor.moveToFirst()) { + int notificationId = cursor.getInt(cursor.getColumnIndex(Notification.NotificationEntry._ID)); + db.delete(TABLE_NAME, com.allyants.notifyme.Notification.NotificationEntry._ID + " = " + notificationId, null); + cursor.close(); + mNotificationManager.cancel(notificationId); + } db.close(); - cursor.close(); - mNotificationManager.cancel(notificationId); }catch (Exception e){ e.printStackTrace(); }