UILocalNotification 不会在 ios9 设备上触发(横幅未显示)
UILocalNotification doesn't fire on ios9 device (banner not showing)
此通知在模拟器 (9.2) 上完美触发,但不会在 iPhone 6(9.2) 上触发:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = NSLocalizedString(@"You are going to be disconnected. To continue being online, please open the application",nil);
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
我从以下位置调用此代码:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self performSelector:@selector(sendLocalNotification) withObject:nil afterDelay:5];
}
远程通知适用于所有客户
如果您想触发本地通知,例如:5 秒后使用此代码:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UILocalNotification* localNotification = [UILocalNotification new];
localNotification.fireDate = [[NSDate new] dateByAddingTimeInterval:5]; // Notification will be fired after 5 since now.
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
注意:如果您想接收通知,您必须征得用户的许可,例如:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
上面列出的权限代码可以在您的 AppDelegate.m
中调用,例如:在 didFinishLaunchingWithOptions
方法中。你必须知道 performSelector
方法在这种情况下不合适,每个 UILocalNotification
都有 fireDate
属性 应该用于在给定的 NSDate
延迟下触发通知.
更新
今天我遇到了同样的问题,我通过 alertBody
空字符串并且没有出现通知。所以在我看来你的
NSLocalizedString(@"You are going to be disconnected. To continue being online, please open the application",nil);
returns 空字符串或 nil。
请尝试分配给 alertBody
属性 任何其他字符串,例如
localNotification.alertBody = @"test"
它应该有效:)
Apple 文档的一部分:
Declaration SWIFT
var alertBody: String?
Discussion Assign a string
or, preferably, a localized-string key (using NSLocalizedString) as
the value of the message. If the value of this property is non-nil, an
alert is displayed. The default value is nil (no alert). Printf style
escape characters are stripped from the string prior to display; to
include a percent symbol (%) in the message, use two percent symbols
(%%).
从您当前的代码看来,选择器可能不会被调用,除非您的应用程序在后台配置为 运行。 Roher 的回答为您提供了正确的协议,但如果这不起作用,请尝试从您的测试设备中手动删除该应用程序,然后重新安装它。我遇到过一些用户在首次打开应用程序时不小心点击了警报视图中的否,该警报视图要求获得发送通知的权限。您可能在没有注意到的情况下无意中这样做了。删除重新安装会重新发起第一次权限请求。如果您不想删除该应用程序,我想您可以从“设置”中删除。
问题是您在应用进入后台后试图调用某些东西。这是不正确的,不会起作用,iOS.
上有很多关于 运行 后台任务的文章
您可以像这样安排通知:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = NSLocalizedString(@"You are going to be disconnected.", nil);
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
[application scheduleLocalNotification:localNotification];
}
如果用户在这 5 秒间隔到期之前打开您的应用,您可以取消此通知:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[application cancelLocalNotification:application];
// If you don't want to keep reference to the notification
// [application cancelAllLocalNotifications];
}
问题是我用
执行选择器
但应该使用
performSelectorInBackground
如果应用程序在后台运行,performSelector 将不起作用
此通知在模拟器 (9.2) 上完美触发,但不会在 iPhone 6(9.2) 上触发:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = NSLocalizedString(@"You are going to be disconnected. To continue being online, please open the application",nil);
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
我从以下位置调用此代码:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self performSelector:@selector(sendLocalNotification) withObject:nil afterDelay:5];
}
远程通知适用于所有客户
如果您想触发本地通知,例如:5 秒后使用此代码:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UILocalNotification* localNotification = [UILocalNotification new];
localNotification.fireDate = [[NSDate new] dateByAddingTimeInterval:5]; // Notification will be fired after 5 since now.
localNotification.alertBody = @"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
注意:如果您想接收通知,您必须征得用户的许可,例如:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
上面列出的权限代码可以在您的 AppDelegate.m
中调用,例如:在 didFinishLaunchingWithOptions
方法中。你必须知道 performSelector
方法在这种情况下不合适,每个 UILocalNotification
都有 fireDate
属性 应该用于在给定的 NSDate
延迟下触发通知.
更新
今天我遇到了同样的问题,我通过 alertBody
空字符串并且没有出现通知。所以在我看来你的
NSLocalizedString(@"You are going to be disconnected. To continue being online, please open the application",nil);
returns 空字符串或 nil。
请尝试分配给 alertBody
属性 任何其他字符串,例如
localNotification.alertBody = @"test"
它应该有效:)
Apple 文档的一部分:
Declaration SWIFT
var alertBody: String?
Discussion Assign a string or, preferably, a localized-string key (using NSLocalizedString) as the value of the message. If the value of this property is non-nil, an alert is displayed. The default value is nil (no alert). Printf style escape characters are stripped from the string prior to display; to include a percent symbol (%) in the message, use two percent symbols (%%).
从您当前的代码看来,选择器可能不会被调用,除非您的应用程序在后台配置为 运行。 Roher 的回答为您提供了正确的协议,但如果这不起作用,请尝试从您的测试设备中手动删除该应用程序,然后重新安装它。我遇到过一些用户在首次打开应用程序时不小心点击了警报视图中的否,该警报视图要求获得发送通知的权限。您可能在没有注意到的情况下无意中这样做了。删除重新安装会重新发起第一次权限请求。如果您不想删除该应用程序,我想您可以从“设置”中删除。
问题是您在应用进入后台后试图调用某些东西。这是不正确的,不会起作用,iOS.
上有很多关于 运行 后台任务的文章您可以像这样安排通知:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = NSLocalizedString(@"You are going to be disconnected.", nil);
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
[application scheduleLocalNotification:localNotification];
}
如果用户在这 5 秒间隔到期之前打开您的应用,您可以取消此通知:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[application cancelLocalNotification:application];
// If you don't want to keep reference to the notification
// [application cancelAllLocalNotifications];
}
问题是我用 执行选择器 但应该使用 performSelectorInBackground
如果应用程序在后台运行,performSelector 将不起作用