【问题标题】:react native push notification (Android) - notification keeps repeating and wont stop反应本机推送通知(Android) - 通知不断重复并且不会停止
【发布时间】:2020-07-29 00:41:56
【问题描述】:

我正在使用 react-native-push-notification 库。 我可以让通知与振动和声音一起出现.....但是问题是它不断重复并且不会停止。

我连续收到 20 多个相同的通知。有人知道为什么会这样吗?

见下方代码

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

  constructor(props) {
    super(props);

    PushNotification.configure({
      onNotification: function(notification) {
        console.log('NOTIFICATION:', notification);
        notification.finish(PushNotificationIOS.FetchResult.NoData);
      },

      popInitialNotification: true,
      requestPermissions: true,
    });

runPushNotification = () => {
    PushNotification.localNotification({
     title: 'title'
     message: 'message',
     playSound: true,
     soundName: 'sound.mp3',
     autoCancel: true, 
     vibrate: true, 
     vibration: 300, 
     actions: '["Yes", "No"]',
     onlyAlertOnce: true,
    });
  };

【问题讨论】:

  • 您需要先从后端服务检查!
  • 2 件事。首先,您在哪里调用runPushNotification(),其次,您的服务是如何被调用的。如果您收到 20 个“推送”通知,那么就是您的后端服务在执行此操作。如果你收到 20 个“本地”通知——这就是你的 runPushNotification() 函数正在做的事情,那是因为它被调用的频率。
  • 这是 localNotification 所以没有后端。 @DougWatkins 的好建议。我多次调用该函数
  • 因为我的评论是你的答案,所以我创建了它作为你的答案。
  • 当我点击这个帖子以为这是一些配置问题时,我寄予厚望,但结果却是另一个“它正在发生,因为我正在调用它”问题。与此同时,我仍然坚持每隔几秒钟就会弹出一次 Android 通知。

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


【解决方案1】:

对于未来的观众,如果您收到通知,有两种方法可以做到这一点,一种是来自外部服务器的推送通知,另一种是使用您的应用自己的代码创建的本地通知。

如果您收到推送通知,那么您需要检查您的服务器端逻辑,看看您没有触发太多次。

如果这些是本地通知,请在您的代码中搜索您调用通知的任何地方。

同时做这两个应该会导致它被过度调用。

【讨论】:

    【解决方案2】:

    对我来说,删除 repeatType 解决了问题

      //repeatType: 'day', // (optional) Repeating interval. Check 'Repeating 
    
     PushNotification.localNotification({
          /* Android Only Properties */
          channelId: '767888', // (required) channelId, if the channel doesn't exist, notification will not trigger.
          ticker: 'My Notification Ticker', // (optional)
          showWhen: true, // (optional) default: true
          autoCancel: true, // (optional) default: true
          largeIcon: 'ic_launcher', // (optional) default: "ic_launcher". Use "" for no large icon.
          largeIconUrl: 'https://www.example.tld/picture.jpg', // (optional) default: undefined
          smallIcon: 'ic_notification', // (optional) default: "ic_notification" with fallback for "ic_launcher". Use "" for default small icon.
          bigText: 'My big text that will be shown when notification is expanded', // (optional) default: "message" prop
          subText: 'This is a subText', // (optional) default: none
          bigPictureUrl: 'https://www.example.tld/picture.jpg', // (optional) default: undefined
          bigLargeIcon: 'ic_launcher', // (optional) default: undefined
          bigLargeIconUrl: 'https://www.example.tld/bigicon.jpg', // (optional) default: undefined
          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
          groupSummary: false, // (optional) set this notification to be the group summary for a group of notifications, default: false
          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
          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). should be used in combine with `com.dieam.reactnativepushnotification.notification_foreground` setting
          shortcutId: 'shortcut-id', // (optional) If this notification is duplicative of a Launcher shortcut, sets the id of the shortcut, in case the Launcher wants to hide the shortcut, default undefined
          onlyAlertOnce: false, // (optional) alert will open only once with sound and notify, default: false
    
          when: null, // (optional) Add a timestamp (Unix timestamp value in milliseconds) pertaining to the notification (usually the time the event occurred). For apps targeting Build.VERSION_CODES.N and above, this time is not shown anymore by default and must be opted into by using `showWhen`, default: null.
          usesChronometer: false, // (optional) Show the `when` field as a stopwatch. Instead of presenting `when` as a timestamp, the notification will show an automatically updating display of the minutes and seconds since when. Useful when showing an elapsed time (like an ongoing phone call), default: false.
          timeoutAfter: null, // (optional) Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled, default: null
    
          messageId: 'google:message_id', // (optional) added as `message_id` to intent extras so opening push notification can find data stored by @react-native-firebase/messaging module.
    
          actions: ['Yes', 'No'], // (Android only) See the doc for notification actions to know more
          invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true
    
          /* iOS only properties */
          category: '', // (optional) default: empty string
    
          /* iOS and Android properties */
          id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
          title: 'My Notification Title', // (optional)
          message: 'My Notification Message', // (required)
          userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
          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.
        });
    ``
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      相关资源
      最近更新 更多