【问题标题】:Control Sound and Vibration for Notifications in React-Native在 React-Native 中控制通知的声音和振动
【发布时间】:2018-08-06 13:12:31
【问题描述】:

我想知道在react-native 项目中是否可以控制推送通知的声音振动

实际上我想让用户决定通知是否应该有声音/振动。用户应该能够完全禁用这些功能。

我到处搜索,但我的问题与典型问题有所不同:

  • 我想让用户决定(是否应该禁用声音或振动),所以我想要一些包或具有 Javascript 实现的东西,以便我可以将此功能添加到我的应用程序的设置视图中。
  • 我希望能够在 IOS 和 Android 中执行此操作。(如果您只了解这两种情况,请随时回答)

【问题讨论】:

    标签: react-native react-native-android react-native-ios react-native-push-notification


    【解决方案1】:

    根据文档,首先您需要创建频道

    PushNotification.createChannel(
        {
          channelId: "channel-id", // (required)
          channelName: "My channel", // (required)
          channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
          playSound: false, // (optional) default: true
          soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
          importance: 4, // (optional) default: 4. Int value of the Android notification importance
          vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
        },
        (created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
      );
    

    并在本地通知对象中使用此频道 ID“channel-id”

    PushNotification.localNotification({
                    channelId: "channel_id", 
                    vibrate: true,
                    title: "My Notification Title",
                    message: "My Notification Message",
                    onlyAlertOnce: false,
                    ignoreInForeground: false,
                    priority: "high",
                });
    

    【讨论】:

      【解决方案2】:

      你应该使用react-native-push-notification 包。只需按照文档进行即可。

      安装包并链接到项目后。 设置通知完成(来自文档):

      PushNotification.localNotification({
          /* Android Only Properties */
          id: '0', // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
          ticker: "My Notification Ticker", // (optional)
          autoCancel: true, // (optional) default: true
          largeIcon: "ic_launcher", // (optional) default: "ic_launcher"
          smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
          bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
          subText: "This is a subText", // (optional) default: none
          color: "red", // (optional) default: system default
          vibrate: true, // (optional) default: true
          vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
          tag: 'some_tag', // (optional) add tag to message
          group: "group", // (optional) add group to message
          ongoing: false, // (optional) set whether this is an "ongoing" notification
          priority: "high", // (optional) set notification priority, default: high
          visibility: "private", // (optional) set notification visibility, default: private
          importance: "high", // (optional) set notification importance, default: high
      
          /* iOS only properties */
          alertAction: // (optional) default: view
          category: // (optional) default: null
          userInfo: // (optional) default: null (object containing additional notification data)
      
          /* iOS and Android properties */
          title: "My Notification Title", // (optional)
          message: "My Notification Message", // (required)
          playSound: false, // (optional) default: true
          soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
          number: '10', // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
          repeatType: 'day', // (optional) Repeating interval. Check 'Repeating Notifications' section for more info.
          actions: '["Yes", "No"]',  // (Android only) See the doc for notification actions to know more
      });
      

      该软件包适用于 Android 和 iOS...两个平台的一个代码

      有几点要提:

      1. iOS - 振动和声音相结合,不能禁用声音但可以激活振动
      2. iOS - 要仅启用没有声音的振动,您应该“附加”一个将附加到声音属性的静音文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-17
        相关资源
        最近更新 更多