ios icon indicating copy to clipboard operation
ios copied to clipboard

Push Notification call back not get when app is in background

Open siddharthpvi opened this issue 3 years ago • 18 comments

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

siddharthpvi avatar Jul 31 '21 07:07 siddharthpvi

@siddharthpvi did you find a solution?

linus-komnick avatar Aug 04 '21 08:08 linus-komnick

+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.

jordan-rp-cb avatar Aug 09 '21 17:08 jordan-rp-cb

Not

siddharthpvi avatar Sep 07 '21 13:09 siddharthpvi

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}}).

MartijnHols avatar Sep 24 '21 17:09 MartijnHols

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

Joshmatjjen avatar Nov 03 '21 17:11 Joshmatjjen

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

wlek777 avatar Nov 17 '21 04:11 wlek777

+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?

wlek777 avatar Nov 17 '21 04:11 wlek777

@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.

Joshmatjjen avatar Nov 17 '21 04:11 Joshmatjjen

@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)

wlek777 avatar Nov 17 '21 04:11 wlek777

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

Joshmatjjen avatar Nov 17 '21 04:11 Joshmatjjen

@Joshmatjjen IOS only

wlek777 avatar Nov 17 '21 05:11 wlek777

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',
  });

Joshmatjjen avatar Nov 17 '21 05:11 Joshmatjjen

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.

Saad-Bashar avatar Nov 22 '21 03:11 Saad-Bashar

Has anyone found a work around?

DarshanDodal avatar May 01 '22 05:05 DarshanDodal

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

ambessh avatar Aug 16 '22 19:08 ambessh

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

Joshmatjjen avatar Aug 28 '22 00:08 Joshmatjjen

@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

Joshmatjjen avatar Aug 28 '22 01:08 Joshmatjjen

@Joshmatjjen Thank you so much!!.

DarshanDodal avatar Aug 28 '22 06:08 DarshanDodal