相关疑难解决方法(0)

如何在点击通知时处理Swift 3中的启动选项?获得语法问题

我正在尝试处理启动选项并在点击我在swift 3中收到的远程通知时打开一个特定的视图控制器.我已经看到类似的问题,例如这里,但没有新的swift 3实现.我看到了一个类似的问题(和)在AppDelegate.swift中我在didFinishLaunchingWithOptions中有以下内容:

    var localNotif = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! String)
if localNotif {
    var itemName = (localNotif.userInfo!["aps"] as! String)
    print("Custom: \(itemName)")
}
else {
    print("//////////////////////////")
}
Run Code Online (Sandbox Code Playgroud)

但是Xcode给了我这个错误:

Type '[NSObject: AnyObject]?' has no subscript members
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

   if let launchOptions = launchOptions {
        var notificationPayload: NSDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary!

    }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

error: ambiguous reference to member 'subscript'
Run Code Online (Sandbox Code Playgroud)

我有类似的错误,无论我以前使用类似的代码通过键从字典中获取值,我不得不替换代码,基本上安全地首先解开字典.但这似乎不适用于此.任何帮助,将不胜感激.谢谢.

ios firebase swift firebase-cloud-messaging firebase-notifications

17
推荐指数
3
解决办法
2万
查看次数

XCode:为什么didFinishLaunchingWithOptions中的launchOptions总是为零?

当点击通知启动应用程序时,我希望我的应用程序执行特定的操作.当应用程序已经运行到后台时我想做这些特定的事情但是当通过点击通知启动应用程序FROM SCRATCH(没有运行到后台)时.

当通过点击通知从后台启动应用程序时,我通过以下方式获得通知:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Run Code Online (Sandbox Code Playgroud)

=>没有问题!

当通过点击通知从头开始启动应用程序时,我希望通过以下方式获取通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

}
Run Code Online (Sandbox Code Playgroud)

但是launchOptions总是零!当应用程序通过点击应用程序图标(正常)从头开始,但是当通过点击通知(不正常)从头开始应用程序时,它是零.

有谁知道如何解决这个问题?

谢谢 !!!

编辑1

以下是我的通知创建方式(Joe问题):

  NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:notifText,latitudeString,longitudeString,nil]
forKeys:[NSArray arrayWithObjects:@"notifText",@"latitude",@"longitude", nil]];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody =msg;
    localNotif.alertAction = @"Ok";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.userInfo = userInfo;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
Run Code Online (Sandbox Code Playgroud)

编辑2(回答我的问题!:))

这是我用来调试我的应用程序但是...这个程序错了!

  1. 我在"编辑方案"菜单中设置了"等待MyApp.app启动"选项的调试器
  2. 我第一次使用XCode启动了我的应用程序(从头开始)XCode显示"等待我的MyApp启动"=>我点击我的应用程序图标启动应用程序
  3. 应用程序启动=>我点击主页按钮=>显示通知
  4. 我点击XCode中的停止按钮关闭应用程序我用XCode重新启动它=> XCode再次显示"等待我的MyApp启动"消息=>我点击状态栏中的通知启动应用程序
  5. => launchOptions is …

xcode notifications startup push-notification ios

13
推荐指数
1
解决办法
2万
查看次数