react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
734 stars 282 forks source link

Push Notification call back not get when app is in background #312

Open siddharthpvi opened 2 years ago

siddharthpvi commented 2 years ago

when app is in the background we get the notification but do not get that callback for any action we can do on that callback

linus-komnick commented 2 years ago

@siddharthpvi did you find a solution?

jordan-rp-cb commented 2 years ago

+1 this is happening to us as well. We are trying to run some code whenever a notification is received while the app is in the background / phone is locked. Have gone through troubleshoot page, but there doesn't seem to be anything around the callback not being fired off.

siddharthpvi commented 2 years ago

Not

MartijnHols commented 2 years ago

In order to wake the app when a notification is received, since iOS 13, you need to add the "content-available":1, value to the notification's aps field. Without it, your app will not receive any of the event handlers when it is in the background and a notification is received.

So far I have only been able to get this to work on a real device using notifications sent through node-apn. .apns files sent to the simulator, even with the content-available field, do not seem to work no matter what listener I implement (e.g. {"Simulator Target Bundle": "my.app.id","aps":{"sound":"default","alert":"Test","badge":1,"content-available":1}}).

Joshmatjjen commented 2 years ago

@MartijnHols ... This was added and working fine... But after upgrading to react native version 0.66 ... It dont trigger notification on background.

wlek777 commented 2 years ago

I have this issue too, can anyone help?? react-native version: 0.63.4

wlek777 commented 2 years ago

+1 this is happening to us as well. We are trying to run some code whenever a notification is received while the app is in the background / phone is locked. Have gone through troubleshoot page, but there doesn't seem to be anything around the callback not being fired off.

Have you managed to solve this?

Joshmatjjen commented 2 years ago

@wlek777 ... i ended up using @notifee/react-native it was a better option.. So, I use it for only Local notifications... pass the Remote notification data to it. It would be very helpful and currently, they made the library free.

wlek777 commented 2 years ago

@Joshmatjjen I cannt even get the remote notification data when killed state, this worked fine when app state in active/background (without swipe off the app)

Joshmatjjen commented 2 years ago

@wlek777 ... Is it IOS or Both Android And IOS?

wlek777 commented 2 years ago

@Joshmatjjen IOS only

Joshmatjjen commented 2 years ago

I used react-native-push-notification & @react-native-community/push-notification-ios

react-native-push-notification , was used to configure the remote notification. like this

// Root component.. ie App.js

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

PushNotification.configure({
    // Called when Token is generated.
    onRegister: function (token) {
      console.log('TOKEN:', token);
     // Register ur remote notificaiton to listen to
    },

    // Called when a remote or local notification is opened or received.
    onNotification: function (notification) {
      console.log('NOTIFICATION:----REMOTE', notification);
      console.log(notification.data.type);
        );
        PushNotification.cancelAllLocalNotifications();
        console.log('NO Push', notification);
      }

      //   .then((url) => console.log('Hello uRl', url))
      //   .catch((err) => console.log(err));
      // Do something with the notification.
      // Required on iOS only (see fetchCompletionHandler docs: https://reactnative.dev/docs/pushnotificationios)
      notification.finish(PushNotificationIOS.FetchResult.NoData);
    },
    // (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
    onAction: function (notification) {
      console.log('ACTION:', notification.action);
      console.log('NOTIFICATIONS:', notification);

      // PushNotification.getChannels(function (channel_ids) {
      //   console.log('Channel ID', channel_ids); // ['channel_id_1']
      // });

      // process the action
    },

    // (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
    onRegistrationError: function (err) {
      console.log('THis is the error registering notification', err);
      console.error(err.message, err);
    },

    // IOS ONLY (optional): default: all - Permissions to register.
    permissions: {
      alert: true,
      badge: true,
      sound: true,
    },

    // Should the initial notification be popped automatically
    // default: true
    popInitialNotification: true,

    /**
     * (optional) default: true
     * - Specified if permissions (ios) and token (android and ios) will requested or not,
     * - if not, you must call PushNotificationsHandler.requestPermissions() later
     * - if you are not using remote notification or do not have Firebase installed, use this:
     *     requestPermissions: Platform.OS === 'ios'
     */
    requestPermissions: true,
    // ANDROID: GCM or FCM Sender ID
    senderID: '112233333333',
  });
Saad-Bashar commented 2 years ago

Same issue, the callback is getting triggered when app is in foreground, but it does not get triggered at all when the app is closed or in the background.

DarshanDodal commented 2 years ago

Has anyone found a work around?

ambessh commented 1 year ago

@DarshanDodal @MartijnHols @wlek777 @Saad-Bashar @Joshmatjjen have you guys found solution for this ?

Joshmatjjen commented 1 year ago

@ambessh .. Not yet bro.... Does any one know of a library that can always listen to function on Background...

Joshmatjjen commented 1 year ago

@ambessh @DarshanDodal @MartijnHols @Saad-Bashar. react-native-push-notification is no longer maintained ....Please Switch to this library.... Thank me later

react-native-notifications by wix

DarshanDodal commented 1 year ago

@Joshmatjjen Thank you so much!!.