CrossGeeks / FirebasePushNotificationPlugin

Firebase Push Notification Plugin for Xamarin iOS and Android
MIT License
396 stars 177 forks source link

App Reload on Push Notification Click with Splash Activity Xamarin Forms #79

Closed AnthonyLatty closed 6 years ago

AnthonyLatty commented 6 years ago

This is a continuation of issue #11

I have this Application that has a Splash Activity as its main launcher followed by the Main Activity. When its just the Main Activity i have this:

 [Activity(LaunchMode = LaunchMode.SingleTop, Label = "App", Icon = "@drawable/app_logo", Theme = "@style/MainTheme", ScreenOrientation = ScreenOrientation.Portrait, MainLauncher = true)]

Also an overidden method:

protected override void OnNewIntent(Intent intent)
 {
         FirebasePushNotificationManager.ProcessIntent(intent);
 }

This works as expected with just only a Main Activity as your launcher. The issue i am having is for my instance i have a Splash Activity as my main launcher, I tried switching around the implementation but still no success.

What am i missing?

rdelrosario commented 6 years ago

If using latest version you can assign your MainActivity as the one to be opened when notification is clicked:

FirebasePushNotificationManager.NotificationActivityType = typeof(MainActivity);

rdelrosario commented 6 years ago

That should fix your issue if not let me know

AnthonyLatty commented 6 years ago

This solution does not fix the issue.

rdelrosario commented 6 years ago

How are tou initializing the plugin?

AnthonyLatty commented 6 years ago

In my MainActivity class i have :

protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(bundle);
            Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
            FirebasePushNotificationManager.ProcessIntent(Intent);
            ImageCircleRenderer.Init();
        }

Splash Activity is :


protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            var intent = new Intent(this, typeof(MainActivity));
            FirebasePushNotificationManager.NotificationActivityType = typeof(MainActivity);
            StartActivity(intent);
            Finish();
        }
AnthonyLatty commented 6 years ago

Question can i open a XAML view instead of an Activity?

rdelrosario commented 6 years ago

No has to be an android activity. Can you put here your initialization code on the MainApplication class

AnthonyLatty commented 6 years ago

Application class:

 public override void OnCreate()
        {
            base.OnCreate();

            //If debug you should reset the token each time.
#if DEBUG
            FirebasePushNotificationManager.Initialize(this,true);
#else
            FirebasePushNotificationManager.Initialize(this, false);
#endif

            //Handle notification when app is closed here
            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
            {

            };
        }
rdelrosario commented 6 years ago

Set this on your MainApplication class instead of where you have it now:

FirebasePushNotificationManager.NotificationActivityType = typeof(MainActivity);

Szymaniuk commented 6 years ago

Any full working solution for MainApplication + SplashActivity (used only for splash image and starting MainActivity) + MainActivity (where we would like to be able to handle notifications)?

I didn't have any issues with MainApplication + MainActivity, I could simply extract my notification details from Intent.Extras, but I also do have troubles, when using SplashActivity (even with FirebasePushNotificationManager.NotificationActivityType = typeof(MainActivity); code inside MainApplication).

rdelrosario commented 6 years ago

I will create a working sample of use with splash but bare with me might take me a few days because of other commitments I currently have.

Szymaniuk commented 6 years ago

Great, thanks a lot! Really great plugin 👍 . Can't wait for splash example :).

Szymaniuk commented 6 years ago

@rdelrosario any news about SplashScreen sample :)?

rdelrosario commented 6 years ago

Getting back to this. Sorry guys I was on a trip but now back. Will get that sample going this week

Niach commented 6 years ago

Can't get it to work as well. What is the current state of the sample?

simon25608 commented 6 years ago

I have a splash and it works correctly, the main activity is opened. However, I want to redirect the user to specific xaml, how can I get the notification data?

For example, when the app is not in, I do the following: CrossFirebasePushNotification.Current.OnNotificationOpened += async (s, p) => { await NotificationsHelper.SendToDetailNotification(p, NavigationService); }; I am using Prism for navigation.

AnthonyLatty commented 6 years ago

I dont think this plugin supports that at this moment, navigation can only take place with the Activities that are in the android specific project but not in the Xamarin.forms project. I have a similar issue, i am still trying to find a solution.

@rdelrosario Will this be a feature implemented in the future releases or is there an existing solution?

luismts commented 6 years ago

@AnthonyLatty try to use LaunchMode = LaunchMode.SingleTask in your MainActivity even if you have a SplashActivity.

I hope that can help. Regards!

simon25608 commented 6 years ago

@luismts Could you explain how your solution could help us with the problem of navigation? I don't understand.

Applying that solution, the result is the same.

Thanks

luismts commented 6 years ago

@simon25608

The title of this theme is that your app is reloaded when you tap on the push notification; This is solved by using LaunchMode = LaunchMode.SingleTask in your MainActivity.

Now, if you want to open a specific page in Forms you must do it through the OnNotificationOpened event.

For the OnNotificationOpened event to be triggered, you need to process the intent in the MainLauncher as shown below:

 public class SplashActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            StartActivity(typeof(MainActivity));

            FirebasePushNotificationManager.ProcessIntent(Intent);
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            FirebasePushNotificationManager.ProcessIntent(intent);
        }
    }

After this you can use a payload like this:

{
    "data":{
       "open_page": "MyPage"
    },
    "notification": {
        "body" : "Probando notificiones pushh",
       "content_available" : true
     },
    "condition": "'general' in topics",
        "priority": "high"
}

And in you App.css you can do something like this:

CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
{
           NavigationService.Navigate(p.Data["open_page"]);     
};

Of course, this is gonna happen after you app is loaded. But you can manage this.

I hope that can help. Regards!

simon25608 commented 6 years ago

@luismts

Yes, I'm already doing that and it works perfectly :)

What doesn't work, or isn't implemented, is when the application is completely closed, the OnNotificationOpened event trigger and what it does is launch the app.

Where could it be intercepted? Or how to do it? Or just know if it's not possible.

Thanks for you help.

edit:

The OnNotificationOpened event if launched in the MainApplication class, what I don't know is how to pass the information to the main activity and navigate to the page I want, instead of opening the main one of the application.

luismts commented 6 years ago

@simon25608

Have you tried using the OnNotificationOpened event in Forms?

Try to use it after your application has started. For example in your app.cs in the OnStart method. The event is triggered after the app started.

simon25608 commented 6 years ago

Yes, it is as I am using it and it works perfectly when the application is active or in the background, when it is closed this event is not triggered in forms, it is triggered in the MainApplication and I don't know how the notification data should be passed to MainActivity which initiates the app again.

I'll show you how I actually have it: App.cs: https://pastebin.com/gUpz1e1B

MainApplication.cs: https://pastebin.com/g30j44MX

MainActivity.cs: https://pastebin.com/taxARNLM

Regards

luismts commented 6 years ago

@simon25608

If you are using prism be sure that your OnStart method is being triggered. There is an issue with that part, try using something like this:

protected override void OnStart()
{
            base.OnStart();
            System.Diagnostics.Debug.WriteLine("============================== start");
 }

In another hand you can use it after call your NavigationService. For example:

 protected override async void OnInitialized()
 {
            InitializeComponent();

            await NavigationService.PushPageWithMenuAsync<object>(nameof(StartPage), null);  
            FirebaseSettings();
 }

Or you can do the same in your StartPageViewModel in your ctor. Another thing, you can not use breakpoints in OnStart method. As I said, there is an issue.

Bring me your feedback, regards!

simon25608 commented 6 years ago

@luismts

Can you pass me the link to the issue? Because with the app launched if you run the OnStart method

I've tried what you're saying and it already seems to be working correctly :)

Anyway, I had already found a solution, in MainApplication the OnNotificationOpened method was triggered and from there I sent the notification data to the PCL.

Thank you very much for your help :)

rdelrosario commented 6 years ago

Hey guys sorry all for the big delay I just updated the sample on branch release-2.2.5

You can find it at this url:

https://github.com/CrossGeeks/FirebasePushNotificationPlugin/tree/release-2.2.5/samples/FirebasePushSample

Is very important to set your NotificationActivityType to your main activity if using Forms and have an splash activity:

FirebasePushNotificationManager.NotificationActivityType = typeof(MainActivity);

Note: Generally will do this on Android Main Application class

Also important to subscribe to events on OnStart of App.cs or after that to make sure Forms is already started and ready to receive events.

In this sample, you will see that if you send a hex color code on "color" key inside "data" payload it will navigate to a page with that color as the background.

Sample Payload:

{
    "data": {
        "message" : "my_custom_value",
        "other_key" : true,
        "body":"tordnads",
         "color":"#3333ff",
          "priority": "high"
     },
         "condition": "'general' in topics"
}

I used color key just as a sample to demostrate navigation but you could use your own keys based on your use case.

I suggest you install:

https://www.nuget.org/packages/Plugin.FirebasePushNotification/2.2.5-alpha

Has an important bug fix concerning to this. Will be releasing this officially in a few days.

luismts commented 6 years ago

@simon25608

Update to the alpha version (2.2.5) as Rendy says. You don't need to use MainApplication.cs, you only need to subscribe OnNotificationOpened event on OnStart method as it's shown in the following code:

protected override void OnStart()
{
            base.OnStart();

            CrossFirebasePushNotification.Current.Subscribe("general"); //Subscribing to single topic
            CrossFirebasePushNotification.Current.OnNotificationOpened += MyOnNotificationOpenedMethod; //Handle push notification 
} 

OnNotificationOpened is triggered when the app has started. Try to implement this and give us your feedback. Regards!

rdelrosario commented 6 years ago

Just updated the sample using splash and released the official version:

https://www.nuget.org/packages/Plugin.FirebasePushNotification/2.2.7

Let me know any questions or issues you have.

mwagdy83 commented 5 years ago

i tried what you did in your sample but still not working