将UILocalNotification发送到主要通知区域而不是警报

时间:2017-01-17 16:35:43

标签: ios objective-c uilocalnotification

我正在开发一个iOS应用程序,它会在应用程序内的屏幕上显示本地通知。我希望它将通知发送到主要通知区域,而不是在屏幕上提醒任何内容。我怎么能这样做?

目前通知区域只是说“没有通知”。

这是我的代码:

的AppDelegate

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
        // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}

的ViewController

self.itemText.text = @"Notification";
NSDate *pickerDate = [[NSDate alloc] initWithTimeIntervalSinceNow:5];
NSLog(@"Picked date is %@", pickerDate);

NSDate *todaysDate;
todaysDate = [NSDate date];
NSLog(@"Todays date is %@", todaysDate);


// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
// TO DO : Assign proper text to self.itemText.text based on real data
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = @"Another";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

1 个答案:

答案 0 :(得分:0)

仅当应用程序处于后台或终止时,UILocalNotification才会添加到主要通知区域。在前台,它将调用您的方法

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

您可以获得所需内容的唯一方法是使用UserNotifications.framework(因为 iOS 10 )。

Introduction to User Notifications Framework in iOS 10只是第一个教程

Here是本地通知的Apple文档

使用它,即使你的应用程序在前台,也不要忘记实现UNUserNotificationCenterDelegate的方法来获取Native通知。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
   willPresentNotification:(UNNotification *)notification 
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;

希望它有所帮助!