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.

Among two notifications android only display one single notification

See original GitHub issue

Question

My scenario is on my app, once I do the successful payment I received two FCM notifications same time from my back end. But my problem is my device only show the last FCM message which I got,

Once I debug my application it shows two messages have arrived as Foreground but only shows one popup.

My codebase

fcmService.registerAppWithFCM();
    fcmService.register(onRegister, onNotification, onOpenNotificaion);
    localNotificationService.configure(
      onRegister,
      onNotification,
      onOpenNotificaion,
      '327977904469',
    );

    if (Platform.OS == 'android') {
      localNotificationService.createChannelAndroid('wapp');
    }

    function onRegister(token) {
      console.log(
        '🚀 ~ file: Router.js ~ line 302 ~ onRegister ~ token',
        token,
      );
      saveFCMToken(token);
    }

    function onNotification(notify) {
      console.log(
        '🚀 ~ file: Router.js ~ line 309 ~ onNotification ~ notify',
        notify,
      );

      const options = {
        soundName: 'default',
        playSound: true,
      };
      localNotificationService.showNotification(
        0,
        notify.title,
        notify.body,
        notify,
        options,
        'wapp',
      );
    }

Helper File

import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import {Platform} from 'react-native';

class NotificationHelper {
  configure = (onRegister, onNotification, onOpenNotification, senderID) => {
    PushNotification.configure({
      onRegister: function (token) {
        onRegister(token.token);
        console.log('[NotificationManager] onRegister token:', token.token);
      },

      onNotification: function (notification) {
        console.log('[NotificationManager] onNotification:', notification);

        if (Platform.OS === 'ios') {
          if (notification.data.openedInForeground) {
            notification.userInteraction = true;
          }
        }

        if (notification.userInteraction) {
          onOpenNotification(notification);
        } else {
          onNotification(notification);
        }

        if (Platform.OS === 'android') {
          notification.userInteraction = true;
        }

        // Only call callback if not from foreground
        if (Platform.OS === 'ios') {
          if (!notification.data.openedInForeground) {
            notification.finish('backgroundFetchResultNoData');
          }
        } else {
          notification.finish('backgroundFetchResultNoData');
        }
      },

      // ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
      senderID: senderID,
    });
  };

  unregister = () => {
    PushNotification.unregister();
  };

  createChannelAndroid = (channel) => {
    PushNotification.createChannel(
      {
        channelId: channel, // (required)
        channelName: 'My channel', // (required)
        channelDescription: 'A channel to categorise your notifications', // (optional) default: undefined.
        playSound: false, // (optional) default: true
        soundName: 'default', // (optional) See `soundName` parameter of `localNotification` function
        importance: 4, // (optional) default: 4. Int value of the Android notification importance
        vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
      },
      (created) => console.log(`createChannel returned '${created}'`), // (optional) callback returns whether the channel was created, false means it already existed.
    );
  };

  showNotification = (id, title, message, data = {}, options = {}, channel) => {
    console.log('SHOWWWWWW');
    PushNotification.localNotification({
      /* Android Only Properties */
      ...this.buildAndroidNotification(
        id,
        title,
        message,
        data,
        options,
        channel,
      ),
      /* iOS and Android properties */
      ...this.buildIOSNotification(id, title, message, data, options),
      /* iOS and Android properties */
      title: title || '',
      message: message || '',
      playSound: options.playSound || false,
      soundName: options.soundName || 'default',
      userInteraction: false, // BOOLEAN: If the notification was opened by the user from the notification area or not
    });
  };

  buildAndroidNotification = (
    id,
    title,
    message,
    data = {},
    options = {},
    channel,
  ) => {
    return {
      channelId: channel,
      id: id,
      autoCancel: true,
      largeIcon: options.largeIcon || 'ic_launcher',
      smallIcon: options.smallIcon || 'ic_launcher',
      bigText: message || '',
      subText: title || '',
      vibrate: options.vibrate || true,
      vibration: options.vibration || 300,
      priority: options.priority || 'high',
      importance: options.importance || 'high', // (optional) set notification importance, default: high,
      data: data,
    };
  };

  buildIOSNotification = (id, title, message, data = {}, options = {}) => {
    return {
      alertAction: options.alertAction || 'view',
      category: options.category || '',
      userInfo: {
        id: id,
        item: data,
      },
    };
  };

  cancelAllLocalNotifications = () => {
    if (Platform.OS === 'ios') {
      PushNotificationIOS.removeAllDeliveredNotifications();
    } else {
      PushNotification.cancelAllLocalNotifications();
    }
  };

  removeDeliveredNotificationByID = (notificationId) => {
    console.log(
      '[LocalNotificationService] removeDeliveredNotificationByID: ',
      notificationId,
    );
    PushNotification.cancelLocalNotifications({id: `${notificationId}`});
  };
}

export const localNotificationService = new NotificationHelper();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
Dallas62commented, Mar 16, 2021

Hi @akiladevinda I suggest you to change the ID of the notification. You use the same notification id, which can lead to notification replacement. Regards

1reaction
akiladevindacommented, Mar 29, 2021

@JoelOnGithub

 localNotificationService.showNotification(
        0,  // You need to replace this with random generate number - not hardcode
        notify.title,
        notify.body,
        notify,
        options,
        'wapp',
      );
Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple notifications and only show the first one in Android
Hhere only shows the id of the notification, but it's always 1. Any idea about what happend? I'm testing it on a 2.3...
Read more >
Create a Group of Notifications - Android Developers
Starting in Android 7.0 (API level 24), you can choose to display related notifications in a group (previously called "bundled" ...
Read more >
Duplicated Notifications - OneSignal Documentation
Our Android SDKs prevent duplicate notifications by checking the notification payload for OneSignal specific data. Android devices with 2 copies of the app ......
Read more >
Control notifications on Android - Google Support
Option 2: On a notification. To find your notifications, from the top of your phone screen, swipe down. Touch and hold the notification,...
Read more >
Use Notification Center on Mac - Apple Support
Expand or collapse a stack of notifications: If an app's notifications are grouped, multiple notifications are stacked. To expand the stack and show...
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