Notification channel description

This commit is contained in:
Yegor Vialov 2020-05-25 11:05:16 +00:00
parent aac0cfbb56
commit 8eb15ab9a4
3 changed files with 7 additions and 16 deletions

View File

@ -15,7 +15,6 @@ import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
@ -31,10 +30,8 @@ public class MessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Map<String, String> data = remoteMessage.getData();
if (data.size() > 0) {
Log.d(TAG, "Message data payload: " + data);
if (data.containsKey("body") || data.containsKey("title")) {
sendNotification(data);
}
@ -43,18 +40,18 @@ public class MessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
//TODO update token
}
private void sendNotification(Map<String, String> data) {
String channelId, messageBody, messageTitle, imageUrl;
String nTag;
String channelId, messageBody, messageTitle, imageUrl, nTag, channelDescription;
boolean autoCancel;
if (!data.containsKey("channelId")) {
channelId = "ha_notify";
channelDescription = "Default notification channel";
} else {
channelId = data.get("channelId");
channelDescription = channelId;
}
if (!data.containsKey("body")) {
messageBody = "";
@ -71,7 +68,6 @@ public class MessagingService extends FirebaseMessagingService {
} else {
nTag = data.get("tag");
}
Log.d(TAG, "Notification tag: " + nTag);
if (data.containsKey("dismiss")) {
try {
boolean dismiss = Boolean.parseBoolean(data.get("dismiss"));
@ -132,8 +128,8 @@ public class MessagingService extends FirebaseMessagingService {
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Home Assistant notifications",
NotificationManager.IMPORTANCE_DEFAULT);
channelDescription,
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}

View File

@ -22,7 +22,6 @@ public class NotificationActionReceiver extends BroadcastReceiver {
String rawActionData = intent.getStringExtra("actionData");
if (intent.hasExtra("tag")) {
String notificationTag = intent.getStringExtra("tag");
Log.d(TAG, "Canceling notification by tag: " + notificationTag);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationTag, 0);
}
@ -36,7 +35,6 @@ public class NotificationActionReceiver extends BroadcastReceiver {
":" +
prefs.getString("flutter.hassio-port", "") + "/api/webhook/" + webhookId;
JSONObject actionData = new JSONObject(rawActionData);
Log.d(TAG, "request url: " + requestUrl);
if (URLUtil.isValidUrl(requestUrl)) {
JSONObject dataToSend = new JSONObject();
JSONObject requestData = new JSONObject();
@ -53,17 +51,16 @@ public class NotificationActionReceiver extends BroadcastReceiver {
}
dataToSend.put("data", requestData);
String stringRequest = dataToSend.toString();
Log.d(TAG, "Data to send home: " + stringRequest);
SendTask sendTask = new SendTask();
sendTask.execute(requestUrl, stringRequest);
} else {
Log.w(TAG, "Invalid url");
Log.w(TAG, "Invalid HA url");
}
} catch (Exception e) {
Log.e(TAG, "Error handling notification action", e);
}
} else {
Log.d(TAG, "Webhook id not found");
Log.w(TAG, "Webhook id not found");
}
}
}

View File

@ -26,7 +26,6 @@ public class SendTask extends AsyncTask<String, String, String> {
String data = params[1];
try {
Log.d(TAG, "Connecting and sending...");
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
@ -38,7 +37,6 @@ public class SendTask extends AsyncTask<String, String, String> {
int responseCode = urlConnection.getResponseCode();
Log.d(TAG, "responseCode: " + responseCode);
urlConnection.disconnect();
} catch (Exception e) {
Log.e(TAG, "Error sending data", e);