Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29445

Couldn't recieve foreground notification with react native firebase (android)

$
0
0

-I'am unable to get foreground notification on android device.

-it works fine on simulator but not on real device.

-background is working but after opening it i receive an alert with value initial notification, I don't now why?

"react": "16.8.6", "react-native": "^0.58.5", "react-native-firebase": "^5.2.3",

  • my AndroidManifest:
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_notification"/>
    <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="test_app"/>

    <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
    </service>

    <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
      <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
      </intent-filter>
    </service>

    <receiver android:name="io.invertase.firebase.notifications.RNFirebaseNotificationReceiver"/>
    <receiver android:enabled="true" android:exported="true" android:name="io.invertase.firebase.notifications.RNFirebaseNotificationsRebootReceiver">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    </receiver>
  • my compontentDidMount:
firebase
      .messaging()
      .hasPermission()
      .then(enabled => {
        console.log('HAS PERMISS: ', enabled);
        if (enabled) {
          firebase
            .messaging()
            .getToken()
            .then(token => {
              console.log('LOG: ', token);
            })
            .catch(err => console.log(err));
        } else {
          firebase.messaging().requestPermission();
        }
      });

    this.onMessageListener = firebase.messaging().onMessage((message: RemoteMessage) => {
      console.log('mmmm==', message);
      const notification_to_be_displayed = new firebase.notifications.Notification({
        data: message.data,
        sound: 'default',
        show_in_foreground: true,
        title: message.title,
        body: message.body,
      });
      if (Platform.OS === 'android') {
        notification_to_be_displayed.android
          .setPriority(firebase.notifications.Android.Priority.High)
          .setSound('default')
          .android.setChannelId('test-channel')
          .android.setSmallIcon('@drawable/ic_notification')
          .android.setLargeIcon('@mipmap/ic_launcher')
          .android.setVibrate(1000);
      } else if (Platform.OS === 'ios') {
        notification_to_be_displayed.ios.setBadge(2);
      }
      firebase.notifications().displayNotification(notification_to_be_displayed);
    });

    const notificationOpen1 = await firebase.notifications().getInitialNotification();
    if (notificationOpen1) {
      const action = notificationOpen1.action;
      const notification = notificationOpen1.notification;
      console.log('NOTIFICATION IS OPEN');
    }
    // config android
    const channel = new firebase.notifications.Android.Channel(
      'test-channel',
      'Test Channel',
      firebase.notifications.Android.Importance.Max
    ).setDescription('My apps test channel');

    // Create the channel
    firebase.notifications().android.createChannel(channel);
    this.notificationDisplayedListener = firebase
      .notifications()
      .onNotificationDisplayed((notification: Notification) => {
        console.log('CREATED CHANNEL');
        // Process your notification as required
        // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
      });

    this.notificationListener = firebase
      .notifications()
      .onNotification((notification: Notification) => {
        console.log('HAS Notification: ', notification);
        // Process your notification as required
        const notification_to_be_displayed = new firebase.notifications.Notification({
          data: notification.data,
          sound: 'default',
          show_in_foreground: true,
          title: notification.title,
          body: notification.body,
        });
        if (Platform.OS === 'android') {
          notification_to_be_displayed.android
            .setPriority(firebase.notifications.Android.Priority.High)
            .setSound('default')
            .android.setChannelId('test-channel')
            .android.setSmallIcon('@drawable/ic_notification')
            .android.setLargeIcon('@mipmap/ic_launcher')
            .android.setVibrate(1000);
        }
        firebase.notifications().displayNotification(notification_to_be_displayed);
      });

    this.notificationOpenedListener = firebase
      .notifications()
      .onNotificationOpened((notificationOpen: NotificationOpen) => {
        // Get the action triggered by the notification being opened
        const action = notificationOpen.action;
        // Get information about the notification that was opened
        const notification = notificationOpen.notification;
        console.log('notification Day nay', notification);
        firebase.notifications().removeDeliveredNotification(notification.notificationId);
      });

please any help...


Viewing all articles
Browse latest Browse all 29445

Trending Articles