0

我需要从设备(而不是后端)发送通知有没有办法做到这一点?

感谢您的回复,我将不胜感激

4

1 回答 1

0

正如其他人所提到的,您可以为此目的使用一些库。其中我相信“react-native-push-notification”得到了更广泛的认可:https ://www.npmjs.com/package/react-native-push-notification

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

此外,这个库的一部分依赖于 gms/firebase,所以在 HMS 上下文中你不能使用它的一些特性,例如远程通知。如果您的目的仅仅是本地通知,那么您就可以开始了。

另请注意,如果您计划处理本地通知的点击,则需要配置 onNotificaiton 以收听点击。需要注意的重要一点是,在 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
    });
  }
  ...
  
}

我希望你可以使用代码模式。

于 2020-07-23T21:15:01.947 回答