问题
HandleNotificationOpened is working perfectly if App is on the background or running but it's not fired if i open the notification when app is closed.
I've tried to persist the data from event with SecureStorage, because i'm not sure if the event run but in the wrong time or it doesn't run at all.
public App()
{
OneSignal.Current.StartInit("onesignal-id").HandleNotificationOpened(HandleNotificationOpened).HandleNotificationReceived(HandleNotificationReceived).EndInit();
}
private async void HandleNotificationOpened(OSNotificationOpenedResult result)
{
var data = result.notification.payload.additionalData;
if (data != null)
{
data.TryGetValue("Title", out object Title);
data.TryGetValue("Conteudo", out object Conteudo);
data.TryGetValue("Link", out object RLink);
string lastvar = (Title.ToString().GetHashCode() + Conteudo.ToString().GetHashCode() + RLink.ToString().GetHashCode()).ToString();
if (!ChecarDB(lastvar))
{
InserirDB(Title.ToString(), Conteudo.ToString(), RLink.ToString());
}
await SecureStorage.SetAsync("UrlFromPush", RLink.ToString());
var page = new MainPage();
MessagingCenter.Send<MainPage>(page, "MudarURL");
}
}
Expected result is the application properly handle the event, No error messages at all.
回答1:
This method will not be called when app is closed.
Although I did not use OneSignal to push notifications , according to the Android/iOS system notification processing mechanism, when the app is closed, when the remote notification is received, the click notification will restart the app, and the notification processing mechanism is processed by the system tray.
So the HandleNotificationOpened
method will not be called.
回答2:
I've solved this issue using URI Scheme to access early init background data, replacing the HandleNotificationOpened
method for Intent?.Data?.EncodedQuery;
with custom formatting method, that's how i got the expected result.
来源:https://stackoverflow.com/questions/57659354/handlenotificationopened-are-not-handled-if-app-is-closed-before-i-open-the-noti