question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Receiving multiple identical nofitications at once.

See original GitHub issue

Hi @evollu ,

I have a project that I’m using react-native-fcm to receive messages from Firebase Cloud Functions, currently I have the heads up notifications working in foreground background and when the device is locked. Still working on when the app is killed, but that’s another issue.

My current problem is that I seem to be receiving duplicate notifications. I have noticed that sometimes if I switch between foreground and background, back to foreground it adds another duplicate when I receive another notification, I’ve seen it increase up to 10 at once. If I kill the app it resets the notifications back to a single notification.

Although I can’t always reproduce it in this fashion. For instance it was just sending 8 at once, then I killed the app on both the device and emulator and now it is only sending 1 notification and is not increasing in number if I switch between foreground and background. It must be something to do with the way I’ve implemented the PushController component, I’m fairly new to react native and perhaps I’m just doing something wrong.

RN - 0.44 react-native-fcm - ^6.2.3 Emulator - Nexus_5X_API_23 - Android 6.0 Device - Nexus 5 - Android 6.0.1

Here is my usage of the PushController Component inside my Screen that renders a user list:

	renderItem= ({item}) => {
		//const { navigate } = this.props.navigation
		return <TouchableOpacity onPress={() => this.props.navigation.navigate('Chat', {friend: item}) }
			title={item.name}>
			<View style={styles.profileRowsContainer}>
				<View style={styles.imageAndTextContainer}>
					<Image source={{ uri: 'https://www.gravatar.com/avatar/'}} style={styles.profileImage}/>
					<View style={styles.textColumnContainer}>
						<Text style={styles.profileName}>{item.name}</Text>
						<Text style={styles.lastMessage}>{item.lastMessage}</Text>
					</View>
				</View>
				<Text style={styles.timeStamp}>{item.timeStamp}</Text>
			</View>
		</TouchableOpacity>
	}

	render() {
		return (
			<View style={styles.container}>
				<PushController/>
					<FlatList
						data={this.state.data}
						renderItem={this.renderItem}
						extraData={this.state}
						keyExtractor={item => item.uid}
					/>
				<Spinner visible={this.state.loading} />
			</View>
		);
	}

And the PushController.js

import React, { Component } from "react";

import { Platform } from 'react-native';

import FCM, {
            FCMEvent,
            RemoteNotificationResult,
            WillPresentNotificationResult,
            NotificationType
            }
            from "react-native-fcm";

//import firebaseClient from  "./FirebaseClient";
const firebase = require("firebase");

export default class PushController extends Component {
  constructor(props) {
    super(props);

    this.friendsRef = this.getRef().child('friends');
  }

  getRef() {
    return firebase.database().ref();
  }

  componentDidMount() {
    FCM.requestPermissions();
    var user = firebase.auth().currentUser;

    FCM.getFCMToken().then(token => {
      console.log("TOKEN (getFCMToken)", token);
      this.friendsRef.child(user.uid).update({
        token: token
      })
      this.props.onChangeToken(token);
    });

    FCM.getInitialNotification().then(notif => {
      console.log("INITIAL NOTIFICATION", notif)
    });

    this.notificationListner = FCM.on(FCMEvent.Notification, notif => {
      console.log("Notification", notif);
      if(notif.local_notification){
        return;
      }
      if(notif.opened_from_tray){
        return;
      }

      if(Platform.OS ==='ios'){
              //optional
              //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
              //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
              //notif._notificationType is available for iOS platfrom
              switch(notif._notificationType){
                case NotificationType.Remote:
                  notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
                  break;
                case NotificationType.NotificationResponse:
                  notif.finish();
                  break;
                case NotificationType.WillPresent:
                  notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
                  break;
              }
            }
      this.showLocalNotification(notif);
    });

    this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, token => {
      console.log("TOKEN (refreshUnsubscribe)", token);
      this.props.onChangeToken(token);
    });
  }

  showLocalNotification(notif) {
    FCM.presentLocalNotification({
      title: notif.title,
      body: notif.body,
      priority: "high",
      click_action: notif.click_action,
      show_in_foreground: true,
      local: true
    });
  }

  componentWillUnmount() {
    this.notificationListner.remove();
    this.refreshTokenListener.remove();
  }


  render() {
    return null;
  }
}

Any help is appreciated! Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
MichalRemiscommented, Nov 8, 2017

I’ve had similar problem, but it was my mistake. When I put FCM.on(FCMEvent.Notification,... outside of component and componentDidMount, multiple firings weren’t occuring. Finally found the reason - make sure your have correct setup of AndroidManifext.xml etc… (my problem was missing android:launchMode="singleTop") https://github.com/evollu/react-native-fcm#config-for-notification-and-click_action-in-android

0reactions
CristinaNicolaecommented, Aug 21, 2017

@nkov How did you figured you have registered multiple listeners? I think I have the same problem.

I have also the same implementation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicated Notifications - OneSignal Documentation
Usually duplicate notifications occur due to sending the same push data multiple times. If you see multiple notifications sent in your Delivery Tab...
Read more >
Duplicate Notifications - Apple Community
Restart your iPhone. Re-enable notifications for the apps you selected. Test to see if duplicate notifications are received.
Read more >
Fix: Duplicate Message Alert Notifications on iPhone - Techbout
Go to Settings > Notifications > Messages > scroll down to the bottom of the screen and tap on Repeat Alerts option located...
Read more >
Stop duplicate chat notifications - Android - Google Chat Help
On your mobile device, open Settings. · Tap Apps & notifications and then Chat and then App notifications. · Make sure app notifications...
Read more >
Solved: Multiple notifications - Canvas Community
Solved: One of my faculty members is getting all of the app notifications twice and wondered ... I'm having the same issue and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found