🚧 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:
中,則當點擊前台通知時,也會調用此方法。在這種情況下,您應該只在其中一個回呼中通知 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 | function | 是 | 接收已傳遞通知陣列的函式。 |
已傳遞的通知是一個包含以下內容的物件
identifier
:此通知的識別碼。title
:此通知的標題。body
:此通知的內文。category
:此通知的類別(選填)。userInfo
:包含其他通知資料的物件(選填)。thread-id
:此通知的執行緒識別碼(如果有的話)。
removeDeliveredNotifications()
static removeDeliveredNotifications(identifiers: string[]);
從通知中心移除指定的通知。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
identifiers | array | 是 | 通知識別碼陣列。 |
setApplicationIconBadgeNumber()
static setApplicationIconBadgeNumber(num: number);
設定主畫面應用程式圖示的徽章編號。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
number | number | 是 | 應用程式圖示的徽章編號。 |
getApplicationIconBadgeNumber()
static getApplicationIconBadgeNumber(callback: (num: number) => void);
取得主畫面應用程式圖示的目前徽章編號。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
callback | function | 是 | 處理目前徽章編號的函式。 |
cancelLocalNotifications()
static cancelLocalNotifications(userInfo: Object);
取消任何與提供的 userInfo
中的欄位相符的已排程本地通知。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
userInfo | 物件 | 否 |
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 | 是 | 要監聽的事件類型。請參閱下方。 |
handler | function | 是 | 監聽器。 |
有效的事件類型包括
notification
:在收到遠端通知時觸發。處理常式將使用PushNotificationIOS
的實例調用。這將處理在前台到達或從背景點擊以開啟應用程式的通知。localNotification
:在收到本地通知時觸發。處理常式將使用PushNotificationIOS
的實例調用。這將處理在前台到達或從背景點擊以開啟應用程式的通知。register
:在使用者成功註冊遠端通知時觸發。處理常式將使用代表 deviceToken 的十六進位字串調用。registrationError
:在使用者無法註冊遠端通知時觸發。通常由於 APNS 問題或裝置是模擬器而發生。處理常式將使用{message: string, code: number, details: any}
調用。
removeEventListener()
static removeEventListener(
type: PushNotificationEventName,
);
移除事件監聽器。在 componentWillUnmount
中執行此操作以防止記憶體洩漏。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
type | string | 是 | 事件類型。請參閱 addEventListener() 以取得選項。 |
requestPermissions()
static requestPermissions(permissions?: PushNotificationPermissions[]);
向 iOS 請求通知權限,並使用對話方塊提示使用者。預設情況下,這將請求所有通知權限,但您可以選擇性地指定要請求的權限。支援以下權限
alert
badge
sound
如果為此方法提供對應,則只會請求具有真值的權限。
此方法會傳回一個 Promise,該 Promise 將在使用者接受或拒絕請求時,或在先前已拒絕權限時解析。Promise 會解析為請求完成後權限的狀態。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
permissions | array | 否 | alert、badge 或 sound |
abandonPermissions()
static abandonPermissions();
取消註冊透過 Apple 推播通知服務接收的所有遠端通知。
您應僅在極少數情況下呼叫此方法,例如,當新版本的應用程式移除對所有類型遠端通知的支援時。使用者可以透過「設定」應用程式暫時阻止應用程式接收遠端通知。透過此方法取消註冊的應用程式可以隨時重新註冊。
checkPermissions()
static checkPermissions(
callback: (permissions: PushNotificationPermissions) => void,
);
檢查目前已啟用的推播權限。
參數
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
callback | function | 是 | 請參閱下方。 |
將使用 permissions
物件調用 callback
alert:布林值
badge:布林值
sound:布林值
getInitialNotification()
static getInitialNotification(): Promise<PushNotification | null>;
此方法傳回一個 Promise。如果應用程式是由推播通知啟動的,則此 Promise 會解析為已點擊通知的 PushNotificationIOS
類型的物件。否則,它會解析為 null
。
getAuthorizationStatus()
static getAuthorizationStatus(): Promise<number>;
此方法傳回一個 Promise,該 Promise 會解析為目前的通知授權狀態。請參閱 UNAuthorizationStatus 以取得可能的值。
finish()
finish(result: string);
此方法適用於透過 application:didReceiveRemoteNotification:fetchCompletionHandler:
接收的遠端通知。但是,如果同時實作 application:didReceiveRemoteNotification:fetchCompletionHandler:
和 UNUserNotificationCenterDelegate
中的較新處理常式,則此方法將被 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。