Xamarin.forms Xamarin-推送通知,OnMessageReceived会多次触发,加载多个页面
伙计们,我有个问题 我想加载一个显示推送通知字符串内容的页面,但OnReceivedMessage()被触发的次数越来越多,相当于我返回显示页面的次数 因此,我第一次运行时,OnRevceivedMessage会触发,打开我关闭的DisplayPage(),并通过PopAsync()返回到上一页。第二次我与邮递员一起发送通知并循环加载DisplayNotificationPage,然后返回时,OnReceivedMessage()会触发两次,然后每次运行都会触发三次,以此类推 发生了什么事?我认为这与订阅有关。。我怎样才能解决这个问题Xamarin.forms Xamarin-推送通知,OnMessageReceived会多次触发,加载多个页面,xamarin.forms,push-notification,navigation,Xamarin.forms,Push Notification,Navigation,伙计们,我有个问题 我想加载一个显示推送通知字符串内容的页面,但OnReceivedMessage()被触发的次数越来越多,相当于我返回显示页面的次数 因此,我第一次运行时,OnRevceivedMessage会触发,打开我关闭的DisplayPage(),并通过PopAsync()返回到上一页。第二次我与邮递员一起发送通知并循环加载DisplayNotificationPage,然后返回时,OnReceivedMessage()会触发两次,然后每次运行都会触发三次,以此类推 发生了什么事?我认
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NotificationReceivingPage : ContentPage
{
public NotificationReceivingPage ()
{
InitializeComponent ();
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<object, string>(this, App.NotificationReceivedKey, OnMessageReceived);
}
private void OnMessageReceived(object sender, string incomingNotificationBody) // gets triggered x1, x2, x3... and so on
{
if (!string.IsNullOrEmpty(incomingNotificationBody))
{
Navigation.PushAsync(new NotificationDisplayPage(incomingNotificationBody));
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<object>(this, App.NotificationReceivedKey);
}
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NotificationDisplayPage : ContentPage
{
ISimpleAudioPlayer soundPlayer;
private static string incomingMessage = null;
//default constructor
public AlarmDisplayPage ()
{
InitializeComponent ();
}
// overloaded constructor that receives incoming notification body
public AlarmDisplayPage(string messageBody)
{
InitializeComponent();
if (messageBody != null)
{
incomingMessage = messageBody;
}
//alarm to sound on incoming message
var assembly = typeof(AlarmDisplayPage);
alertHeader.Source = ImageSource.FromResource("ShakeAlarmRR1.Assets.Images.test-event-header.png", assembly);
}
protected override void OnAppearing()
{
base.OnAppearing();
stopAlarmButton.IsEnabled = true;
//initialize soundplayer
soundPlayer = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
soundPlayer.Load(GetStreamFromFile("alarm.mp3"));
if (!string.IsNullOrEmpty(incomingMessage))
{
setEventDetails();
//play the alarm
soundPlayer.Play();
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
soundPlayer.Stop();
incomingMessage = null;
}
private Stream GetStreamFromFile(string mediaFilename)
{
var assembly = typeof(App).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("ShakeAlarmRR1.Assets.Sounds." + mediaFilename);
return stream;
}
private void stopAlarmButton_Clicked(object sender, EventArgs e)
{
stopAlarmButton.IsEnabled = false;
Navigation.PopToRootAsync();
}
private void setEventDetails()
{
if (!string.IsNullOrEmpty(incomingMessage))
{
labelEventType.Text = incomingMessage;
}
else
{
labelEventType.Text = "No Data";
}
}
}
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类通知接收页面:内容页面
{
公共通知接收页()
{
初始化组件();
}
出现时受保护的覆盖无效()
{
base.OnAppearing();
MessagingCenter.Subscribe(此,App.NotificationReceivedKey,OnMessageReceived);
}
private void OnMessageReceived(对象发送方,字符串incomingNotificationBody)//触发x1、x2、x3…等等
{
如果(!string.IsNullOrEmpty(incomingNotificationBody))
{
PushAsync(新的NotificationDisplayPage(incomingNotificationBody));
}
}
受保护的覆盖无效OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe(此,App.NotificationReceivedKey);
}
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类通知显示页面:内容页面
{
ISimpleAudioPlayer声音播放器;
私有静态字符串incomingMessage=null;
//默认构造函数
公共报警显示页()
{
初始化组件();
}
//接收传入通知正文的重载构造函数
公共报警显示页面(字符串消息体)
{
初始化组件();
if(messageBody!=null)
{
incomingMessage=messageBody;
}
//在收到信息时发出警报
var assembly=typeof(AlarmDisplayPage);
alertHeader.Source=ImageSource.FromResource(“ShakeAlarmRR1.Assets.Images.test event header.png”,程序集);
}
出现时受保护的覆盖无效()
{
base.OnAppearing();
stopAlarmButton.IsEnabled=true;
//初始化声音播放器
soundPlayer=Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
Load(GetStreamFromFile(“alarm.mp3”);
如果(!string.IsNullOrEmpty(incomingMessage))
{
setEventDetails();
//播放闹钟
soundPlayer.Play();
}
}
受保护的覆盖无效OnDisappearing()
{
base.OnDisappearing();
soundPlayer.Stop();
incomingMessage=null;
}
私有流GetStreamFromFile(字符串mediaFilename)
{
var assembly=typeof(App).GetTypeInfo().assembly;
var stream=assembly.GetManifestResourceStream(“ShakeAlarmRR1.Assets.Sounds.+mediaFilename”);
回流;
}
私有void stopAlarmButton_已单击(对象发送者,事件参数e)
{
stopAlarmButton.IsEnabled=false;
Navigation.PopToRootAsync();
}
私有void setEventDetails()
{
如果(!string.IsNullOrEmpty(incomingMessage))
{
labelEventType.Text=输入消息;
}
其他的
{
labelEventType.Text=“无数据”;
}
}
}
在OnAppearing事件中处理消息后,请尝试设置incomingMessage=null;
或另一次一次性检查。可能在OnAppearing事件中触发错误,但中间没有ondissapering。如果是这种情况,请移动订阅构造函数。@NickKovalsky我通过添加IssuSubscribed布尔类变量来解决它。这boolean跟踪页面是否已订阅,使用该页面时,订阅和取消订阅仅取决于页面的当前订阅状态。问题是我每次订阅OnAppear()被触发,所以订阅发生了多次。我不知道的是,多次订阅会导致OnMessageReceived()被触发多次。