New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Android] Navigating to ViewModel from Android notification using PendingIntent #2418
Comments
The generic |
I've found I can use a Is there any MvvmCross 5 or 6 sample showing how to navigate to a ViewModel when an Android notification is clicked using the |
So... in order to fix this issue, we will need to:
Btw thanks for the input and update! Regarding your question, I don't think there's an example unfortunately... But you should be able to create a custom dependency service (interface & implementation at Core level) that handles incoming notifications. You just need to:
|
@nmilcoff would you be able to generate a PR that demonstrates this issue in the playground? |
@nmilcoff I did another trick, but have a different issue. I created a custom |
@Nickolas- What version of Mvx were you using before? I would probably start by trying to remove the dependency on |
I’m not using IMvxCurrentTopActivity in the BroadcastReceiver. But I found the problem in my logic. I was trying to present a MvxFragmentViewModel, but with no top current activity the presenter could not understand how to show it. Then I added the logic back. Now it shows the correct activity even in the background state. But it will not bring the activity to the front. I need to use Activity.LaunchMode = SingleTop + NewIntentHandle. |
@Nickolas- I have the exact same issue as you. When the app is in the background I cannot get it to resume the app and display the activity. Only when the app is opened then it navigates correctly from the broadcast receiver. I'm also just using NavigationService.Navigate(data) but my activity/vm doesn't contain fragments or isn't a fragment. When the navigate is called the VM initialized is called but after that I get the logs: and the app is not opened How were you able to resume the app when calling the navigate from the broad cast receiver? I ended up implementing my own workaround. Since navigation will work in onresume in an activity what I do is I call a different activity with a regular pending intent and navigate from there into the activity I really want to go. Simple, not perfect but works for what I need to do. Looking forward for the top activity fix though. |
Here's a working solution for an Android app using one activity and multiple fragments: For starters you want to specify the singleTop launch mode for your activity: [Activity(LaunchMode = LaunchMode.SingleTop, ...)]
public class MainActivity : MvxAppCompatActivity Generate the notification var intent = new Intent(Context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
// Putting an extra in the Intent to pass data to the MainActivity
intent.PutExtra("from_notification", true);
var pendingIntent = PendingIntent.GetActivity(Context, notificationId, intent, 0); Now there are places to handle this Intent from
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
if (bundle == null && Intent.HasExtra("from_notification"))
{
// The notification was clicked while the app was not running.
// Calling MvxNavigationService multiple times in a row here won't always work as expected. Use a Task.Delay(), Handler.Post(), or even an MvvmCross custom presentation hint to make it work as needed.
}
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
if (intent.HasExtra("from_notification"))
{
// The notification was clicked while the app was already running.
// Back stack is already setup.
// Show a new fragment using MvxNavigationService.
}
} |
I've made a simple Android project demonstrating the problem.
I asked a question on StackOverflow and was asked to create an issue here.
The problem I have is I can't figure out how to navigate to a
MvxViewModel
using aPendingIntent
. This was doable in MvvmCross 4.x. The same MvvmCross 4.x techniques work in MvvmCross 5.x, but the documentation states it's not good to mix the two navigation schemes together.Steps to reproduce 📜
Create an Android notification using
Notification.Builder
orNotificationCompat.Builder
.Create an
MvxViewModelRequest
for the ViewModel.Get a
PendingIntent
from theMvxViewModelRequest
.Associate the
PendingIntent
with the Android notification usingSetContentIntent
.Click on the notification. The activity is displayed but the new MvvmCross 5.x lifecycle is not fully implemented. Specifically
Prepare
is not called.Expected behavior 🤔
Prepare
should be called.Actual behavior 🐛
Prepare
is not called, so the ViewModel is essentially uninitialized. The following output can be observed:2017-11-28 04:10:00 [TRACE] (MvxNotificationNavigation.Core.ViewModels.NewViewModel) NewViewModel.ctor called
11-28 16:10:00.373 I/mvx ( 4616): 34.28 Missing parameter for call to NewViewModel - missing parameter parameter - asssuming null - this may fail for value types!
[0:] mvx:Diagnostic: 34.28 Missing parameter for call to NewViewModel - missing parameter parameter - asssuming null - this may fail for value types!
2017-11-28 04:10:00 [TRACE] (MvxNotificationNavigation.Core.ViewModels.NewViewModel) NewViewModel.Initialize called
Configuration 🔧
Version: 5.5
Platform:
The text was updated successfully, but these errors were encountered: