ios - App Delegate 的应用程序 :didReceiveLocalNotification: not called
<p><p>在我的应用委托(delegate)中,方法 <code>- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification</code> 永远不会被调用。</p>
<p>这就是我创建通知的方式:</p>
<pre><code>UILocalNotification *notification = [ init];
// set UUID, which we will store in userDefaults for later
NSMutableDictionary *myUserInfo = [ init];
NSString *uuid = [ globallyUniqueString];
;
;
;
NSInteger row = ;
switch (row) {
case 0:
;
break;
case 1:
;
break;
case 2:
;
break;
default:
;
break;
}
notification.userInfo = myUserInfo;
// calculate date for next notification, depends on the user's selection
NSDate *today = ;
NSCalendar *calendar = ;
NSDateComponents *myComps = [ init];
;
notification.fireDate = ;
notification.timeZone = ;
notification.alertBody = @"My alertBody";
[ scheduleLocalNotification:notification];
</code></pre>
<p>这是在我的应用委托(delegate)中,但从未被调用:</p>
<pre><code>- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
BOOL repeat = [ boolValue];
if (repeat)
{
NSInteger frequency = (NSInteger);
NSString *unit = (NSString *);
NSString *uuid = (NSString *);
// calculate date for next notification
NSDate *today = ;
NSCalendar *calendar = ;
NSDateComponents *myComps = [ init];
if () {
//;
;
} else {
}
// create new notification
UILocalNotification *newNotification = [ init];
newNotification.fireDate = ;
newNotification.timeZone = ;
newNotification.alertAction = notification.alertAction;
newNotification.alertBody = notification.alertBody;
newNotification.userInfo = notification.userInfo;
// schedule it
[ scheduleLocalNotification:newNotification];
}
}
</code></pre>
<p>在 iOS 8 上测试,不确定 iOS 7...</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果在通知触发时应用程序未处于事件状态,您将在 <code>didFinishLaunchingWithOptions</code> 中进行处理,如 <a href="https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html" rel="noreferrer noopener nofollow">Local and Push Notifications Programming Guide</a> 的 <em>处理本地和远程通知</em> 部分中的示例所示:</p>
<pre><code>- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif =
;
if (localNotif) {
NSString *itemName = ;
;// custom method
app.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
;
;
return YES;
}
</code></pre>
<p>如果在通知触发时应用程序处于事件状态,则调用 <code>didReceiveLocalNotification</code>。</p></p>
<p style="font-size: 20px;">关于ios - App Delegate 的应用程序 :didReceiveLocalNotification: not called,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28073152/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28073152/
</a>
</p>
页:
[1]