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

View File

@ -22,7 +22,6 @@ public class NotificationActionReceiver extends BroadcastReceiver {
String rawActionData = intent.getStringExtra("actionData"); String rawActionData = intent.getStringExtra("actionData");
if (intent.hasExtra("tag")) { if (intent.hasExtra("tag")) {
String notificationTag = intent.getStringExtra("tag"); String notificationTag = intent.getStringExtra("tag");
Log.d(TAG, "Canceling notification by tag: " + notificationTag);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationTag, 0); notificationManager.cancel(notificationTag, 0);
} }
@ -36,7 +35,6 @@ public class NotificationActionReceiver extends BroadcastReceiver {
":" + ":" +
prefs.getString("flutter.hassio-port", "") + "/api/webhook/" + webhookId; prefs.getString("flutter.hassio-port", "") + "/api/webhook/" + webhookId;
JSONObject actionData = new JSONObject(rawActionData); JSONObject actionData = new JSONObject(rawActionData);
Log.d(TAG, "request url: " + requestUrl);
if (URLUtil.isValidUrl(requestUrl)) { if (URLUtil.isValidUrl(requestUrl)) {
JSONObject dataToSend = new JSONObject(); JSONObject dataToSend = new JSONObject();
JSONObject requestData = new JSONObject(); JSONObject requestData = new JSONObject();
@ -53,17 +51,16 @@ public class NotificationActionReceiver extends BroadcastReceiver {
} }
dataToSend.put("data", requestData); dataToSend.put("data", requestData);
String stringRequest = dataToSend.toString(); String stringRequest = dataToSend.toString();
Log.d(TAG, "Data to send home: " + stringRequest);
SendTask sendTask = new SendTask(); SendTask sendTask = new SendTask();
sendTask.execute(requestUrl, stringRequest); sendTask.execute(requestUrl, stringRequest);
} else { } else {
Log.w(TAG, "Invalid url"); Log.w(TAG, "Invalid HA url");
} }
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error handling notification action", e); Log.e(TAG, "Error handling notification action", e);
} }
} else { } 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]; String data = params[1];
try { try {
Log.d(TAG, "Connecting and sending...");
URL url = new URL(urlString); URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST"); urlConnection.setRequestMethod("POST");
@ -38,7 +37,6 @@ public class SendTask extends AsyncTask<String, String, String> {
int responseCode = urlConnection.getResponseCode(); int responseCode = urlConnection.getResponseCode();
Log.d(TAG, "responseCode: " + responseCode);
urlConnection.disconnect(); urlConnection.disconnect();
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error sending data", e); Log.e(TAG, "Error sending data", e);