コンテンツにスキップ

通知受信時の処理

プッシュ通知を受信したときに、通知センターに表示するための処理を実装します。
App ExtensionであるNotification Service Extensionに処理を追加します。

追加するメソッド

RFNotificationService.configureRFNotification(content: UNMutableNotificationContent, appGroupId: String, displayNavigate: Bool, completeHandler: @escaping ((UNMutableNotificationContent) -> Void))
[RFNotificationService configureRFNotification:appGroupId:displayNavigate:completeHandler:]
パラメータ 内容
content UNMutableNotificationContentオブジェクト
appGroupId AppGroupsのGroup Id
displayNavigate 通知画面に「(長押しで詳しく)」の文字列を表示するか。true:表示 false:表示しない
completeHandler 処理完了時に呼び出されるハンドラ

実装例

追加するコードは2箇所です。

Swift

NotificationService.swift

import UserNotifications
// 追加する
import RichFlyer

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            RFNotificationService.configureRFNotification(content: bestAttemptContent,
                                    appGroupId: "group.jp.co.infocity.richflyer",
                                    displayNavigate: true,
                                    completeHandler: { (content) in
                contentHandler(content)
            })
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}
Objective-C

NotificationService.m

#import "NotificationService.h"
// 追加
#import <RichFlyer/RichFlyer.h>

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    // 追加
    [RFNotificationService configureRFNotification:self.bestAttemptContent
                            appGroupId:@"group.jp.co.infocity.richflyer"
                            displayNavigate:YES
                            completeHandler:^(UNMutableNotificationContent *content) {
        self.bestAttemptContent = content;
        self.contentHandler(self.bestAttemptContent);
    }];
}

- (void)serviceExtensionTimeWillExpire {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    self.contentHandler(self.bestAttemptContent);
}

@end