【发布时间】:2019-10-04 21:51:10
【问题描述】:
问题:3 种不同上下文中的不同行为
好的,好的,在 iOS 中,推送通知似乎会发生三种不同的事情:
- 当应用在前台不时收到推送通知
- 通知中心出现了一些东西
- 如果通过点击通知打开应用程序,会调用
AppDelegate.DidReceiveRemoteNotification(...)或AppDelegate.ReceivedRemoteNotification(...),显然取决于实现的是哪一个(??)。 - 如果打开应用程序没有点击通知,只有
AppDelegate.WillEnterForeground(...)被调用,没有任何明确提及通知,并且没有别的碰巧承认这一点收到通知。
- 当应用程序在前台收到推送通知时,它会导致
UNUserNotificationCenterDelegate(如果有)执行UNUserNotificationCenterDelegate.WillPresentNotification(...)。李>
方法:从所有上下文路由到一个方法
因此,要使用 Push 覆盖所有基础,我需要在所有三种方法中实现一些东西:AppDelegate.DidReceiveRemoteNotification(...) / AppDelegate.ReceivedRemoteNotification(...)、AppDelegate.WillEnterForeground(...) 和 UNUserNotificationCenterDelegate .WillPresentNotification(...)。
这里有一些存根来展示我处理这一切的方法。
首先,我创建了一个自定义UNUserNotificationCenterDelegate,带有一个Shared 静态成员:
public class IncomingNotificationHandler : UNUserNotificationCenterDelegate
{
public static IncomingNotificationHandler Shared = new IncomingNotificationHandler();
...
}
其次,在该类中,我创建了一个处理程序,在每种情况下我都可以路由到该处理程序(再次强调,这只是一个用于调试目的的存根):
//sets all parameters to null by default, so it can be called from methods
//that don't know anything about notifications:
public void HandleNotificationsIfAny(UIApplication application = null,
NSDictionary userInfo = null,
Action<UIBackgroundFetchResult> completionHandler = null)
{
//checks if userInfo is null, and logs its conclusions about that:
if (userInfo == null)
{
//In the null case, we can get pending notifications from
//UNUserNotificationCenter:
UNNotification[] pendingNotifications = new UNNotification[] { };
UNUserNotificationCenter.Current.GetDeliveredNotifications(returnedValue => pendingNotifications = returnedValue);
//Then we log the number of pending notifications:
Debug.WriteLine("IncomingNotificationHandler: HandleNotificationsIfAny(...): delivered notification count: " + pendingNotifications.Length);
//And make note of where this was probably called from:
Debug.WriteLine("IncomingNotificationHandler: HandleNotificationsIfAny(...): may have been called from this.WillPresentNotification(...) OR AppDelegate.WillEnterForeground(...)");
return;
});
}
else
{
//In the non-null case, we log the userInfo
Debug.WriteLine("IncomingNotificationHandler: HandleNotificationsIfAny(...): just got info: " + userInfo);
//And make note of where this was probably called from:
Debug.WriteLine("IncomingNotificationHandler: HandleNotificationsIfAny(...): may have been called from AppDelegate.DidReceiveRemoteNotification(...)");
}
}
第三,在同一个类中,我实现了UNUserNotificationCenterDelegate 所需的单个方法,并从中路由到处理程序:
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
HandleNotificationsIfAny();
}
第四,也是最后,在AppDelegate 中,我从两个相关方法路由到同一个处理程序:
//I prefer using DidReceiveRemoteNotification because in my experience
//the other one is sometimes not reliable:
public override void DidReceiveRemoteNotification(UIApplication application,
NSDictionary userInfo,
Action<UIBackgroundFetchResult> completionHandler)
{
//Simply passing on all the parameters called in this method:
IncomingNotificationHandler.Shared.HandleNotificationsIfAny(application, userInfo, completionHandler);
}
//WillEnterForeground also calls the handler without any parameters
//because it doesn't automatically know anything about notifications:
public override void WillEnterForeground(UIApplication application)
{
IncomingNotificationHandler.Shared.HandleNotificationsIfAny();
}
就目前而言,我认为我正在以相同的方式处理通知事件,无论我的应用如何收到有关它的警报,即使它根本没有收到警报。
有谁知道我现在是否已经涵盖了它,或者我是否需要处理其他一些情况?
【问题讨论】:
标签: ios xamarin.forms push-notification