您好,登錄后才能下訂單哦!
React Native的react-native-push-notification
是一個(gè)用于React Native應(yīng)用的推送通知庫(kù)。它允許開(kāi)發(fā)者輕松地在iOS和Android設(shè)備上實(shí)現(xiàn)推送通知功能。以下是關(guān)于如何使用react-native-push-notification
的一些基本步驟:
react-native-push-notification
添加到項(xiàng)目中:npm install --save react-native-push-notification
或
yarn add react-native-push-notification
react-native link react-native-push-notification
ios/YourProjectName/AppDelegate.m
的文件(如果尚不存在),并添加以下內(nèi)容:#import "react-native-push-notification.h"
// ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
[RNPushNotification registerNotification];
// ...
}
在Android上,你需要在android/app/src/main/AndroidManifest.xml
文件中添加以下權(quán)限:
<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() // <-- 添加這一行
);
}
// ...
}
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
庫(kù)發(fā)送推送通知了。例如,你可以在JavaScript代碼中這樣做:import PushNotification from 'react-native-push-notification';
PushNotification.configure({
// ... 你的配置選項(xiàng)
});
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"]',
});
注意:這只是一個(gè)簡(jiǎn)單的示例,react-native-push-notification
庫(kù)提供了許多其他選項(xiàng)和功能,你可以查閱官方文檔以獲取更多信息。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。