【发布时间】:2016-12-14 08:56:46
【问题描述】:
我在我的应用程序中执行以下步骤
在我的 appdelegate
#import <UserNotifications/UserNotifications.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = (id)self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
return YES;
}
在我的 viewcontroller 中生成本地通知的代码
-(IBAction)btnclicked:(id)sender
{
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = @"gg";
content.subtitle = @"ggg";
content.body = @"hhh";
content.sound = [UNNotificationSound defaultSound];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"2"
content:content trigger:trigger
];
// Schedule the notification.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"hii");
}];
}
现在,当我的应用在 前台 未生成通知时。
我在 ios 9 中使用
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = @"Default";
localNotification.alertBody = @"my notification;)";
localNotification.fireDate = [NSDate date];
localNotification.category = @"Email";
localNotification.repeatInterval = kCFCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
此代码在 ios 9 中有效,但在 ios 10 中 UILocalNotification 已弃用。
那么当您的应用在前台时,如何在 ios 10 中生成本地通知?
【问题讨论】:
-
我可以快速向您发布解决方案,好吗?
-
@Mingebag 没关系。
标签: ios objective-c