HMS定位套件-我们可以在react native中发送本地通知吗



我需要从设备(而不是后端(发送通知,有办法吗?

谢谢你的回复我会很感激

正如其他人所提到的,您可以为此使用一些库。其中,我认为"反应本地推送通知"得到了更广泛的认可:https://www.npmjs.com/package/react-native-push-notification

请注意,如果您使用的是RN0.60.+,它将为您自动链接,您不必经历react native link或在MainApplication.java中添加包等步骤。如果不是,repo本身也有详细的安装说明。

此外,这个库的一部分依赖于gms/firebase,因此在HMS上下文中,您不能使用它的一些功能,例如远程通知。如果你的目的只是本地通知,那么你可以去。

还要注意,如果您计划处理本地通知的点击,则需要配置onNotificationton来监听点击。需要注意的一点是,在HMS设备上,requestPermission参数必须设置为false,否则将调用firebase依赖项并导致错误。

示例代码片段:

import PushNotification from 'react-native-push-notification'
export default class App extends Component {
...
  componentDidMount() {
     ...
     PushNotification.configure({
      // (required) Called when a remote is received or opened, or local notification is opened
      onNotification: function (notification) {
        console.log("NOTIFICATION:", notification);
    
        // process the notification
    
        // (required) Called when a remote is received or opened, or local notification is opened
        notification.finish(...);
      },
      // Should the initial notification be popped automatically
      // default: true
      popInitialNotification: true,
    
      /**
       * (optional) default: true
       * - Specified if permissions (ios) and token (android and ios) will requested or not,
       * - if not, you must call PushNotificationsHandler.requestPermissions() later
       * - if you are not using remote notification or do not have Firebase installed, use this:
       *     requestPermissions: Platform.OS === 'ios'
       */
      requestPermissions: false,
    });
    ...
  }
    
    ...
    
  onSendLocalNotification = () => {
    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
      allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
      ignoreInForeground: false, // (optional) if true, the notification will not be visible when the app is in the foreground (useful for parity with how iOS notifications appear)
     
      /* iOS only properties */
      alertAction: "view", // (optional) default: view
      category: "", // (optional) default: empty string
      userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
     
      /* 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
    });
  }
  ...
  
}

我希望你能使用代码模式。

最新更新