🗑️ 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: を実装します。通知をユーザーに表示するかどうかを判断し、それに応じて RCTPushNotificationManager に通知するために completionHandler を使用します。
// 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: でフォアグラウンド通知を表示するように設定していた場合、このメソッドはフォアグラウンド通知がタップされたときにも呼び出されます。この場合、これらのコールバックのいずれかで 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();
}
リファレンス
Methods
presentLocalNotification()
static presentLocalNotification(details: PresentLocalNotificationDetails);
すぐに表示するためのローカル通知をスケジュールします。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| 詳細 | object | はい | 以下を参照してください。 |
details は以下の要素を含むオブジェクトです。
alertTitle: 通知アラートのタイトルとして表示されるテキスト。alertBody: 通知アラートに表示されるメッセージ。userInfo: 追加の通知データを含むオブジェクト (オプション)。category: この通知のカテゴリ。アクション可能な通知 (オプション) に必要です。例: 返信や「いいね」などの追加アクションを含む通知。applicationIconBadgeNumberアプリのアイコンバッジとして表示する番号。このプロパティのデフォルト値は 0 で、これはバッジが表示されないことを意味します (オプション)。isSilent: true の場合、通知は音なしで表示されます (オプション)。soundName: 通知が発信されたときに再生される音 (オプション)。alertAction: 非推奨。これは iOS のレガシー UILocalNotification で使用されていました。
scheduleLocalNotification()
static scheduleLocalNotification(details: ScheduleLocalNotificationDetails);
将来の表示のためにローカル通知をスケジュールします。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| 詳細 | object | はい | 以下を参照してください。 |
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 | function | はい | 配信された通知の配列を受け取る関数。 |
配信された通知は以下の要素を含むオブジェクトです。
identifier: この通知の識別子。title: この通知のタイトル。body: この通知の本文。category: この通知のカテゴリ (オプション)。userInfo: 追加の通知データを含むオブジェクト (オプション)。thread-id: この通知のスレッド識別子 (存在する場合)。
removeDeliveredNotifications()
static removeDeliveredNotifications(identifiers: string[]);
指定された通知を通知センターから削除します。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| 識別子 | array | はい | 通知識別子の配列。 |
setApplicationIconBadgeNumber()
static setApplicationIconBadgeNumber(num: number);
ホーム画面のアプリアイコンのバッジ番号を設定します。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| number | number | はい | アプリアイコンのバッジ番号。 |
getApplicationIconBadgeNumber()
static getApplicationIconBadgeNumber(callback: (num: number) => void);
ホーム画面のアプリアイコンの現在のバッジ番号を取得します。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| callback | function | はい | 現在のバッジ番号を処理する関数。 |
cancelLocalNotifications()
static cancelLocalNotifications(userInfo: Object);
提供された userInfo のフィールドに一致するスケジュールされたローカル通知をキャンセルします。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| userInfo | object | No |
getScheduledLocalNotifications()
static getScheduledLocalNotifications(
callback: (notifications: ScheduleLocalNotificationDetails[]) => void,
);
現在スケジュールされているローカル通知のリストを取得します。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| callback | function | はい | ローカル通知を記述するオブジェクトの配列を処理する関数。 |
addEventListener()
static addEventListener(
type: PushNotificationEventName,
handler:
| ((notification: PushNotification) => void)
| ((deviceToken: string) => void)
| ((error: {message: string; code: number; details: any}) => void),
);
ローカル通知、リモート通知、および通知登録結果を含む通知イベントにリスナーをアタッチします。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| type | string | はい | リッスンするイベントタイプ。以下を参照してください。 |
| ハンドラ | function | はい | リスナー。 |
有効なイベントタイプは次のとおりです。
notification: リモート通知が受信されたときに発生します。ハンドラはPushNotificationIOSのインスタンスで呼び出されます。これは、フォアグラウンドで届く通知、またはバックグラウンドからアプリを開くためにタップされた通知を処理します。localNotification: ローカル通知が受信されたときに発生します。ハンドラはPushNotificationIOSのインスタンスで呼び出されます。これは、フォアグラウンドで届く通知、またはバックグラウンドからアプリを開くためにタップされた通知を処理します。register: ユーザーがリモート通知の登録に成功したときに発生します。ハンドラは deviceToken を表す16進数文字列で呼び出されます。registrationError: ユーザーがリモート通知の登録に失敗したときに発生します。通常、APNSの問題、またはデバイスがシミュレータである場合に発生します。ハンドラは{message: string, code: number, details: any}で呼び出されます。
removeEventListener()
static removeEventListener(
type: PushNotificationEventName,
);
イベントリスナーを削除します。メモリリークを防ぐために、componentWillUnmount でこれを行ってください。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| type | string | はい | イベントタイプ。オプションについては addEventListener() を参照してください。 |
requestPermissions()
static requestPermissions(permissions?: PushNotificationPermissions[]);
iOS に通知権限を要求し、ダイアログボックスをユーザーに表示します。デフォルトでは、すべての通知権限を要求しますが、オプションで要求する権限を指定できます。次の権限がサポートされています。
alertbadgesound
メソッドにマップが提供された場合、truthy な値を持つ権限のみが要求されます。
このメソッドは、ユーザーがリクエストを承認または拒否したとき、または権限が以前に拒否されたときに解決される Promise を返します。Promise は、リクエスト完了後の権限の状態に解決されます。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| 権限 | array | No | alert、badge、または sound |
abandonPermissions()
static abandonPermissions();
Apple Push Notification サービスを介して受信されるすべてのリモート通知の登録を解除します。
このメソッドは、アプリの新しいバージョンがすべての種類のリモート通知のサポートを削除するなどのまれな状況でのみ呼び出す必要があります。ユーザーは、設定アプリを通じて一時的にアプリがリモート通知を受信するのを防ぐことができます。このメソッドで登録解除されたアプリは、いつでも再登録できます。
checkPermissions()
static checkPermissions(
callback: (permissions: PushNotificationPermissions) => void,
);
現在どのプッシュ権限が有効になっているかを確認します。
パラメータ
| 名前 | 型 | 必須 | 説明 |
|---|---|---|---|
| callback | function | はい | 以下を参照してください。 |
callback は permissions オブジェクトで呼び出されます。
alert: booleanbadge: booleansound: 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を取得します。