Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Objective-C中运行应用程序时如何获取本地通知_Ios_Objective C_Uilocalnotification - Fatal编程技术网

Ios 在Objective-C中运行应用程序时如何获取本地通知

Ios 在Objective-C中运行应用程序时如何获取本地通知,ios,objective-c,uilocalnotification,Ios,Objective C,Uilocalnotification,我是iOS新手,在应用程序运行时,我面临显示本地通知的问题。 我的代码是这样的 UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds. localNotification.alertBody =

我是iOS新手,在应用程序运行时,我面临显示本地通知的问题。 我的代码是这样的

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.
localNotification.alertBody = @"Message hear";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSTimeInterval interval;
interval = 60* 60* 12;//12 hours from now
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];//Enter the time here in seconds.
localNotification.alertBody= @"This is message Users will see";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval= NSCalendarUnitMinute;//NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
我在单击按钮时编写此代码,但它不会向我显示通知。
提前谢谢

您必须添加此代码

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
在didFinishLaunchingWithOptions中

如果你设置了这个,那么你必须改变这一行

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.

当您点击按钮设置通知但无法显示时,因为您的应用程序处于活动状态,并以0到60的时间间隔替换,然后将应用程序背景置于选中状态,您必须添加此代码

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
在didFinishLaunchingWithOptions中

如果你设置了这个,那么你必须改变这一行

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.

当您点击按钮设置通知但无法显示时,因为您的应用程序处于活动状态,并在0到60的时间间隔内替换,然后将应用程序背景设置为选中“否”。如果您使用上述代码,则不会显示。首先,我想问一些问题

如果使用iOS 10,上述代码是否有效?

在你的类中,你在哪里调用上面的代码行,或者应用哪种方法?

iOS 10有一个名为
UNUserNotification
的新框架。我们必须导入
UNUserNotificationCenterDelegate
,以便在应用程序运行时显示UILocalNotification

协议定义了 响应可操作的通知并接收通知 当你的应用程序在前台时。指定代理对象 指向共享的UNUserNotificationCenter的委托属性 对象用户通知中心对象调用用户的方法 在适当的时间委派人员传递信息

然后,当通知到达时,它会调用

如果通知到达时您的应用程序位于前台,则 通知中心调用下面的方法

上述方法传递通知 直接到你的应用程序。如果实现此方法,则可以 处理通知和更新所需的任何操作 你的应用程序。完成后,执行completionHandler块并 指定希望系统向用户发出警报的方式(如果有)。如果你的 如果代理未实现此方法,系统会在 如果已将UNNotificationPresentationOptionNone选项传递给 completionHandler块。如果您根本不提供委托 对于UNUserNotificationCenter对象,系统使用 通知的原始选项,用于提醒用户

如何显示通知?

为了实现这一目标,我们有3种选择

Sound
Badge
Alert 

以上是一个很好的理解和做

我也喜欢一些答案。是的


不,如果您使用上述代码,则不会显示。首先,我想问一些问题

如果使用iOS 10,上述代码是否有效?

在你的类中,你在哪里调用上面的代码行,或者应用哪种方法?

iOS 10有一个名为
UNUserNotification
的新框架。我们必须导入
UNUserNotificationCenterDelegate
,以便在应用程序运行时显示UILocalNotification

协议定义了 响应可操作的通知并接收通知 当你的应用程序在前台时。指定代理对象 指向共享的UNUserNotificationCenter的委托属性 对象用户通知中心对象调用用户的方法 在适当的时间委派人员传递信息

然后,当通知到达时,它会调用

如果通知到达时您的应用程序位于前台,则 通知中心调用下面的方法

上述方法传递通知 直接到你的应用程序。如果实现此方法,则可以 处理通知和更新所需的任何操作 你的应用程序。完成后,执行completionHandler块并 指定希望系统向用户发出警报的方式(如果有)。如果你的 如果代理未实现此方法,系统会在 如果已将UNNotificationPresentationOptionNone选项传递给 completionHandler块。如果您根本不提供委托 对于UNUserNotificationCenter对象,系统使用 通知的原始选项,用于提醒用户

如何显示通知?

为了实现这一目标,我们有三种选择

Sound
Badge
Alert 

以上是一个很好的理解和做

我也喜欢一些答案。是的

像这样试试

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.
localNotification.alertBody = @"Message hear";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSTimeInterval interval;
interval = 60* 60* 12;//12 hours from now
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];//Enter the time here in seconds.
localNotification.alertBody= @"This is message Users will see";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval= NSCalendarUnitMinute;//NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
像这样试试

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.
localNotification.alertBody = @"Message hear";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSTimeInterval interval;
interval = 60* 60* 12;//12 hours from now
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];//Enter the time here in seconds.
localNotification.alertBody= @"This is message Users will see";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval= NSCalendarUnitMinute;//NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

如果应用正在运行,为什么要显示通知?通知是让用户启动应用程序的一种方式。如果应用程序正在运行,为什么要这样做?如果应用程序正在运行,为什么要显示通知?通知是让用户启动应用程序的一种方式。如果应用程序已经在运行,你为什么要这么做?谢谢你的帖子。您能否给出更多解释以使您的响应更有用。但是如果您不想这样做,则会重复通知。请将该行替换为以下代码localNotification.fireDate=[[NSDate date]addTimeInterval:5];谢谢你的帖子。您能否给出更多解释以使您的响应更有用。但是如果您不想这样做,则会重复通知。请将该行替换为以下代码localNotification.fireDate=[[NSDate date]addTimeInterval:5];