🚧 PushNotificationIOS
非推奨。代わりにコミュニティパッケージを使用してください。
スケジューリングや権限など、アプリの通知を処理します。
はじめに
プッシュ通知を有効にするには、Apple で通知を構成し、サーバー側のシステムを構成します。
次に、プロジェクトでリモート通知を有効にします。これにより、必要な設定が自動的に有効になります。
register
イベントのサポートを有効にする
AppDelegate.m
に以下を追加します
#import <React/RCTPushNotificationManager.h>
次に、リモート通知登録イベントを処理するために、以下を実装します
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// This will trigger 'register' events on PushNotificationIOS
[RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
// This will trigger 'registrationError' events on PushNotificationIOS
[RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
}
通知の処理
AppDelegate
に UNUserNotificationCenterDelegate
を実装する必要があります
#import <UserNotifications/UserNotifications.h>
@interface YourAppDelegate () <UNUserNotificationCenterDelegate>
@end
アプリ起動時にデリゲートを設定します
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
return YES;
}
フォアグラウンド通知
アプリがフォアグラウンドにあるときに通知が届いた場合の処理には、userNotificationCenter:willPresentNotification:withCompletionHandler:
を実装します。completionHandler を使用して、通知をユーザーに表示するかどうかを決定し、それに応じて RCTPushNotificationManager
に通知します。
// Called when a notification is delivered to a foreground app.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
// This will trigger 'notification' and 'localNotification' events on PushNotificationIOS
[RCTPushNotificationManager didReceiveNotification:notification];
// Decide if and how the notification will be shown to the user
completionHandler(UNNotificationPresentationOptionNone);
}
バックグラウンド通知
通知がタップされたときに処理するには、userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
を実装します。通常、これはユーザーがタップしてアプリを開くバックグラウンド通知に対して呼び出されます。ただし、userNotificationCenter:willPresentNotification:withCompletionHandler:
でフォアグラウンド通知を表示するように設定した場合、このメソッドは、タップされたときにフォアグラウンド通知でも呼び出されます。この場合、これらのコールバックのいずれか1つでのみ RCTPushNotificationManager
に通知する必要があります。
タップされた通知がアプリの起動につながった場合は、setInitialNotification:
を呼び出します。通知が以前に userNotificationCenter:willPresentNotification:withCompletionHandler:
で処理されなかった場合は、didReceiveNotification:
も呼び出します
- (void) userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
// This condition passes if the notification was tapped to launch the app
if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
// Allow the notification to be retrieved on the JS side using getInitialNotification()
[RCTPushNotificationManager setInitialNotification:response.notification];
}
// This will trigger 'notification' and 'localNotification' events on PushNotificationIOS
[RCTPushNotificationManager didReceiveNotification:response.notification];
completionHandler();
}
リファレンス
メソッド
presentLocalNotification()
static presentLocalNotification(details: PresentLocalNotificationDetails);
ローカル通知を即時表示のためにスケジュールします。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
details | オブジェクト | はい | 下記参照。 |
details
は次のものを含むオブジェクトです
alertTitle
: 通知アラートのタイトルとして表示されるテキスト。alertBody
: 通知アラートに表示されるメッセージ。userInfo
: 追加の通知データを含むオブジェクト (オプション)。category
: この通知のカテゴリ。アクション可能な通知に必要です (オプション)。例: 返信やいいねなどの追加のアクションを持つ通知。applicationIconBadgeNumber
アプリのアイコンバッジとして表示する数値。このプロパティのデフォルト値は0で、これはバッジが表示されないことを意味します (オプション)。isSilent
: true の場合、通知は音なしで表示されます (オプション)。soundName
: 通知がトリガーされたときに再生される音 (オプション)。alertAction
: 非推奨。これは iOS のレガシー UILocalNotification に使用されていました。
scheduleLocalNotification()
static scheduleLocalNotification(details: ScheduleLocalNotificationDetails);
ローカル通知を将来の表示のためにスケジュールします。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
details | オブジェクト | はい | 下記参照。 |
details
は次のものを含むオブジェクトです
alertTitle
: 通知アラートのタイトルとして表示されるテキスト。alertBody
: 通知アラートに表示されるメッセージ。fireDate
: 通知がトリガーされる時間。fireDate
またはfireIntervalSeconds
のいずれかを使用して通知をスケジュールします。fireDate
が優先されます。fireIntervalSeconds
: 通知を表示するまでの現在からの秒数。userInfo
: 追加の通知データを含むオブジェクト (オプション)。category
: この通知のカテゴリ。アクション可能な通知に必要です (オプション)。例: 返信やいいねなどの追加のアクションを持つ通知。applicationIconBadgeNumber
アプリのアイコンバッジとして表示する数値。このプロパティのデフォルト値は0で、これはバッジが表示されないことを意味します (オプション)。isSilent
: true の場合、通知は音なしで表示されます (オプション)。soundName
: 通知がトリガーされたときに再生される音 (オプション)。alertAction
: 非推奨。これは iOS のレガシー UILocalNotification に使用されていました。repeatInterval
: 非推奨。代わりにfireDate
またはfireIntervalSeconds
を使用してください。
cancelAllLocalNotifications()
static cancelAllLocalNotifications();
スケジュールされたすべてのローカル通知をキャンセルします。
removeAllDeliveredNotifications()
static removeAllDeliveredNotifications();
通知センターから配信されたすべての通知を削除します。
getDeliveredNotifications()
static getDeliveredNotifications(callback: (notifications: Object[]) => void);
現在通知センターに表示されているアプリの通知のリストを提供します。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
callback | 関数 | はい | 配信された通知の配列を受信する関数。 |
配信された通知は、次のものを含むオブジェクトです
identifier
: この通知の識別子。title
: この通知のタイトル。body
: この通知の本文。category
: この通知のカテゴリ (オプション)。userInfo
: 追加の通知データを含むオブジェクト (オプション)。thread-id
: この通知のスレッド識別子 (ある場合)。
removeDeliveredNotifications()
static removeDeliveredNotifications(identifiers: string[]);
指定された通知を通知センターから削除します。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
identifiers | 配列 | はい | 通知識別子の配列。 |
setApplicationIconBadgeNumber()
static setApplicationIconBadgeNumber(num: number);
ホーム画面のアプリのアイコンのバッジ数を設定します。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
数値 | 数値 | はい | アプリのアイコンのバッジ数。 |
getApplicationIconBadgeNumber()
static getApplicationIconBadgeNumber(callback: (num: number) => void);
ホーム画面のアプリのアイコンの現在のバッジ数を取得します。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
callback | 関数 | はい | 現在のバッジ数を処理する関数。 |
cancelLocalNotifications()
static cancelLocalNotifications(userInfo: Object);
提供された userInfo
のフィールドに一致するスケジュールされたローカル通知をキャンセルします。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
userInfo | オブジェクト | いいえ |
getScheduledLocalNotifications()
static getScheduledLocalNotifications(
callback: (notifications: ScheduleLocalNotificationDetails[]) => void,
);
現在スケジュールされているローカル通知のリストを取得します。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
callback | 関数 | はい | ローカル通知を記述するオブジェクトの配列を処理する関数。 |
addEventListener()
static addEventListener(
type: PushNotificationEventName,
handler:
| ((notification: PushNotification) => void)
| ((deviceToken: string) => void)
| ((error: {message: string; code: number; details: any}) => void),
);
ローカル通知、リモート通知、通知登録結果など、通知イベントにリスナーをアタッチします。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
type | 文字列 | はい | リッスンするイベントの種類。下記参照。 |
handler | 関数 | はい | リスナー。 |
有効なイベントの種類には以下が含まれます
notification
: リモート通知を受信したときに発生します。ハンドラーはPushNotificationIOS
のインスタンスで呼び出されます。これにより、フォアグラウンドに届いた通知、またはバックグラウンドからアプリを開くためにタップされた通知が処理されます。localNotification
: ローカル通知を受信したときに発生します。ハンドラーはPushNotificationIOS
のインスタンスで呼び出されます。これにより、フォアグラウンドに届いた通知、またはバックグラウンドからアプリを開くためにタップされた通知が処理されます。register
: ユーザーがリモート通知に正常に登録したときに発生します。ハンドラーは、deviceToken を表す16進数の文字列で呼び出されます。registrationError
: ユーザーがリモート通知の登録に失敗したときに発生します。通常、APNS の問題が発生した場合、またはデバイスがシミュレーターの場合に発生します。ハンドラーは{message: string, code: number, details: any}
で呼び出されます。
removeEventListener()
static removeEventListener(
type: PushNotificationEventName,
);
イベントリスナーを削除します。メモリリークを防ぐために、componentWillUnmount
でこれを行います。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
type | 文字列 | はい | イベントの種類。オプションについては addEventListener() を参照してください。 |
requestPermissions()
static requestPermissions(permissions?: PushNotificationPermissions[]);
iOS から通知の権限を要求し、ダイアログボックスでユーザーにプロンプトを表示します。デフォルトでは、これはすべての通知の権限を要求しますが、要求する権限をオプションで指定できます。次の権限がサポートされています
alert
badge
sound
マップがメソッドに提供されている場合、真の値を持つ権限のみが要求されます。
このメソッドは、ユーザーがリクエストを承認または拒否した場合、または権限が以前に拒否された場合に解決されるプロミスを返します。プロミスは、リクエストが完了した後の権限の状態に解決されます。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
permissions | 配列 | いいえ | alert, badge, または sound |
abandonPermissions()
static abandonPermissions();
Apple Push Notification サービス経由で受信したすべてのリモート通知の登録を解除します。
このメソッドは、アプリの新しいバージョンがすべての種類のリモート通知のサポートを削除した場合など、まれな状況でのみ呼び出す必要があります。ユーザーは、設定アプリを通じてアプリがリモート通知を受信するのを一時的に防ぐことができます。このメソッドで登録解除されたアプリは、いつでも再登録できます。
checkPermissions()
static checkPermissions(
callback: (permissions: PushNotificationPermissions) => void,
);
現在有効になっているプッシュ通知の権限を確認します。
パラメータ
名前 | 型 | 必須 | 説明 |
---|---|---|---|
callback | 関数 | はい | 下記参照。 |
callback
は、permissions
オブジェクトとともに呼び出されます。
alert: boolean
badge: boolean
sound: boolean
getInitialNotification()
static getInitialNotification(): Promise<PushNotification | null>;
このメソッドは Promise を返します。アプリがプッシュ通知によって起動された場合、この Promise はタップされた通知の PushNotificationIOS
型のオブジェクトに解決されます。それ以外の場合は、null
に解決されます。
getAuthorizationStatus()
static getAuthorizationStatus(): Promise<number>;
このメソッドは、現在の通知許可ステータスに解決される Promise を返します。可能な値については、UNAuthorizationStatus を参照してください。
finish()
finish(result: string);
このメソッドは、application:didReceiveRemoteNotification:fetchCompletionHandler:
を介して受信されたリモート通知に使用できます。ただし、これは UNUserNotificationCenterDelegate
によって置き換えられ、application:didReceiveRemoteNotification:fetchCompletionHandler:
と UNUserNotificationCenterDelegate
の新しいハンドラーの両方が実装されている場合は呼び出されなくなります。
何らかの理由でまだ application:didReceiveRemoteNotification:fetchCompletionHandler:
に依存している場合は、iOS 側でイベント処理を設定する必要があります。
- (void) application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
[RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:handler];
}
JS側で通知の処理が完了したら、ネイティブの完了ハンドラーを実行するために finish()
を呼び出してください。このブロックを呼び出すときは、操作の結果を最もよく表すフェッチ結果の値を渡してください。可能な値の一覧については、PushNotificationIOS.FetchResult
を参照してください。
application:didReceiveRemoteNotification:fetchCompletionHandler:
を使用している場合は、必ずこのハンドラーを呼び出す必要があり、できるだけ早く呼び出す必要があります。詳細については、公式ドキュメントを参照してください。
getMessage()
getMessage(): string | Object;
通知のメインメッセージ文字列を取得するための getAlert
のエイリアスです。
getSound()
getSound(): string;
aps
オブジェクトからサウンド文字列を取得します。これはローカル通知の場合は null
になります。
getCategory()
getCategory(): string;
aps
オブジェクトからカテゴリ文字列を取得します。
getAlert()
getAlert(): string | Object;
aps
オブジェクトから通知のメインメッセージを取得します。エイリアス getMessage()
も参照してください。
getContentAvailable()
getContentAvailable(): number;
aps
オブジェクトから content-available の数値を取得します。
getBadgeCount()
getBadgeCount(): number;
aps
オブジェクトからバッジカウントの数値を取得します。
getData()
getData(): Object;
通知のデータオブジェクトを取得します。
getThreadID()
getThreadID();
通知のスレッド ID を取得します。