-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationActivity.java
More file actions
108 lines (85 loc) · 3.81 KB
/
NotificationActivity.java
File metadata and controls
108 lines (85 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.example.myinventoryapp;
import android.database.Cursor;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class NotificationActivity extends AppCompatActivity {
private Button sendTestButton, saveButton;
private EditText phoneNumberEditText;
private Switch enableNotificationsSwitch;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
dbHelper = new DatabaseHelper(this);
phoneNumberEditText = findViewById(R.id.phoneNumberEditText);
enableNotificationsSwitch = findViewById(R.id.enableNotificationsSwitch);
sendTestButton = findViewById(R.id.sendTestButton);
saveButton = findViewById(R.id.saveButton);
// Enable all controls (removed the disabled state)
phoneNumberEditText.setEnabled(true);
enableNotificationsSwitch.setEnabled(true);
sendTestButton.setEnabled(true);
sendTestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendTestNotification();
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(NotificationActivity.this, "Notification settings saved", Toast.LENGTH_SHORT).show();
finish();
}
});
}
private void sendTestNotification() {
String phoneNumber = phoneNumberEditText.getText().toString().trim();
if (phoneNumber.isEmpty()) {
Toast.makeText(this, "Please enter a phone number", Toast.LENGTH_SHORT).show();
return;
}
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null,
"This is a test notification from MyInventoryApp", null, null);
Toast.makeText(this, "Test notification sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to send test notification: " + e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
public void checkAndSendLowInventoryNotifications(String phoneNumber) {
if (phoneNumber.isEmpty()) return;
Cursor cursor = dbHelper.getLowInventoryItems();
if (cursor.moveToFirst()) {
StringBuilder messageBuilder = new StringBuilder("Low inventory alert: ");
do {
int itemNameColumnIndex = cursor.getColumnIndex("item_name");
int quantityColumnIndex = cursor.getColumnIndex("quantity");
if (itemNameColumnIndex >= 0 && quantityColumnIndex >= 0) {
String itemName = cursor.getString(itemNameColumnIndex);
int quantity = cursor.getInt(quantityColumnIndex);
messageBuilder.append(itemName).append(" (").append(quantity).append("), ");
}
} while (cursor.moveToNext());
if (messageBuilder.length() > "Low inventory alert: ".length()) {
String message = messageBuilder.substring(0, messageBuilder.length() - 2);
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
cursor.close();
}
}