Skip to content

[🐛] iOS - Cannot receive any notifications before the app opens second time #4299

@jagwingchoy

Description

@jagwingchoy

Background

Here I am changing the notification service from appcenter to firebase.
I am building the app to my real device (iPhone XR) using Xcode in order to test notifications.

I have tried to search the issue from this issues board, stack overflow, and google, but still cannot find any related reports.

Project Files

index.js

import messaging from '@react-native-firebase/messaging';
messaging().setBackgroundMessageHandler(async (remoteMessage) => {});

import App from './App';

AppRegistry.registerComponent('sampleApp', () => App)

App.js

import messaging from '@react-native-firebase/messaging';

class App extends Component {
  requestUserPermission = async () => {
    const authStatus = await messaging().requestPermission();
    const enabled =
      authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
      authStatus === messaging.AuthorizationStatus.PROVISIONAL;

    console.log("notification authorized?", enabled);
  };

  async componentDidMount() {
    await this.requestUserPermission();

    /* Get the token and store in database */
    messaging().getToken().then((token) => {
      console.log("fcm token, need to store to backend database", token);
    });

    /* Success */
    const unsubscribe = messaging().onMessage(async remoteMessage => {
      console.log("Notification when app is on foreground", remoteMessage);
    });

    /* Success */
    messaging().onNotificationOpenedApp(remoteMessage => {
      console.log('Notification caused app to open from background state:', remoteMessage);
    });

    /* Success */
    messaging().getInitialNotification().then(remoteMessage => {
      if (remoteMessage) {
        console.log('Notification caused app to open from quit state:', remoteMessage);
      }
    });
  }

  render() {
    return (/* Blah blah blah */);
  }
}

export default App;

I have also built up a firebase-admin nodejs server for pushing notification:

const admin = require('firebase-admin');
const credential = require('./sample-firebase-adminsdk-aaaaaaaaaaa.json');

admin.initializeApp({ credential: admin.credential.cert(credential) });

/*
*/
function sendNotification(targets, title, message, badge = 1, imageUrl = null) {
  if (!Array.isArray(targets) || targets.length == 0) {
    throw new Error("Please provide non-empty targets tokens as Array.");
  }

  if (!title || !message) {
    throw new Error("Please provide title and message");
  }

  let notification = {
    tokens: targets,
    notification: {
      title: title,
      body: message
    },
    data: {},
    apns: {
      payload: {
        aps: {
          'mutable-content': 1,
          badge: badge,
          sound: 'default',
        }
      }
    }
  };
  
  if (imageUrl) {
    notification.apns.fcm_options = { image: imageUrl };
    notification.notification.imageUrl = imageUrl;
    notification.android = { notification: { image: imageUrl } };
  }

  admin.messaging().sendMulticast(notification).then((response) => {
    console.log('Successfully sent message:', response);
  }).catch((error) => {
    console.log('Error sending message:', error);
  });
}

Then I will call the function like below:

sendNotification(['device_firebase_message_token'], 'Hello World', 'If you see me, it is success!', 1, 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');

I surely follow the instructions to install the react-native-firebase and all firebase-cloud-messaging steps in https://rnfirebase.io/. (Also the image notification in iOS)

Issue Encountered

After I have built the app into my real device(iPhone XR) through Xcode,
I will open the debugger to get the fcm token from console.log(). No matter:

  • I push the notification through firebase console or my own built nodejs function
  • The app is foreground, background or quit state

it cannot receive any notifications. The logs in nodejs function show it is success:

Successfully sent message: {
  responses: [
    {
      success: true,
      messageId: 'projects/sample-app/messages/0:xxxxxxxxxxxxxxxx'
    }
  ],
  successCount: 1,
  failureCount: 0
}

But after I completely quit the app from background and restart the app again, it can receive notifications normally. The logs also show it is success:

Successfully sent message: {
  responses: [
    {
      success: true,
      messageId: 'projects/sample-app/messages/xxxxxxxxxxxxxxxx'
    }
  ],
  successCount: 1,
  failureCount: 0
}

One thing I have noticed is: the message ID are always start with 0: when I cannot receive the notification.

Does anyone encountered this problem? And how to solve it?

Environment

Click To Expand

react-native info output:

React Native Environment Info:
    System:
      OS: macOS 10.15.6
      CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
      Memory: 181.42 MB / 16.00 GB
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 13.14.0 - /usr/local/bin/node
      Yarn: 1.19.1 - /usr/local/bin/yarn
      npm: 6.14.4 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 14.0, DriverKit 19.0, macOS 10.15, tvOS 14.0, watchOS 7.0
      Android SDK:
        API Levels: 23, 24, 25, 26, 27, 28, 29
        Build Tools: 26.0.2, 26.0.3, 27.0.3, 28.0.3, 29.0.2, 29.0.3
        System Images: android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom
    IDEs:
      Android Studio: 3.5 AI-191.8026.42.35.5791312
      Xcode: 12.0/12A7209 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.3 => 16.8.3
      react-native: 0.59.10 => 0.59.10
    npmGlobalPackages:
      create-react-native-module: 0.19.0
      react-native-cli: 2.0.1
      react-native-rename: 2.4.1
  • Platform that you're experiencing the issue on:
    • iOS but have not tested behavior on Android
  • Dependencies in package.json
    "@react-native-firebase/app": "8.4.1",
    "@react-native-firebase/messaging": "7.8.4",
    "react": "16.8.3",
    "react-native": "0.59.10",
    "react-redux": "5.0.7",
    "redux": "3.7.2",

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocked: firebase-sdkPending a confirmed fix landing on the official native sdk's (iOS/Android).platform: iosplugin: messagingFCM only - ( messaging() ) - do not use for Notificationstype: bugNew bug report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions