我尝试在我的ios应用程序中使用Onesignal Notifications,在使用Xamarin的Visual Studio中创建。我想在用户点击通知时让应用程序打开ViewController,但它不起作用。当用户点击通知时,应用程序会打开ViewController,当用户应用程序转到后台时该窗口处于活动状 希望有人表现出来,我犯了错误 代码如下:
public class AppDelegate : UIApplicationDelegate
{
public static UIStoryboard Storyboard = UIStoryboard.FromName("Main", null);
public override UIWindow Window {
get;
set;
}
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
OneSignal.Current.StartInit("mykey")
.InFocusDisplaying(OSInFocusDisplayOption.None)
.HandleNotificationOpened(HandleNotificationOpened)
.EndInit();
OneSignal.Current.IdsAvailable(IdsAvailable);
return true;
}
void IdsAvailable(string userID, string pushToken)
{
GlobalUserInfo._token = userID;
}
private static void HandleNotificationOpened(OSNotificationOpenedResult result)
{
OSNotificationPayload payload = result.notification.payload;
Dictionary<string, object> additionalData = payload.additionalData;
if (additionalData != null)
{
UINavigationController NavigationController = (UINavigationController)Storyboard.InstantiateInitialViewController();
NotificationsViewController notificationsController = Storyboard.InstantiateViewController("NotificationsViewController") as NotificationsViewController;
NavigationController.PushViewController(notificationsController, true);
}
}
}
NotificationsViewController 是收到的所有通知用户的简单列表。
已更新 正如我所注意到的,函数HandleNotificationOpened在iOS中不起作用。
已更新 发现错误。我忘了,我在其他地方使用OneSignal Init。当我删除重复时,HandleNotificationOpened开始正常工作。
答案 0 :(得分:1)
您应该找到当前的根ViewController而不是创建新实例。所以请修改如下:
if (additionalData != null)
{
UINavigationController NavigationController = (UINavigationController)Window.RootViewController;
NotificationsViewController notificationsController = Storyboard.InstantiateViewController("NotificationsViewController") as NotificationsViewController;
NavigationController.PushViewController(notificationsController, true);
}
此外,如果根ViewController是UITabBarController,请提供此视图控制器(NotificationsViewController)。