1

アプリが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!"); 
      } 
     }]; 

    } 
} 

をトリガーする私の方法です。

ありがとうございます。

+0

は([ここから]のhttpを私の答えを参照してください。 com/questions/37324331/local-notification-ios-using-swift)それはあなたを助けるかもしれません! – Lion

+1

'willPresentNotification'がコンソールログに表示されますか?アプリケーションがフォアグラウンドで_not_でない場合、通知アラートが表示されますか? – matt

+0

はい@matt、私のアプリがforgroundにないときは、通知、つまりnotgroundがnotgroundに表示されています。 – Nik

答えて

10

この機能を実装してください:

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler 
{ 
    completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound); 
} 
+0

彼はこれが既に実装されているサンプルでは理解できませんか? – loki

0

SCREEN

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler 
{ 
    completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound); 

} 
+0

- (ボイド)userNotificationCenter:(UNUserNotificationCenter *)中心willPresentNotification:(UNNotification *)通知withCompletionHandler:(ボイド(^)(UNNotificationPresentationOptions))completionHandler {completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound)。 } –

0

OF SHOWのALERTのTOP最前面にローカル通知を処理するために、このコードを参照してください。 /*****************************/

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 

      UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound; 

      [center requestAuthorizationWithOptions:options 
            completionHandler:^(BOOL granted, NSError * _Nullable error) { 
             if (!granted) { 
              //NSLog(@"Something went wrong"); 
             } 
            }]; 

      int dayCounter =5; 

      int minute = 48; 


      { 
       NSDateComponents *components = [[NSDateComponents alloc] init]; 
       components.weekday = dayCounter; 


       dayCounter++; 

       components.hour = 12; 
       components.minute = minute; 

       UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES]; 

       UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init]; 
       objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil]; 

       objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"We made a surprise Edit for You" arguments:nil]; 

       objNotificationContent.sound = [UNNotificationSound defaultSound]; 

       objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1); 


       UNNotificationAttachment *attachment = nil; 

       NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
       NSError *attachmentError = nil; 

       attachment = [UNNotificationAttachment attachmentWithIdentifier:@"image" 
                      URL: outputURL 
                     options:nil 
                      error:&attachmentError]; 
       if (attachmentError) { 

        return; 
       } 

       [email protected][attachment]; 

       NSString *identifier = @"UYLLocalNotification"; 
       UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier 
                         content:objNotificationContent trigger: trigger]; 

       [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
        if (error != nil) { 
         NSLog(@"Something went wrong: %@",error); 
        } 
        else 
        { 
        } 
       }]; 

/********** ******************/

//ハンドルに - (無効)アプリケーション:(のUIApplication *)アプリケーションdidReceiveLocalNotification:(UILocalNotification *)通知{

UIApplicationState state = [application applicationState]; 
    if (state == UIApplicationStateActive) { 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Reminder" message:notification.alertBody delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; 
      alert.tag = 330; 
      [alert show]; 
     }); 
    } 
else{ 
// handle for background 
} 
1

アプリがフォアグラウンドにあるときに通知を受けるには、iOS10のUNUserNotificationCenterDelegateの次の代理メソッドを実装する必要があります。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

    completionHandler([.alert,.badge]) 
} 

詳細については、appleのドキュメントを参照してください。アプリがフォアグラウンドにあるときにバナーメッセージを表示するための

https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate?language=objc

0

、次のメソッドを使用します。ここにリンクがあります。

AppDelegateであなたのデリゲートメソッドにそれを追加し、このプロトコルに準拠 - UNUserNotificationCenterDelegate

iOSの10、スウィフト3:// stackoverflowの:

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    completionHandler(UNNotificationPresentationOptions.alert) 
} 
関連する問題