display notifications

This commit is contained in:
CiufudeanDani 2022-03-31 14:12:03 +03:00
parent ca4255943a
commit ba0dc850f7
4 changed files with 64 additions and 65 deletions

View File

@ -78,6 +78,4 @@ dependencies {
// add Gson // add Gson
implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.google.code.gson:gson:2.8.6'
//retrofit
} }

View File

@ -16,17 +16,22 @@ public class NotificationActivity extends Activity{
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// get notification type // get notification type
int key = getIntent().getExtras().getInt("key"); int key = getIntent().getExtras().getInt("key");
// broadcast intent // broadcast intent
Intent i = new Intent(); Intent i = new Intent();
switch(key) switch(key) {
{ case AppParams.messageNotif:
case AppParams.messageNotif: i.setAction(NOTIFICATION_MESSAGE_INTENT); break; i.setAction(NOTIFICATION_MESSAGE_INTENT);
case AppParams.alertNotif: i.setAction(NOTIFICATION_ALERT_INTENT); break; break;
case AppParams.pollNotif: i.setAction(NOTIFICATION_POLL_INTENT); break; case AppParams.alertNotif:
} i.setAction(NOTIFICATION_ALERT_INTENT);
break;
case AppParams.pollNotif:
i.setAction(NOTIFICATION_POLL_INTENT);
break;
}
getBaseContext().sendBroadcast(i); getBaseContext().sendBroadcast(i);
@ -35,6 +40,4 @@ public class NotificationActivity extends Activity{
/* Finish activity and return to parent activity */ /* Finish activity and return to parent activity */
finish(); finish();
} }
}
}

View File

@ -14,13 +14,11 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.safemobile.activities.AbstractEmptyActivity; import com.safemobile.activities.AbstractEmptyActivity;
import com.safemobile.activities.AbstractLiveActivity; import com.safemobile.activities.AbstractLiveActivity;
import com.safemobile.activities.AbstractMessagesActivity; import com.safemobile.activities.AbstractMessagesActivity;
import com.safemobile.activities.AbstractRadioActivity; import com.safemobile.activities.AbstractRadioActivity;
import com.safemobile.activities.AbstractSDParentActivity; import com.safemobile.activities.AbstractSDParentActivity;
import com.safemobile.safedispatch.R;
import com.safemobile.enums.AuthorizationCode; import com.safemobile.enums.AuthorizationCode;
import com.safemobile.enums.AuthorizationStatus; import com.safemobile.enums.AuthorizationStatus;
import com.safemobile.enums.ProviderSettingsStatus; import com.safemobile.enums.ProviderSettingsStatus;
@ -59,7 +57,7 @@ import com.safemobile.services.TCPService.TCPBinder;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Dialog; import android.app.Dialog;
import android.app.Notification; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -74,6 +72,7 @@ import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
@ -94,6 +93,8 @@ import android.widget.Toast;
import android.widget.TabHost.TabSpec; import android.widget.TabHost.TabSpec;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class TabLayoutActivity extends AbstractSDParentActivity { public class TabLayoutActivity extends AbstractSDParentActivity {
@ -1233,7 +1234,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
getMessageActivity().updateSMS(sms.smsList); getMessageActivity().updateSMS(sms.smsList);
} }
} catch (Exception ex) { } catch (Exception ex) {
SM.Debug("Error on smsReceived:" + ex.toString()); SM.Debug("Error on smsReceived:" + ex);
} }
} }
@ -1426,7 +1427,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
SM.Debug("tmpHist seqID:" + tmpHist.seqID); SM.Debug("tmpHist seqID:" + tmpHist.seqID);
int pos = Integer.parseInt(tmpHist.seqID.substring(0, tmpHist.seqID.indexOf('.'))); int pos = Integer.parseInt(tmpHist.seqID.substring(0, tmpHist.seqID.indexOf('.')));
int all = Integer.parseInt(tmpHist.seqID.substring(tmpHist.seqID.indexOf('.') + 1, tmpHist.seqID.length())); int all = Integer.parseInt(tmpHist.seqID.substring(tmpHist.seqID.indexOf('.') + 1));
if (all != 0) { if (all != 0) {
if (Boolean.TRUE.equals(getFirstHistoryData())) { if (Boolean.TRUE.equals(getFirstHistoryData())) {
try { try {
@ -1668,13 +1669,11 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
case 137: case 137:
setMess("zone " + tempArr[1]); setMess("zone " + tempArr[1]);
break; break;
case 138:
setMess("emergency");
break;
case 140: case 140:
setMess("telemetry " + tempArr[1]); setMess("telemetry " + tempArr[1]);
break; break;
default: default:
// 138
setMess("emergency"); setMess("emergency");
} }
myHandler.post(updateResultsAlarm); myHandler.post(updateResultsAlarm);
@ -1896,10 +1895,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
public void createNotification(int icon) { public void createNotification(int icon) {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String tickerText = "SafeMobile Dispatch: New Message from " + getImei(); String contentTitle, contentText;
String contentTitle = "New Message from " + getImei();
String contentText = "\"" + getMess() + "\"";
Vehicle veh = getVehicle4Imei(getImei()); Vehicle veh = getVehicle4Imei(getImei());
int iconValue = icon; int iconValue = icon;
@ -1912,7 +1908,6 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
case AppParams.pollNotif: case AppParams.pollNotif:
contentText = "\"" + getMess() + "\""; contentText = "\"" + getMess() + "\"";
contentTitle = "Poll Reply from " + (veh != null ? getVehicle4Imei(getImei()).name : getImei()); contentTitle = "Poll Reply from " + (veh != null ? getVehicle4Imei(getImei()).name : getImei());
tickerText = "SafeMobile Dispatch: Poll Reply from " + (veh != null ? getVehicle4Imei(getImei()).name : getImei());
icon = R.drawable.poll; icon = R.drawable.poll;
break; break;
case AppParams.alertNotif: case AppParams.alertNotif:
@ -1921,34 +1916,53 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
vehName = getString(R.string.from) + ": " + getVehicle4Imei(getImei()).name; vehName = getString(R.string.from) + ": " + getVehicle4Imei(getImei()).name;
contentText = "\"" + getMess() + "\""; contentText = "\"" + getMess() + "\"";
contentTitle = getString(R.string.newAlarm) + vehName; contentTitle = getString(R.string.newAlarm) + vehName;
tickerText = "SafeMobile Dispatch: " + getString(R.string.newAlarm) + vehName; icon = R.drawable.arrow_down;
icon = R.drawable.alert;
break; break;
default: default:
throw new IllegalStateException("Unexpected value: " + icon); throw new IllegalStateException("Unexpected value: " + icon);
} }
Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
// set intent to be opened on NotificationClick // set intent to be opened on NotificationClick
/* Notification */ /* Notification */
Intent notificationIntent = new Intent(this, NotificationActivity.class); Intent intent = new Intent(this, NotificationActivity.class);
notificationIntent.putExtra("key", iconValue); intent.putExtra("key", iconValue);
// cancel old notification // cancel old notification
mNotificationManager.cancel(icon); mNotificationManager.cancel(icon);
PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ACTIVITY_RESULT, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent pendingIntent = PendingIntent.getActivity(context, NOTIFICATION_ACTIVITY_RESULT, intent, PendingIntent.FLAG_CANCEL_CURRENT);
createNotificationChannel(icon, contentTitle, contentText, pendingIntent);
// flag that the notification will be closed when clicked
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number = 1; // the same notification will be shown;
notification.tickerText = tickerText; // notification text when arrives
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.new_sms);
// add notification to the manager
mNotificationManager.notify(icon, notification);
} }
private void createNotificationChannel(int icon, String title, String text, PendingIntent pendingIntent) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
String notificationChannelID = String.valueOf(icon);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Notification Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(notificationChannelID, name, importance);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notificationChannelID)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(text)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Log.v("test111", "test");
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
managerCompat.notify(icon, builder.build());
}
// return vehicle according to imei // return vehicle according to imei
private Vehicle getVehicle4Imei(String imei) { private Vehicle getVehicle4Imei(String imei) {
Vehicle veh = null; Vehicle veh = null;
@ -2092,7 +2106,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
setVehicleStatus(Integer.parseInt(params[1]), Integer.parseInt(params[2]), Integer.parseInt(params[3]), Integer.parseInt(params[4])); setVehicleStatus(Integer.parseInt(params[1]), Integer.parseInt(params[2]), Integer.parseInt(params[3]), Integer.parseInt(params[4]));
} catch (Exception ex) { } catch (Exception ex) {
SM.Exception("Paramas -> setVehicleStatus", "EXCeption ex " + ex.toString()); SM.Exception("Paramas -> setVehicleStatus", "EXCeption ex " + ex);
} }
break; break;
@ -2123,21 +2137,6 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
new ConnectTask().execute(params); new ConnectTask().execute(params);
} }
public class ConnectParserTask extends AsyncTask<String, Void, TCPhandler> {
@Override
protected TCPhandler doInBackground(String... params) {
if (myService != null)
tcpParser = myService.getTCPmsgParser();
// add TCPParserListener
if (tcpParser != null) {
SM.Debug("## tcpParser != null ", "#### call tcpParserListener(");
tcpParserListener();
}
return null;
}
}
/* Display Toast messages*/ /* Display Toast messages*/
@Override @Override
public void displayToast(final String msg) { public void displayToast(final String msg) {
@ -2247,10 +2246,8 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
SM.Debug("onRequestPermissionsResult", "[requestCode] " + requestCode); SM.Debug("onRequestPermissionsResult", "[requestCode] " + requestCode);
String permission = "";
switch (requestCode) { switch (requestCode) {
case PermissionModule.RECORD_AUDIO_PERMISSION_REQUEST_CODE: case PermissionModule.RECORD_AUDIO_PERMISSION_REQUEST_CODE:
permission = Manifest.permission.RECORD_AUDIO;
break; break;
default: default:
throw new IllegalStateException("Unexpected value: " + requestCode); throw new IllegalStateException("Unexpected value: " + requestCode);
@ -2301,10 +2298,9 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
private void waitForUserInput(Boolean result) { private void waitForUserInput(Boolean result) {
if (result) { if (result) {
final int requestCode = 1202;
//navigate to application's settings //navigate to application's settings
String action = ProviderSettingsHelper.getAction(ProviderSettingsStatus.APPLICATION_DETAILS); String action = ProviderSettingsHelper.getAction(ProviderSettingsStatus.APPLICATION_DETAILS);
if (action == null || action == "") if (action == null || action.equals(""))
return; return;
boolean isActionApplicationDetailsSettings = action.equals(ProviderSettingsHelper.ACTION_APPLICATION_DETAILS_SETTINGS); boolean isActionApplicationDetailsSettings = action.equals(ProviderSettingsHelper.ACTION_APPLICATION_DETAILS_SETTINGS);

View File

@ -88,7 +88,9 @@
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/layoutNewMessage" android:id="@+id/layoutNewMessage"
android:layout_width="350dp" android:layout_width="350dp"
@ -113,7 +115,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
android:padding="3dp"> android:padding="3dp">
<TextView <TextView
android:id="@+id/slideTabsText" android:id="@+id/slideTabsText"