Reputation: 33
I'm making an inventory app in Android Studio and I need some help with a custom notification function.
I want to create a notification system that outputs a notification when the user has no items in their inventory. I have coded a basic function for notifications, but I can't seem to figure out how to output the notification when all items in the inventory have been destroyed.
I'm not sure if I should be calling my notification method in the DisplayInventory class or my database class.
Any help is much appreciated!
This is the code for my DisplayInventory screen. This runs most of the functionality
public class DisplayInventory extends AppCompatActivity {
// Setting up all my objects from the display inventory layout, Database Helper and Adapter
RecyclerView mRecyclerView;
Button mAdd_button;
MyDBHelper myDatabase;
UserPermission notification;
ArrayList<String> itemId;
ArrayList<String> itemName;
ArrayList<String> itemOwner;
ArrayList<String> numItem;
InventoryAdapter inventoryAdapter;
TextView mLonelyInventory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_inventory);
//Instantiating necessary objects from the layout for functionality
mRecyclerView = findViewById(R.id.recyclerView);
mLonelyInventory = findViewById(R.id.LonelyInventory);
mAdd_button = findViewById(R.id.add_button);
//On Click listener that will take users from the display inventory screen to add new item screen
mAdd_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(DisplayInventory.this, AddItem.class);
startActivity(intent);
}
});
//Instantiating ArrayLists that will be used to hold information from the DB
myDatabase = new MyDBHelper(DisplayInventory.this);
itemId = new ArrayList<>();
itemName = new ArrayList<>();
itemOwner = new ArrayList<>();
numItem = new ArrayList<>();
InsertDataToArray();
inventoryAdapter = new InventoryAdapter(DisplayInventory.this, this, itemId, itemName, itemOwner, numItem);
mRecyclerView.setAdapter(inventoryAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(DisplayInventory.this));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) {
recreate();
}
}
//This function uses cursors to help move data into the arrays we set up
//setVisibility() lets me show some text when the inventory is empty
void InsertDataToArray() {
Cursor cursor = myDatabase.readAllData();
if(cursor.getCount() == 0) {
mLonelyInventory.setVisibility(View.VISIBLE);
}
else {
while (cursor.moveToNext()) {
itemId.add(cursor.getString(0));
itemName.add(cursor.getString(1));
itemOwner.add(cursor.getString(2));
numItem.add(cursor.getString(3));
}
mLonelyInventory.setVisibility(View.GONE);
}
}
This is the code for my database
public class MyDBHelper extends SQLiteOpenHelper {
// Setting up all variables that make up the columns in the database.
private Context context;
private static final String DATABASE_NAME = "InventoryList.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_list";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "item_name";
private static final String COLUMN_OWNER = "item_owner";
private static final String COLUMN_NUM = "item_num";
MyDBHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
//Initializing the Database
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query =
"CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_OWNER + " TEXT, " +
COLUMN_NUM + " INTEGER);";
sqLiteDatabase.execSQL(query);
}
@Override
//This will make sure to erase the table if one already exists.
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
//This method is adding functionality to add the item name, owner and number of items.
void addItem(String itemName, String itemOwner, int numItem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, itemName);
cv.put(COLUMN_OWNER, itemOwner);
cv.put(COLUMN_NUM, numItem);
long result = db.insert(TABLE_NAME, null, cv);
if (result == -1) {
Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Item Added to Inventory!", Toast.LENGTH_SHORT).show(); //PENDING: May not need this toast message. AFTER MAIN SCREEN RETURN FUNCTION ADDED
}
}
//This method sets up the functionality to search the entire database and read the data.
Cursor readAllData() {
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if (db != null) {
cursor = db.rawQuery(query, null);
}
return cursor;
}
//This method allows the user to update the item's data. Values that will be updated are item name, owner and number of items.
void updateData(String row_id, String item_name, String item_owner, String item_num) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, item_name);
cv.put(COLUMN_OWNER, item_owner);
cv.put(COLUMN_NUM, item_num);
long result = db.update(TABLE_NAME, cv, "_id=?", new String[]{row_id});
// if (result == -1) {
// Toast.makeText(context, "Well, that didn't work", Toast.LENGTH_SHORT).show(); //PENDING: May not need this toast message.
// }
// else {
// Toast.makeText(context, "The Update was a Success!", Toast.LENGTH_SHORT).show(); //PENDING: May not need this toast message.
// }
}
void deleteItem(String row_id) {
SQLiteDatabase db = this.getWritableDatabase();
long result = db.delete(TABLE_NAME, "_id=?", new String[]{row_id});
// if (result == -1) {
// Toast.makeText(context, "Data Could Not Be Deleted! It's Still Here!", Toast.LENGTH_SHORT).show(); //PENDING: May not need this toast message.
// }
// else {
// Toast.makeText(context, "The Data is No More!", Toast.LENGTH_SHORT).show(); //PENDING: May not need this toast message.
// }
}
void destroyInventory() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
}
}
This is the code where I have tried to set up notifications
public class UserPermission extends AppCompatActivity {
Button mUserPermission;
private int notificationId = 1;
MyDBHelper myDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_permission);
mUserPermission = findViewById(R.id.userPermission);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("My Channel ID", "My Channel ID", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
public void userPermission(View view) {
givePermission();
}
public void inventoryNotification() {
// NEW CODE ADDED HERE
String NotificationMessage = "No inventory Items!";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(UserPermission.this, "My Channel ID")
.setSmallIcon(R.drawable.notifications)
.setContentTitle("Inventory Notification")
.setContentText(NotificationMessage)
.setAutoCancel(true);
//.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(UserPermission.this);
managerCompat.notify(1,mBuilder.build());
}
Upvotes: 0
Views: 1163
Reputation: 33
Found a solution to this problem after asking the question.
Instead of creating a separate class that holds the notification functionality, I instead Implemented the notification functionality in the DisplayInventory class.
I first created a channel in onCreate()
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("My Channel ID", "My Channel ID", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
I then placed the code for the notification in the InsertDataToArray() method and checked if the database was empty. If so, the notification displays.
void InsertDataToArray() {
Cursor cursor = myDatabase.readAllData();
if(cursor.getCount() == 0) {
mLonelyInventory.setVisibility(View.VISIBLE);
//ADDING NEW CODE HERE
String NotificationMessage = "No items in Inventory!";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(DisplayInventory.this, "My Channel ID")
.setSmallIcon(R.drawable.notifications)
.setContentTitle("Inventory Notification")
.setContentText(NotificationMessage)
.setAutoCancel(true);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(DisplayInventory.this);
managerCompat.notify(1,mBuilder.build());
//NEW CODE ENDS HERE
}
Upvotes: 1