From 92a12302679f1d2fc2f8d8f30fdf0837d394801a Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Sun, 24 May 2020 15:04:55 +0000 Subject: [PATCH] WIP #471 Handling basic notifications in native code --- android/app/build.gradle | 1 + android/app/src/main/AndroidManifest.xml | 6 +- .../hassclient/MessagingService.java | 95 +++++++++++++++++++ .../hassclient/NotificationActionWorker.java | 25 +++++ 4 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 android/app/src/main/java/com/keyboardcrumbs/hassclient/MessagingService.java create mode 100644 android/app/src/main/java/com/keyboardcrumbs/hassclient/NotificationActionWorker.java diff --git a/android/app/build.gradle b/android/app/build.gradle index 772bc9e..6215bc3 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -80,6 +80,7 @@ flutter { dependencies { implementation 'com.google.firebase:firebase-analytics:17.2.2' implementation 'com.google.firebase:firebase-messaging:20.2.0' + implementation 'androidx.work:work-runtime:2.3.4' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 31520d2..8099c4e 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -48,15 +48,15 @@ - + data = remoteMessage.getData(); + if (data.size() > 0) { + Log.d(TAG, "Message data payload: " + data); + if (data.containsKey("body") || data.containsKey("title")) { + sendNotification( + data.get("body"), + data.get("title"), + data.get("channelId"), + data.get("action1"), + data.get("action2"), + data.get("action3") + ); + } + } + } + + @Override + public void onNewToken(String token) { + Log.d(TAG, "Refreshed token: " + token); + //TODO update token + } + + private void executeAction() { + OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(NotificationActionWorker.class) + .build(); + WorkManager.getInstance().beginWith(work).enqueue(); + } + + private void sendNotification(String messageBody, String messageTitle, String channelId, String action1, String action2, String action3) { + if (channelId == null) { + channelId = "ha_notify"; + } + if (messageBody == null) { + messageBody = ""; + } + if (messageTitle == null) { + messageTitle = "HA Client"; + } + Intent intent = new Intent(this, MainActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, + PendingIntent.FLAG_ONE_SHOT); + Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + NotificationCompat.Builder notificationBuilder = + new NotificationCompat.Builder(this, channelId) + .setSmallIcon(R.drawable.mini_icon) + .setContentTitle(messageTitle) + .setContentText(messageBody) + .setAutoCancel(false) + .setSound(defaultSoundUri) + .setContentIntent(pendingIntent); + + NotificationManager notificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + // 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); + notificationManager.createNotificationChannel(channel); + } + + notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); + } +} \ No newline at end of file diff --git a/android/app/src/main/java/com/keyboardcrumbs/hassclient/NotificationActionWorker.java b/android/app/src/main/java/com/keyboardcrumbs/hassclient/NotificationActionWorker.java new file mode 100644 index 0000000..5fbf844 --- /dev/null +++ b/android/app/src/main/java/com/keyboardcrumbs/hassclient/NotificationActionWorker.java @@ -0,0 +1,25 @@ +package com.keyboardcrumbs.hassclient; + +import android.content.Context; +import androidx.annotation.NonNull; +import android.util.Log; + +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +public class NotificationActionWorker extends Worker { + + private static final String TAG = "NotificationActionWorker"; + + public NotificationActionWorker(@NonNull Context appContext, @NonNull WorkerParameters workerParams) { + super(appContext, workerParams); + } + + @NonNull + @Override + public Result doWork() { + Log.d(TAG, "Performing long running task in scheduled job"); + // TODO(developer): add long running task here. + return Result.success(); + } +} \ No newline at end of file