Skip to content

5 プッシュ通知受信時のバナー表示(フォアグラウンド)

iOS9以下では、アプリがフォアグラウンド状態のときは、OS の通知センターが関与しないため、プッシュ通知受信時に通知センターのバナーが表示されることはありませんでした。
iOS10からは、そのバナーを出すことが可能になりました。

5-1.png

5.1 フォアグラウンドでプッシュ通知が届いた時、通知センターのバナーを出したい

AppDelegate.m において、UNUserNotificationCenterDelegateuserNotificationCenter:willPresentNotification:withCompletionHandler: を以下のように実装してください。

バナーをタップした場合

表示されたバナーをユーザーがタップすると、userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: がコールされます。
そして、プッシュ通知の受信処理、お知らせ詳細画面への遷移などに進みます。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification: (UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
    UNNotificationTrigger *trigger = notification.request.trigger;
    if ([trigger isMemberOfClass:[UNPushNotificationTrigger class]]) {
        completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
    }
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> ()) {
    let trigger = notification.request.trigger
    if type(of: trigger) == UNPushNotificationTrigger.self {
        completionHandler([.badge, .alert, .sound])
    }
}