0

我正在开发获取本地通知功能的应用程序。当少数条件匹配时,我需要触发本地通知。然后我完成了触发本地通知和操作。但我需要在“通知中心”中显示所有触发的通知,即使应用程序是前台或后台。

我正在使用以下代码:

在 didFinishLaunchingWithOptions 方法中:

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

当几个条件匹配时:

for (int i = 0; i < [arrayNotif count]; i++) {
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    [localNotif setAlertBody:[NSString stringWithFormat:@"%@", [[arrayNotif objectAtIndex:i] valueForKey:@"title"]]];
    [localNotif setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    [localNotif setTimeZone:[NSTimeZone defaultTimeZone]];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[arrayNotif objectAtIndex:i] forKey:[NSString stringWithFormat:@"%@", [[arrayNotif objectAtIndex:i] valueForKey:@"id"]]];
    [localNotif setUserInfo:dictionary];

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    [localNotif setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

    dictionary = nil;
}

我通过didReceiveLocalNotification获得所有通知:只有方法的应用程序在前台。现在,我想在通知中心显示所有通知。

4

0 回答 0