通知バッジの設定¶
RichFlyerではプッシュ通知を受信直後は、通知センターに残っている未読の件数がバッチに表示されます。
アプリを起動後は、以下の方法で端末のホーム画面のアプリアイコンに表示するバッジの数字を設定できます。
バッジの表示¶
// Class RFApp
static func setBadgeNumber(application: UIApplication, badge: Int)
// Class RFApp
+ setBadgeNumber:badge:
パラメータ | 内容 |
---|---|
application | UIApplicationオブジェクト |
badge | バッジに表示する数値 |
バッジの非表示¶
// Class RFApp
static func resetBadgeNumber(application: UIApplication)
// Class RFApp
+ resetBadgeNumber:
パラメータ | 内容 |
---|---|
application | UIApplicationオブジェクト |
実装例¶
Swift
func applicationDidBecomeActive(_ application: UIApplication) {
// バッジの数字を非表示にしています
RFApp.resetBadgeNumber(application: application)
// バッジの数字に3を表示します
// RFApp.setBadgeNumber(application: application, badge: 3)
}
Objective-C
- (void)applicationDidBecomeActive:(UIApplication *)application {
// バッジの数字を非表示にしています
[RFApp resetBadgeNumber:application];
// バッジの数字に3を表示します
// [RFApp setBadgeNumber:application badge:3];
}
プッシュ通知受信直後のバッジの値を変更したい場合¶
プッシュ通知受信直後に、通知センターの未読件数ではなく、任意の値を表示したい場合はNotification Service Extensionに以下のように実装してください。
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
// バッジの値を1にする
content.badge = NSNumber(value: 1)
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;
// バッジの値を1にする
self.bestAttemptContent.badge = [NSNumber numberWithInt:1];
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