当应用程序关闭时如何在ios中接收本地通知 [英] how to recieve local notification in ios when app is closed

查看:103
本文介绍了当应用程序关闭时如何在ios中接收本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为警报系统使用本地通知,但在处理本地通知时遇到了一些问题,当我点击警报通知(当应用程序关闭时)它会启动应用程序,但问题是它应该转到 didFinishLaunchingWithOptions 函数,但它不会进入 appDelegate 中的任何函数(我使用断点来检查).我正在使用带有导航控制器的故事板,我想在应用关闭时在通知点击时打开一个特定的视图控制器.

I am using local notification for alarm system but I am facing some problem while handling local notification, When i clicked on alarm notification (When app is closed) its launches app but the problem is it should go to didFinishLaunchingWithOptions function but it's not going inside any of the function in appDelegate (I used breakpoints to check). I'm using story board with navigation controller, I want to open a specific view controller on notification click when app is closed.

但是当我正常启动该应用程序时,它会进入 didFinishLaunchingWithOptions 函数.

but when I'm launching that app normally it's going inside didFinishLaunchingWithOptions function.

请提出建议.

任何帮助将不胜感激.

main.m

#import "DEMOAppDelegate.h"
int main(int argc, char * argv[])
{
    @autoreleasepool 
    {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([DEMOAppDelegate class]));
    }
}

DEMOAppDelegate.m

#import "DEMOAppDelegate.h"        
@implementation DEMOAppDelegate        
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) 
    {
        NSLog(@"Recieved Notification %@",localNotif);
    }
    return YES;
}                                   
- (void)applicationWillResignActive:(UIApplication *)application 
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application 
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application 
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application 
{
}
- (void)applicationWillTerminate:(UIApplication *)application 
{
}
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}
@end

推荐答案

这是不可能的,当你的应用没有运行通知时,它不会直接对通知做出反应.

It's not possible, when your app is not running notification it will not react on notification directly.

didFinishLaunchingWithOptions 仅在用户通过此通知打开您的应用时才包含有关通知的信息.如果他取消它并通过仪表板图标打开您的应用程序,您将不会在此方法中看到这一点.

didFinishLaunchingWithOptions will contain information about notification only when user opened your app through this notifcation. If he cancels it and opens your app through dashboard icon you are not gonna see this in this method.

不幸的是,如果您需要对上次用户打开应用时发生的所有通知做出反应,唯一的方法是构建您自己的跟踪逻辑并根据时间获取过去发生的所有事件.此外,甚至无法获得您为应用程序安排的通知列表,因此通常最好构建基于时间的事件逻辑并在其上使用通知,但所有逻辑都在您自己的时间发生基于代码.这样,即使用户禁用通知,您的关键逻辑也会起作用.

Unfortunatelly if you need to react on all notification that happened from last time the user opened the app, only way is to build your own tracking logic and get all events that were in the past based on time. Also there is no way to even get a list of notification you scheduled for your app, so usually it is a good idea to build in time-based event logic and use notification on top of it, but all the logic happens on your own time-based code. This way even if user disable notifications your critical logic will work.

当应用程序在后台运行时,您将通过application:didReceiveRemoteNotification 收到通知.如果您想在用户从后台返回应用后在应用内为它们执行任何特定操作,您可以在应用内收集这些通知,并在 applicationDidBecomeActive 上处理它们.

When the application is running in background you will get notification through application:didReceiveRemoteNotification. You can gather those notifications inside your app, and process them on applicationDidBecomeActive if you want to do anything specific inside your application for them after user comes back to the app from background.

这篇关于当应用程序关闭时如何在ios中接收本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆