I have a web/android app, written using Ionic 4 and Capacitor, and I've been trying without success to re-enter the Ionic app to a specific page from a notification raised from an Android service (activated via a capacitor plugin).
Here's the code that creates the notification in the service:
private Notification getNotification() {
CharSequence contentTitle = "Fun App Background Mode Running";
CharSequence contentText = "Fun App";
long notificationTime = System.currentTimeMillis();
if (_NFC == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("funapp", "FunApp", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(false);
channel.enableVibration(false);
channel.setSound(null,null);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setShowBadge(true);
manager.createNotificationChannel(channel);
}
Intent notificationIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(notificationIntent);
PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
_NFC = new NotificationCompat.Builder(getApplicationContext(),"funapp")
.setSmallIcon(R.drawable.ic_sheep_notif)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_foreground))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setStyle(new NotificationCompat.BigTextStyle().bigText(contentText).setBigContentTitle(contentTitle))
.setContentIntent(pendingIntent)
.setOngoing(true);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
_NFC.setPriority(NotificationCompat.PRIORITY_LOW);
}
}
_NFC.setContentTitle(contentTitle);
_NFC.setContentText(contentText);
_NFC.setStyle(new NotificationCompat.BigTextStyle().bigText(contentText).setBigContentTitle(contentTitle));
_NFC.setWhen(notificationTime);
return _NFC.build();
}
I believe I need to put something in/around the new Intent(this, MainActivity.class) line to get Capacitor / Ionic to initialise the app to the right state, but I cannot work out what that should be!
I've poured over the Capacitor documentation and not been able to find the solution so far, I have a suspicion that I need to send a "view" intent to the activity with some sort of URL?
The current behavior is for it to start what appears to be a completely new instance of the application (it re-loads the splash screen etc), even if the app is still the foreground task on the phone.
UPDATE
My latest attempt is to create the intent like this:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://localhost/event/horse"),
this, MainActivity.class);
(assuming I have a valid route set up in Ionic/Angular for /event/horse, which I do)
No change though, this still expresses the same behavior as described above (re-entering the splash screen).