您好,登錄后才能下訂單哦!
React Native的react-native-push-notification
是一個用于React Native應用的推送通知庫。它允許開發者輕松地在iOS和Android設備上實現推送通知功能。以下是關于如何使用react-native-push-notification
的一些基本步驟:
react-native-push-notification
添加到項目中:npm install --save react-native-push-notification
或
yarn add react-native-push-notification
react-native link react-native-push-notification
ios/YourProjectName/AppDelegate.m
的文件(如果尚不存在),并添加以下內容:#import "react-native-push-notification.h"
// ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
[RNPushNotification registerNotification];
// ...
}
在Android上,你需要在android/app/src/main/AndroidManifest.xml
文件中添加以下權限:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
并在MainApplication.java
中添加以下代碼:
import com.dieam.reactnativepushnotification.modules.RNPushNotificationPackage;
public class MainApplication extends Application implements ReactApplication {
// ...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.asList(
new MainReactPackage(),
new RNPushNotificationPackage() // <-- 添加這一行
);
}
// ...
}
AppDelegate.m
中添加以下代碼:UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithCompletionHandler:^(BOOL granted, NSError * _Nullable error) {
// ...
}];
在Android上,你可以在MainActivity.java
中添加以下代碼:
private void requestPermissionsIfNecessary() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.RECEIVE_BOOT_COMPLETED) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.RECEIVE_BOOT_COMPLETED}, REQUEST_CODE);
}
}
}
react-native-push-notification
庫發送推送通知了。例如,你可以在JavaScript代碼中這樣做:import PushNotification from 'react-native-push-notification';
PushNotification.configure({
// ... 你的配置選項
});
PushNotification.localNotification({
/* Android Only Properties */
channelId: 'your-channel-id', // (required) channelId, if the channel doesn't exist, notification will not trigger.
autoCancel: true,
largeIcon: 'ic_launcher',
smallIcon: 'ic_notification',
bigText: 'My big text that will be shown when notification is expanded',
subText: 'This is a subText',
color: 'blue',
vibrate: true,
vibration: 300,
tag: 'some_tag',
group: 'group',
ongoing: false,
priority: 'high',
visibility: 'private',
importance: 4,
vibratePattern: [0, 1000, 500, 1000],
actions: '["Yes", "No"]',
});
注意:這只是一個簡單的示例,react-native-push-notification
庫提供了許多其他選項和功能,你可以查閱官方文檔以獲取更多信息。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。