invertase / react-native-notifee

Moved to https://github.com/invertase/notifee
https://invertase.io/blog/open-sourcing-notifee
Other
465 stars 31 forks source link

Quit Notification problem on iOS #182

Closed czuiko closed 3 years ago

czuiko commented 4 years ago

Hello,

I have a problem with iOS and notifications when app killed (Quit). I`m usind NodeJS for sending message to Firebase

If I add to my request

notification : {
            title: req.body.title,
            body: req.body.message,
        },

and notification received, but without this data when app killed iOS not delivered the notification to the phone. With foreground and background everything is ok.

How to handle notification on app?

I use: Firebase 6 iOS 14 last version of notifee

my Index.js

messaging().setBackgroundMessageHandler(async (remoteMessage) => {
    try {
      let responseObj = JSON.parse(remoteMessage.data.notifee);
      responseObj = {
        title: Platform.OS === 'ios' ? responseObj.title : `<b>${responseObj.title}</b>`,
        body: responseObj.message,
        data: {chatId: responseObj.chatId},
        android: {
          color: 'green',
          channelId: 'important',
          // smallIcon: 'ic_small_icon',
          style: {
            type: AndroidStyle.BIGPICTURE,
            picture: responseObj.image,
          },
        },

        ios: {
          foregroundPresentationOptions: {
            alert: true,
            badge: true,
            sound: true,
          },
          sound: 'default',
          categoryId: 'important',
          attachments: [
            {
              url: responseObj.image,
              thumbnailTime: 3, // optional
            },
          ],
        },
      };
      await notifee.displayNotification(responseObj);
    } catch (e) {
      //console.log(e);
    }
  });
  notifee.onBackgroundEvent(async ({type, detail}) => {
    console.log(detail)
    const {notification, pressAction, input} = detail;
    if (type === EventType.ACTION_PRESS && pressAction.id === 'reply') {
      // Send message action on send reply
    } else {
      switch (type) {
        case EventType.DISMISSED:
          //action on dismiss notification
          break;
        case EventType.PRESS:
          RootNavigation.navigate('Messenger', {chatId: detail.notification.data.chatId});
          break;
      }
    }
  });

Node JS server code

router.post('/firebase-send', function(req, res) {
    const registrationToken = req.body.token;

    var message = {
        tokens: registrationToken,
        data: {
            chatId: req.body.chatId,
            notifee: JSON.stringify({
                title: req.body.title,
                message: req.body.message,
                chatId: req.body.chatId,
                type: req.body.type,
                rating: req.body.rating,
                image: req.body.image,
                audio: req.body.audio,
                video: req.body.video,
                client: req.body.client,
                android: {
                    channelId:  'default',
                },
                ios: {
                },
            }),
        },
        contentAvailable: true,
        priority: 'high',
        notification : {
            title: req.body.title,
            body: req.body.message,
        },
        apns: {
            payload: {
                aps: {
                    contentAvailable: true
                }
            },
            headers: {
                'pns-push-type': 'background',
                'apns-priority': '5',
                'apns-topic': 'revci.com'
            },
        },
    };

    admin.messaging().sendMulticast(message)
        .then((response) => {
            // Response is a message ID string.
            res.json({
                status: 200,
                message: "Successfully sent message"
            })
mikehardy commented 4 years ago

Interesting, the typical iOS-not-receiving-FCM-when-quit problem is that content-available is not set, but it looks like you are including it

This will be a firebase FCM receipt problem though, not a notifee one - you should check the react-native-firebase issues (there are so many, unfortunately - it's a hard thing to do right) about ios message deliver when quit - it does work unless the user swipes the app away, that's a "force close" and you will never get your code to run again until the user interacts with a visible notification or starts your app