アプリがforgroundにあるときに通知を表示したい。以下は、新しいusernotificationdelegateメソッドに対して行ったコードです。アプリデリゲートでiOS 10、アプリがforgroundにあるときにローカル通知が表示されますか?
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
//iOS 10 handling
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
} }];
}
}
#pragma mark - User notification delegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(@"willPresentNotification");
NSLog(@"%@", notification.request.content.userInfo);
completionHandler (UNNotificationPresentationOptionAlert);
}
そして、これはまだ私がforgroundに通知が届かないのですこれを実行した後、ローカル通知に今
-(void) fireLocalNotification:(NSString *) message
{
NSLog(@"fire Local Notification");
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
//Notification Content
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.body = [NSString stringWithFormat:@"%@",message];
content.sound = [UNNotificationSound defaultSound];
//Set Badge Number
content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:1.0f repeats:NO];
//Notification Request
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"LocalNotification" content:content trigger:trigger];
//schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add Notification Request succeeded!");
}
}];
}
}
をトリガーする私の方法です。
ありがとうございます。
は([ここから]のhttpを私の答えを参照してください。 com/questions/37324331/local-notification-ios-using-swift)それはあなたを助けるかもしれません! – Lion
'willPresentNotification'がコンソールログに表示されますか?アプリケーションがフォアグラウンドで_not_でない場合、通知アラートが表示されますか? – matt
はい@matt、私のアプリがforgroundにないときは、通知、つまりnotgroundがnotgroundに表示されています。 – Nik