From fd874c14df5ff191f526b2a0dccd8ac3d671884a Mon Sep 17 00:00:00 2001 From: yenda Date: Mon, 15 Jun 2020 11:36:32 +0200 Subject: [PATCH] open app when tapping sticky notification --- .../ethereum/module/ForegroundService.java | 21 ++++++++++++++++++- .../module/NewMessageSignalHandler.java | 13 ++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/ForegroundService.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/ForegroundService.java index 77896293f6..7b724bc0d4 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/ForegroundService.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/ForegroundService.java @@ -7,6 +7,7 @@ import android.os.IBinder; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; +import android.app.PendingIntent; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; @@ -22,7 +23,7 @@ public class ForegroundService extends Service { } @Override - public int onStartCommand(Intent intent, int flags, int startId) { + public int onStartCommand(Intent i, int flags, int startId) { // NOTE: recent versions of Android require the service to display // a sticky notification to inform the user that the service is running Context context = getApplicationContext(); @@ -35,6 +36,23 @@ public class ForegroundService extends Service { "Status Service", NotificationManager.IMPORTANCE_HIGH)); } + Class intentClass; + String packageName = context.getPackageName(); + Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName); + String className = launchIntent.getComponent().getClassName(); + try { + intentClass = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return 0; + } + + Intent intent = new Intent(context, intentClass); + intent.addCategory(Intent.CATEGORY_BROWSABLE); + intent.setAction(Intent.ACTION_VIEW); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); + String content = "Keep Status running to receive notifications"; Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_stat_notify_status) @@ -42,6 +60,7 @@ public class ForegroundService extends Service { .setContentText(content) .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_MESSAGE) + .setContentIntent(pendingIntent) .build(); // the id of the foreground notification MUST NOT be 0 startForeground(1, notification); diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/NewMessageSignalHandler.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/NewMessageSignalHandler.java index 27caffe247..5d2fe9d091 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/NewMessageSignalHandler.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/NewMessageSignalHandler.java @@ -87,10 +87,7 @@ public class NewMessageSignalHandler { Log.e(TAG, "Broadcast Receiver registered"); } - //NOTE: this method takes a chatId and returns an intent that will open the app in that chat - //Once we support other kind of notifications we will need to adapt it. The simplest method - //is probably to pass the universal link as param instead of the chatId. - public Intent getOpenAppIntent(String chatId) { + public Intent getOpenAppIntent() { Class intentClass; String packageName = context.getPackageName(); Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName); @@ -112,6 +109,14 @@ public class NewMessageSignalHandler { //flags in this link: //https://stackoverflow.com/questions/52390129/android-intent-setflags-issue intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); + return intent; + } + + //NOTE: this method takes a chatId and returns an intent that will open the app in that chat + //Once we support other kind of notifications we will need to adapt it. The simplest method + //is probably to pass the universal link as param instead of the chatId. + public Intent getOpenAppIntent(String chatId) { + Intent intent = getOpenAppIntent(); intent.setData(Uri.parse("status-im://chat/private/" + chatId)); return intent; }