ブログ

2022-10-17

RichFlyerと他のプッシュ通知システムの共存について

SDK

はじめに

こんにちは。
RichFlyer開発チームです。

「すでにテキストのプッシュ通知を運用しているが、リッチプッシュの機能だけRichFlyerを使って配信することは可能か」といったお問い合わせを多くいただきます。
答えは「可能」です。実現方法も簡単です。

本稿では、実現方法をご紹介いたします。

iOS

RichFlyer SDK対応バージョン

 Swift 

 ・3.4.5以上

 Objective-C 

 ・1.3.51以上

手順

  1. RichFlyer SDKの組み込み
    RichFlyer SDKご利用マニュアルに従ってSDKを導入します。

  2. Notification Service Extensionで以下のように実装を行います。
// Swift Sample
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 {
            // RichFlyerから配信された通知を判別
            if (RFApp.isRichFlyerNotification(userInfo: bestAttemptContent.userInfo)) {
                 // RichFlyerから配信されたプッシュ通知を処理
                RFNotificationService.configureRFNotification(content: bestAttemptContent,
                                                            appGroupId: "group.net.richflyer.app",
                                                            displayNavigate: true,
                                                            completeHandler: { (content) in
                    contentHandler(content)
                })

            } else {
                // RichFlyer以外でで配信したプッシュ通知を処理
                contentHandler(bestAttemptContent)
            }
        }
    }
}

// Objective-C Sample
@implementation NotificationService

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

    // RichFlyerから配信された通知を判別
    if ([RFApp isRichFlyerNotification:self.bestAttemptContent.userInfo]) {
        // RichFlyerから配信されたプッシュ通知を処理
        [RFNotificationService configureRFNotification:self.bestAttemptContent
                                appGroupId:@"group.net.richflyer.app"
                                displayNavigate:YES
                                completeHandler:^(UNMutableNotificationContent *content) {

        self.bestAttemptContent = content;
        self.contentHandler(self.bestAttemptContent);      
        }];
    } else {
        // RichFlyer以外でで配信したプッシュ通知を処理
        self.contentHandler(self.bestAttemptContent);
    }    
}

@end

Android

RichFlyer SDK 対応バージョン

 ・1.4.1以上

手順

  1. RichFlyer SDKの組み込み
    RichFlyer SDKご利用マニュアルに従ってSDKを導入します。
  2. FirebaseMessagingServiceクラスを継承したクラスで以下の実装をします。
public class FirebaseMessagingServiceBase extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
       super.onMessageReceived(remoteMessage);

        RFSendPushInformation spi = new RFSendPushInformation(this, R.mipmap.ic_notice);
        Map<String, String> data = remoteMessage.getData();

        // RichFlyerから配信された通知を判別
        if (spi.isRichFlyerNotification(data)) {
            // RichFlyerから配信されたプッシュ通知を処理
            spi.setPushData(data);
        } else {
            // RichFlyer以外でで配信したプッシュ通知を処理
        }
   }
}

おわりに

いかがでしたでしょうか?受信したデータをメソッドに渡すだけですので比較的簡単に判別ができると思います。
他にも「こういう使い方をしたいが、RichFlyerで実現できるか」など、疑問点がございましたらお気軽にお問合せください。