溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

React Native如何集成推送通知

發(fā)布時間:2024-10-01 20:04:43 來源:億速云 閱讀:81 作者:小樊 欄目:web開發(fā)

要在React Native項目中集成推送通知,您可以使用第三方庫react-native-push-notification

  1. 安裝庫:
npm install --save react-native-push-notification
  1. 對于iOS,您需要使用CocoaPods安裝一些依賴項。在ios文件夾中創(chuàng)建一個名為Podfile的文件(如果尚不存在),并添加以下內(nèi)容:
platform :ios, '10.0'
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

target 'YourProjectName' do
  config = use_native_modules!

  use_react_native!(:path => config["reactNativePath"])

  target 'YourProjectNameTests' do
    inherit! :complete
    # Pods for testing
  end

  # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable the next line.
  use_flipper!()

  post_install do |installer|
    react_native_post_install(installer)
  end
end

確保將YourProjectName替換為您的項目名稱。然后,在ios文件夾中運行pod install

  1. 鏈接庫(對于React Native < 0.60,需要手動鏈接):
npx react-native link react-native-push-notification
  1. 配置庫:

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" />

<application>標(biāo)簽內(nèi)添加以下代碼:

<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
</receiver>

<application>標(biāo)簽內(nèi)添加以下代碼以配置通知樣式:

<meta-data
  android:name="com.google.firebase.messaging.default_notification_icon"
  android:resource="@mipmap/ic_launcher" />
<meta-data
  android:name="com.google.firebase.messaging.default_notification_title"
  android:value="@string/app_name" />
<meta-data
  android:name="com.google.firebase.messaging.default_notification_body"
  android:value="@string/app_description" />
  1. 初始化庫:

index.js(或主入口文件)中,導(dǎo)入并初始化庫:

import PushNotification from 'react-native-push-notification';

PushNotification.configure({
  // (optional) Called when Token is generated (iOS and Android)
  onRegister: function (token) {
    console.log('TOKEN:', token);
  },

  // (required) Called when a remote or local notification is opened or received
  onNotification: function (notification) {
    console.log('NOTIFICATION:', notification);

    // process the notification
  },

  popInitialNotification: true,
  requestPermissions: true,
});
  1. 請求權(quán)限:

對于Android,在AndroidManifest.xml中添加以下代碼以請求權(quán)限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

對于iOS,在Info.plist中添加以下代碼以請求權(quán)限:

<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的位置信息來發(fā)送推送通知</string>

現(xiàn)在,您已經(jīng)成功地在React Native項目中集成了推送通知。您可以使用PushNotification.localNotification()PushNotification.remoteNotification()方法發(fā)送本地和遠(yuǎn)程通知。更多關(guān)于react-native-push-notification庫的信息和配置選項,請參閱官方文檔:https://github.com/react-native-push-notification/react-native-push-notification

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI