从后台杀死应用程序后,UILocalnotification无法正常工作

时间:2022-07-10 01:26:37

I am working with UIlocalnotifications, So whenever a notification pops up,and user clicks on it I open ViewController , so whenever my app is in background, and when notification pops up and when user clicks on it or on the app icon, I am able to show the ViewController.

我正在使用UIlocalnotifications,所以每当弹出一个通知,并且用户点击它时我打开ViewController,所以每当我的应用程序处于后台时,当弹出通知并且当用户点击它或应用程序图标时,我能够显示ViewController。

But whenever user kills the app from background, and when notification occurs, if he clicks on notification, I am able to show the ViewController, but if I click on app Icon, I am not able to show ViewController

但是每当用户从后台杀死应用程序时,当通知发生时,如果他点击通知,我就能够显示ViewController,但是如果我点击app Icon,我就无法显示ViewController

Below is my code

以下是我的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (locationNotification) {
        // Set icon badge number to zero
        application.applicationIconBadgeNumber = 0;


       AlarmViewController *alarmView = [[AlarmViewController alloc]initWithNibName: @"AlarmViewController" bundle:nil];    
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:alarmView];
    [self.window.rootViewController presentViewController:navController animated:YES completion:nil];

    }
}

I tried to debug this problem, and what I found is that, whenever I click on the app icon, its not getting any localnotification.Only if I click on the notification, I get a localnotification. I dont know whenether this is a bug, or whether I am missing something.

我试图调试这个问题,我发现的是,每当我点击应用程序图标时,它都没有得到任何本地通知。只有点击通知,我才会得到本地通知。我不知道这是一个错误,或者我是否遗漏了一些东西。

So please help me out

所以请帮帮我

Regards Ranjit.

关心Ranjit。

1 个解决方案

#1


5  

It's not a bug! Handling Local and Remote Notifications in Apple's development library says

这不是一个错误!处理Apple开发库中的本地和远程通知说

Handling Local and Remote Notifications

处理本地和远程通知

Let’s review the possible scenarios when the system delivers a local notification or a remote notification for an application.

让我们回顾一下系统为应用程序发送本地通知或远程通知时的可能情况。

The notification is delivered when the application isn’t running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.

当应用程序未在前台运行时,将传递通知。在这种情况下,系统会显示通知,显示警报,标记图标,或者播放声音。

As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

作为所呈现的通知的结果,用户点击警报的动作按钮或点击(或点击)应用程序图标。如果点击操作按钮(在运行iOS的设备上),系统将启动应用程序,应用程序将调用其委托的应用程序:didFinishLaunchingWithOptions:method(如果已实现);它传递通知有效负载(用于远程通知)或本地通知对象(用于本地通知)。

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload.

如果在运行iOS的设备上轻触应用程序图标,则应用程序会调用相同的方法,但不会提供有关通知的信息。如果在运行OS X的计算机上单击应用程序图标,则应用程序将调用委托的applicationDidFinishLaunching:方法,其中委托可以获取远程通知负载。

iOS Note: The application delegate could implement applicationDidFinishLaunching: rather than application:didFinishLaunchingWithOptions:, but that is strongly discouraged. The latter method allows the application to receive information related to the reason for its launching, which can include things other than notifications. The notification is delivered when the application is running in the foreground. The application calls its delegate’s application:didReceiveRemoteNotification: method (for remote notifications) or application:didReceiveLocalNotification: method (for local notifications) and passes in the notification payload or the local-notification object.

iOS注意:应用程序委托可以实现applicationDidFinishLaunching:而不是application:didFinishLaunchingWithOptions:,但强烈建议不要这样做。后一种方法允许应用程序接收与其启动原因相关的信息,其中可包括除通知之外的其他信息。当应用程序在前台运行时,将传递通知。应用程序调用其委托的应用程序:didReceiveRemoteNotification:method(用于远程通知)或application:didReceiveLocalNotification:方法(用于本地通知)并传入通知有效内容或本地通知对象。

#1


5  

It's not a bug! Handling Local and Remote Notifications in Apple's development library says

这不是一个错误!处理Apple开发库中的本地和远程通知说

Handling Local and Remote Notifications

处理本地和远程通知

Let’s review the possible scenarios when the system delivers a local notification or a remote notification for an application.

让我们回顾一下系统为应用程序发送本地通知或远程通知时的可能情况。

The notification is delivered when the application isn’t running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.

当应用程序未在前台运行时,将传递通知。在这种情况下,系统会显示通知,显示警报,标记图标,或者播放声音。

As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

作为所呈现的通知的结果,用户点击警报的动作按钮或点击(或点击)应用程序图标。如果点击操作按钮(在运行iOS的设备上),系统将启动应用程序,应用程序将调用其委托的应用程序:didFinishLaunchingWithOptions:method(如果已实现);它传递通知有效负载(用于远程通知)或本地通知对象(用于本地通知)。

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload.

如果在运行iOS的设备上轻触应用程序图标,则应用程序会调用相同的方法,但不会提供有关通知的信息。如果在运行OS X的计算机上单击应用程序图标,则应用程序将调用委托的applicationDidFinishLaunching:方法,其中委托可以获取远程通知负载。

iOS Note: The application delegate could implement applicationDidFinishLaunching: rather than application:didFinishLaunchingWithOptions:, but that is strongly discouraged. The latter method allows the application to receive information related to the reason for its launching, which can include things other than notifications. The notification is delivered when the application is running in the foreground. The application calls its delegate’s application:didReceiveRemoteNotification: method (for remote notifications) or application:didReceiveLocalNotification: method (for local notifications) and passes in the notification payload or the local-notification object.

iOS注意:应用程序委托可以实现applicationDidFinishLaunching:而不是application:didFinishLaunchingWithOptions:,但强烈建议不要这样做。后一种方法允许应用程序接收与其启动原因相关的信息,其中可包括除通知之外的其他信息。当应用程序在前台运行时,将传递通知。应用程序调用其委托的应用程序:didReceiveRemoteNotification:method(用于远程通知)或application:didReceiveLocalNotification:方法(用于本地通知)并传入通知有效内容或本地通知对象。