zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.77k stars 2.05k forks source link

Repeating too many times on Android #374

Closed sungwoncho closed 3 years ago

sungwoncho commented 7 years ago

versions

react-native-push-notification: 2.2.1 React Native: 0.41.2 Android: 6

problem

When I specify repeatType in my localNotification setting object, the notification repeats constantly every 1~2 seconds.

Here is how I am using this package. The code below is fired once and once only:

PushNotification.localNotification({
    /* Android Only Properties */
    autoCancel: true,
    largeIcon: 'ic_launcher',
    smallIcon: 'ic_notification',
    bigText: 'foo', // (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

    /* 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: 'New exercise', // (optional, for iOS this is only used in apple watch, the title will be the app name on other iOS devices)
    message: 'foo', // (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)
    // date: new Date(),
    repeatType: 'hour', // (Android only) Repeating interval. Could be one of `week`, `day`, `hour`, `minute, `time`. If specified as time, it should be accompanied by one more parameter 'repeatTime` which should the number of milliseconds between each interval
    // actions: '['Yes', 'No']',  // (Android only) See the doc for notification actions to know more
  });

No matter what the value is for repeatType the notification repeats constantly every 1~2 seconds. I tried to include date but doing so did not fix the problem. If I remove repeatType from the object, the problem goes away and notification shows once.

Here is my logcat:

...
02-23 12:08:08.232 17656-17656/com.flowmadapp D/RNPushNotification: Repeating notification with id -992659542 at time 162000000
02-23 12:08:08.234 17656-17656/com.flowmadapp D/RNPushNotification: Storing push notification with id -992659542
02-23 12:08:08.238 17656-17656/com.flowmadapp D/RNPushNotification: Setting a notification with id -992659542 at time 162000000
02-23 12:08:08.248 17656-17656/com.flowmadapp I/RNPushNotification: NotificationPublisher: Prepare To Publish: 805127505, Now Time: 1487812088248
02-23 12:08:08.267 17656-17656/com.flowmadapp D/RNPushNotification: Repeating notification with id 805127505 at time 216000000
02-23 12:08:08.270 17656-17656/com.flowmadapp D/RNPushNotification: Storing push notification with id 805127505
02-23 12:08:08.273 17656-17656/com.flowmadapp D/RNPushNotification: Setting a notification with id 805127505 at time 216000000
...

Any ideas?

more info

I had to upgrade build.gradle due to the issues related to https://github.com/zo0r/react-native-push-notification/issues/252

android/app/build.gradle

dependencies {
   ...
    compile "com.android.support:appcompat-v7:23.4.0"
    compile ('com.google.android.gms:play-services-gcm:9.0.1') { force = true; }
}
xuho commented 7 years ago

Before setup push notification, using cancelAllLocalNotifications method to remove all notifcation in notification central.

varungupta85 commented 7 years ago

@sungwoncho Is the above code the exact code that you are using to show a local notification? In the above call, you are presenting a local notification right away which is why you are seeing a notification right away. In the logcat, I don't see any log about notification repeating every 1-2 seconds. The log clearly states that a notification with ID -992659542 at time 162000000. Then no logs are seen for the same ID. The next log is seen for a completely different ID which is 15 hours after the previous one.

peterchibunna commented 6 years ago

Remove the repeatType option. I think there's a bug around it.

lukewlms commented 6 years ago

I suspect you had a date set in the past.

In iOS if you set a past date with recurring, it will only show future reminders; however, Android will try to "catch up" and show every "missed" notification (which in our case was thousands).

Make sure to set ONLY future dates when setting up a repeated notification.

faisal3413 commented 4 years ago

The other case is if you set date in future and device gets offline then you will get too many past notifications at once when device gets back ON. To avoid this you can do the following,

goto --> node_modules --> react-native-push-notifications --> android --> src --> main --> java --> com --> dieam --> reactnativepushnotification --> modules --> RNPushNotificationBootEventReceiver

then remove this line

rnPushNotificationHelper.sendToNotificationCentre(notificationAttributes.toBundle());

dsernst commented 4 years ago

Ran into this same issue. with a local scheduled notification recurring daily.

Unclear what was happening until finally discovering it was from setting a date in the past.

Wrote this helper function to ensure notifications were always set in the future:

import dayjs from 'dayjs'

function calcNext(date) {
  // Make sure we are only setting notifications for dates in the future,
  // to avoid Android going crazy showing backlogged notifications.
  // see: https://github.com/zo0r/react-native-push-notification/issues/374#issuecomment-396089990

  const now = dayjs()

  // 1) set nextNotifTime to today's date
  let nextNotificationTime = dayjs(date)
    .year(now.year())
    .month(now.month())
    .date(now.date())

  // 2) IF nextNotifTime date is < right now
  if (nextNotificationTime.isBefore(now)) {
    //  THEN add 1day to NotifTime
    nextNotificationTime = nextNotificationTime.add(1, 'day')
  }

  return nextNotificationTime.toDate()
}
faisal3413 commented 4 years ago

@dsernst user will still get past notifications if device gets OFF and scheduled notifications time passed, then when user turn device ON, they will receive all past notifications. Make no changes, if you want user to see all notifications including past. Otherwise you should do following.

https://github.com/zo0r/react-native-push-notification/issues/374#issuecomment-579610393

EmpireJones commented 4 years ago

It's not really clear in the docs, but I had to set the date property and switch to localNotificationSchedule, otherwise the notification occurs constantly, as described above.

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

parmarkamlesh commented 3 years ago

same issue here. i want to repeat notification every 10 minutes in Android, but notification pop up infinite time, to stop that i have to uninstall app, My code below:

import { Platform } from 'react-native'
import PushNotification, { Importance } from 'react-native-push-notification'

PushNotification.createChannel(
    {
        channelId: "oneoone", 
        channelName: "My channel", 
        channelDescription: "A channel to categorise your notifications", 
        playSound: false, 
        soundName: "default", 
        importance: Importance.HIGH, 
        vibrate: true, 
    },
    (created) => console.log(`createChannel returned '${created}'`) 
);

PushNotification.configure({

    onNotification: function (notification) {
        console.log('LOCAL NOTIFICATION ==>', notification)
    },

    popInitialNotification: true,
    requestPermissions: Platform.OS === 'ios'
})

export const LocalNotification = () => {
    PushNotification.localNotification({
        channelId: 'oneoone',
        autoCancel: true,
        allowWhileIdle:true,
        repeatType:'minute',
        repeatTime:10,
        bigText:
            'Demo bigtext?',

        title: 'demo title',
        message: 'demo message',
        vibrate: true,
        vibration: 300,
        playSound: true,
        soundName: 'default',
        actions: '["Confirm", "No"]'
    })
}