react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

Unable to Receive Silent Push Notifications on iOS

Open ghost opened this issue 8 years ago • 32 comments

I am unable to receive Silent Push Notification on iOS though I have followed all the steps provided in documentation from messaging and notifications. Same code is working fine on Android. By silent push notifications, I mean notifications with data payload only so that it can be solely handled by app.

Payload used:

{
	"content_available": true,
	"priority":"high",
	"to" : "<FCM Token>",
	"collapse_key" : "type_a",
	"data" : {
         // ...... my data
	}
}

AppDelegate.m

#import <Firebase.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [FIRApp configure];
  [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
  [RNFirebaseNotifications configure];

 // .... template initialization code
  return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
  [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
  [[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];
}

-(void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
 
  [[RNFirebaseMessaging instance] didReceiveRemoteNotification:response.notification.request.content.userInfo];
  completionHandler();
}

@end

BackgroundMessage.js

import firebase from 'react-native-firebase';

export default async (message) => {
    if (message) {
        const {
            topic, title, body
        } = message.data;
        const notification = new firebase.notifications.Notification()
            .setNotificationId(getRandomNotificationId())
            .setTitle(title)
            .setBody(body)
            .setData(message.data);
        notification
            .android.setChannelId('my.channel')
            .android.setSmallIcon('ic_stat_ic_notification');
        firebase.notifications().displayNotification(notification);
    }
    Promise.resolve();
};

index.js

AppRegistry.registerComponent('myApp', () => App);
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => BackgroundMessage);

App.js (Main Component)

class App extends Component {
    async componentDidMount() {
        notificationService.checkPermission();
        this.attachNotificationListeners();
    }

    componentWillUnmount() {
        this.messageListener();
        this.notificationListener();
        this.notificationOpenedListener();
    }

    async attachNotificationListeners() {
        this.messageListener = firebase.messaging().onMessage((message) => {
            // perform action
            }
        });

        this.notificationListener = firebase.notifications().onNotification((notification) => {
             // perform action
        });

        this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
           // perform action
        });

        const notificationOpen = await firebase.notifications().getInitialNotification();
       // perform action
    }

    render() {
        return (
            <Provider store={store}>
                <MainStackRoute />
            </Provider>
        );
    }

Environment

  1. Application Target Platform: iOS and Android both

  2. Development Operating System: Mac OSX Sierra (10.12.6 )

  3. Build Tools: Xcode, Android Studio, Web Storm

  4. React Native version: 0.55.4

  5. React Native Firebase Version: 4.2.0

  6. Firebase Module: notifications, messaging

  7. Are you using typescript? No

ghost avatar Jul 05 '18 13:07 ghost

This issue has been automatically marked as stale because it has not had recent user activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 02 '18 00:09 stale[bot]

i was seeing this issue in both android and iOS, any solution for this?

tsaicharit avatar Sep 06 '18 08:09 tsaicharit

any solutions?? i am not getting the notifications when app is closed, but it works in samsung device.

shrivastavaanurag avatar Sep 20 '18 10:09 shrivastavaanurag

I ran into this issue too; is there a known work-around?

Kerrick avatar Sep 28 '18 19:09 Kerrick

we faced exact same issue aswell when we tried to upgrade to 4.x. Had to revert back to 3.x to make it work.

addmaths07 avatar Oct 02 '18 06:10 addmaths07

the OnMessage func receives the notification on Android, but on IOS it doesnt work...has any one found a workaround for the IOS ??? I want the App to run a func when it receives a data-message as notification, but cant do as it does not work when the app is in background :(

Samykills avatar Oct 04 '18 15:10 Samykills

Hello 👋, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 04 '18 04:11 stale[bot]

This issue still requires attention. @stale[bot]

Kerrick avatar Nov 04 '18 07:11 Kerrick

Found a workaround for this problem...now using VOIP for silent notifications and using react-native-voip-notifications plugin to read the same, this adds a listener to the notification event emitted by the native code, which even works on the background.

Samykills avatar Nov 04 '18 15:11 Samykills

In my experience iOS does not always process silent notifications immediately. Apple probably does not want apps in the background (or foreground but with screen off) to draw much power. Because in theory its possible to keep an app running by sending it a silent notification every 30 seconds.

So I wonder: did anyone ever see if the silent notification is processed at the moment the app became active again ? Because that is what I see happen in my case.

I currently use OneSignal for sending notifications, but that does not work well. Notifications (silent or normal) often arrive late or not at all. So I am looking into an alternative, like Firebase.

Gys avatar Nov 18 '18 15:11 Gys

Hi, I am able to receive a notification message sent from firebase console that means the library is integrated properly.

The issue is same as described above by author, I am unable to receive data-only messages sent from my node server on iOS while it works on android.

ghost avatar Nov 20 '18 07:11 ghost

Will there be any progress on that or its deep in the backlog now? I mean supporting silent notifications on Ios seems like something that can be useful in quite a few scenarios. Actually has this functionality ever worked on earlier versions ?

zaharidichev avatar Jan 21 '19 15:01 zaharidichev

Hi, any progress with this? thanks

danmdinu avatar Mar 07 '19 22:03 danmdinu

Hi there, any updates on this issue? Thanks

jayzyaj avatar Apr 05 '19 03:04 jayzyaj

@Anum-Amin any update on this issue?

ManigandanRaamanathan avatar Apr 14 '19 12:04 ManigandanRaamanathan

@ManigandanRaamanathan Sorry didn't get a chance to look into it

ghost avatar Apr 15 '19 05:04 ghost

Hello 👋, to help manage issues we automatically close stale issues. This issue has been automatically marked as stale because it has not had activity for quite some time. Has this issue been fixed, or does it still require the community's attention?

This issue will be closed in 15 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 13 '19 05:05 stale[bot]

Hey @chrisbianca / @Salakar any chance this issue will be looked into?

ghost avatar May 13 '19 10:05 ghost

@Salakar @invertase any update on this?

ThomasLarge avatar May 30 '19 11:05 ThomasLarge

I receive silent push notifications on ios 9 through 12 following the instructions, but it's incredibly intermittent. Which is basically what Apple says it will be in their APIs, nothing is guaranteed, seemingly. If you're on a real device, you're using 'content_available' (not underscore) and you've definitely requested permission in the code, you're probably just at the mercy of APNs delivering or not, and/or the phone letting your code run or not. My most successful test was on a device where I had interacted with the app a lot, "educating" the power miser algorithms that I cared and wanted the app to run.

mikehardy avatar May 30 '19 12:05 mikehardy