I have a Xamarin multi-platform app and I'm using the FirebasePushNotification Plugin v3.4.35 and am working on the Android app. Their instructions suggest the addition of a MainApplication : Application class to hold their handlers which I've done. In the OnCreate() method there is a CrossFirebasePushNotification.Current.OnNotificationReceived handler that fires when a push notification is received when the app is in the foreground. It fires as expected and I can capture the notification when it arrives. On my Main Page (called OverViewPage) I have an imagebutton who's IsVisible property is bound to a HaveNotification bool in my BaseViewModel, which is inherited by all other VMs. When the OnNotificationReceived fires I set the HaveNotification = true, and that appears to be working. But the problem is that the icon on the OverViewPage is not responding to the property change and does not show. Is this because of where I'm changing the HaveNotification value? How do I get the icon to be visible when the OnNotificationReceived event fires? thanks in advance. Code Below: MainApplication Class
public class MainApplication : Application
{
public MainApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
//... removed meaningless stuff
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Received");
BaseViewModel baseviewmodel = new BaseViewModel
{
HaveNotification = true
};
};
BaseViewModel
public class BaseViewModel : INotifyPropertyChanged
{
bool haveNotification = false;
public bool HaveNotification
{
get { return haveNotification; }
set { SetProperty(ref haveNotification, value); }
}
protected bool SetProperty<T>(ref T backingStore, T value,
[CallerMemberName] string propertyName = "",
Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
OverViewPage
<StackLayout Orientation="Horizontal" Margin="0,0,0,0" WidthRequest="250" BackgroundColor="{StaticResource Primary }">
<ImageButton x:Name="ibtnNotify" IsVisible="{Binding HaveNotification}" Source="@drawable/icon_msg.png" BackgroundColor="Yellow" HorizontalOptions="End"/>
</StackLayout>
The problem is that you create another new instance of class BaseViewModel
when you receive a message.
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Received");
BaseViewModel baseviewmodel = new BaseViewModel
{
HaveNotification = true
};
};
You can try to change the value of HaveNotification
by the instance of ViewModel that bind to Page OverViewPage
when you receive notification.
Note:
You can use MessagingCenter to pass message from xamarin android project to Page OverViewPage
on xamarin form project while receiving notification. And when you reveive message on page OverViewPage
, then you can change the value of HaveNotification
by the instance of ViewModel that bind to Page OverViewPage
.