Reg*_*_AG 13 xcode notifications startup push-notification ios
当点击通知启动应用程序时,我希望我的应用程序执行特定的操作.当应用程序已经运行到后台时我想做这些特定的事情但是当通过点击通知启动应用程序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(回答我的问题!:))
这是我用来调试我的应用程序但是...这个程序错了!
launchOptions equal to nil is due to the fact that relaunching the app with XCode (in this case with the "Waiting for my MyApp to launch" option) deletes the notifications even if it is still displayed in the status bar...
To be able to debug check what is the content of launchOptions after a relaunch of the app from scratch by a click on a notification, it seems that the only way is to display this content in a UIAlert as mentioned in answer by Tammo Freese. So, use the following to debug in this specific case:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
Thanks all for your help !!!!
Tam*_*ese 19
我在您分享的代码中找不到错误.通常这应该在模拟器和设备上都有效.这是一个适合我的示例:首先生成一个新的Single View iPhone应用程序(ARC和故事板上).然后更改以下两个方法AppDelegate
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeInterval:10.0 sinceDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Just some text";
localNotif.alertAction = @"OK";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = @{@"test": @YES};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Run Code Online (Sandbox Code Playgroud)
然后启动此应用程序,按主页按钮,然后停止应用程序.如果您点击主页按钮后10秒内出现的本地通知,您应该看到类似这样的内容,其中显示本地通知已传递给-application:didFinishLaunchingWithOptions:
:
我的建议是:首先得到我上面发布的示例以确保您的设置没有出错,然后检查您在代码中做的不同.
编辑
这适用于问题的编辑2:在等待应用程序启动时(在模拟器中,而不是在设备上),本地通知似乎也适用于我.试试这个:
归档时间: |
|
查看次数: |
18094 次 |
最近记录: |