React-native-firebase: ios background notifications

Created on 11 May 2017  Â·  36Comments  Â·  Source: invertase/react-native-firebase

Hi, i'am using RN 0.44 and with android everything work good but with ios i can get notification only if the app is open, when app closed/background nothing happen.

All 36 comments

Hi, what payload are you sending?

On 11 May 2017 07:04, "niros001" notifications@github.com wrote:

Hi, i'am using RN 0.44 and with android everything work good but with ios
i can get notification only if the app is open, when app closed/background
nothing happen.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/invertase/react-native-firebase/issues/109, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAzZXjRlzi-idhGk74x_0ZtDYyKjTqrfks5r4qUDgaJpZM4NXjO0
.

Hi, thanks for replay. i just use the firebase console for test with only message text and send to single device

Another thing, all the notifications that i received in background saved somewhere and when i open the app i got all the notification in a row (can be 100 notification in a row..)

@niros001 For background notifications to work on iOS, I use the following body:

{
    notification: {
        title: 'MyBabyBio',
        body: 'Bob commented on your memory of Josh',
        badge: 1,
        sound: 'default',
        color: '#84B2D9',
        icon: 'ic_notification',
        click_action: 'OPEN_MAIN_ACTIVITY',
    },
    data: {
        entryId: 1234,
        type: 'entry_comment_create',
    },
}

I'm not sure how to reproduce the same structure through the console, but it's important that the notification part is there. I'd suggest including a title as well and see if that helps?

I will test it from my server now.

same sh**t
While app open i see the notification banner, while app in background nothing!

What function you use in AppDelegate.m?

Have you definitely followed all of the instructions here: http://invertase.io/react-native-firebase/#/installation-ios?id=_3-cloud-messaging-optional

Note that you need to enable the background capability for notifications.

Yes i know. and i make it many times but always i got the same bug - background not working

When i use old version of react-native-fcm (2.5.6) it was working but when i upgrade react-native-fcm i got that bug so i removed that bundle and install react-native-firebase but got the same bug

I used that functions:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
  [RNFirebaseMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler
{
  [RNFirebaseMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  [RNFirebaseMessaging didReceiveLocalNotification:notification];
}

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

but only the willPresentNotification called.

What do you mean when you say you see the notification banner when the app is open? Is that something you're doing within the app itself?

I just call the: firebaseApp.messaging().createLocalNotification(message) and add some properties to message. while app open everything ok.

But when the app is closed, the notification tends to be presented by the OS rather than your app?

I know but that not happen... anyway i use ios 10.3

My app only handle response of user that press the notification at background. but the OS not show the banner :(

How does the user press the notification in the background if the OS isn't showing the banner?

He was when i used old version of react-native-fcm. now with react-native-firebase i have that bug.

Can you confirm the entire body of the FCM notification that you're sending to the app?

the old version just use other functions in AppDelegate.m that fire the push notification from background

Object {entryId: "1234", collapse_key: "com.techmine.MobileApp", type: "entry_comment_create", notification: Object, from: "921934163450"…}
collapse_key
:
"com.techmine.MobileApp"
entryId
:
"1234"
finish
:
function finish(data)
arguments
:
null
caller
:
null
length
:
1
name
:
"finish"
prototype
:
Object
__proto__
:
function ()
[[FunctionLocation]]
:
index.js:34
[[Scopes]]
:
Scopes[2]
from
:
"921934163450"
notification
:
Object
badge
:
"1"
body
:
"Bob commented on your memory of Josh"
click_action
:
"OPEN_MAIN_ACTIVITY"
color
:
"#84B2D9"
e
:
"1"
icon
:
"ic_notification"
sound
:
"default"
sound2
:
"default"
title
:
"MyBabyBio"
get badge
:
function ()
set badge
:
function ()
get body
:
function ()
set body
:
function ()
get click_action
:
function ()
set click_action
:
function ()
get color
:
function ()
set color
:
function ()
get e
:
function ()
set e
:
function ()
get icon
:
function ()
set icon
:
function ()
get sound
:
function ()
set sound
:
function ()
get sound2
:
function ()
set sound2
:
function ()
get title
:
function ()
set title
:
function ()
__proto__
:
Object
type
:
"entry_comment_create"
__proto__
:
Object

Sorry, I just want to see the object that is being sent by the server, not what is received on the client

    public function sendMessage($data, $target, $notification) {
        $apiData = array(
            'apiURL' => 'https://fcm.googleapis.com/fcm/send',
            'type' => 'POST',
            'JsonPost' => true,
            'App' => array(
                is_array($target)?"registration_tokens":"to" => $target,
                "notification" => $notification,
                "data" => $data
            ),
            'httpHeaders' => array('Authorization:key='.$this->server_key)
        );

        return $this->appGlobalServices->apiCall($apiData);
    }

the target is my device token, and the data + notification is what you gave me before

Can you send me your AppDelegate.m and AppDelegate.h?

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import <UIKit/UIKit.h>
@import UserNotifications;

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"
#import <CodePush/CodePush.h>
#import <Firebase.h>
#import <RNFirebaseMessaging.h>

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

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;


  #ifdef DEBUG
  jsCodeLocation = [NSURL URLWithString:@"http://10.0.0.8:8081/index.ios.bundle?platform=ios&dev=true"];
  #else
  jsCodeLocation = [CodePush bundleURL];
  #endif

  // React Native //
  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"MobileApp"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  // End of React Native //

  //FireBase
  [FIRApp configure];

  //Notifications registration
  [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];

  //FaceBook
  [[FBSDKApplicationDelegate sharedInstance] application:application
    didFinishLaunchingWithOptions:launchOptions];

  return YES;
}

// Facebook SDK
- (void)applicationDidBecomeActive:(UIApplication *)application {
      [FBSDKAppEvents activateApp];
}

//openURL
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                          sourceApplication:sourceApplication
                                                          annotation:annotation];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
  [RNFirebaseMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler
{
  [RNFirebaseMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  [RNFirebaseMessaging didReceiveLocalNotification:notification];
}

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


@end

Everything looks fine, so as long as you have the capabilities enabled, there shouldn't be a reason why this isn't working.

Have you tried putting break points in the notification methods in Xcode to see whether they're being triggered whilst the app is in the background?

If they're not, then the issue is with the FCM setup itself which lies outside the scope of this project.

Yes the remote notification & notification enabled in capabilities and yes i try to put break points but nothing called when app in background T_T. only the willPresentNotification when i open back the app.
What do you mean FCM setup?

Yeah, so this isn't an issue with RNFirebase.
The issue lies somewhere between Apple, Firebase and your phone. RNFirebase isn't working because the app isn't receiving the notification. https://firebase.google.com/docs/cloud-messaging/ are the Firebase docs.
One other thing you can try is to set the priority of the message to 'high' and see if that makes any difference. See here: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message

I read that many time and also try the high priority but nothing help, tests on 3 devices of ios T_T

Maybe the version of RN can be the reason i don't know...

The notification showing in the background is managed by the OS, so nothing to do with RN or your app. I'd keep playing with the firebase console and see if you can get the notification to show in the background whilst filling out the different fields that are available. Other than that, I don't really think we'll be able to offer any more advice.

I'm going to close this issue as it's not an RNFirebase issue.

Solved by change that key at info.plist <key>FirebaseAppDelegateProxyEnabled</key> to false.

@niros001 can you elaborate on your solution. I am also not able to receive any notification in background. I set the key and Bool value to No

I had to set the FirebaseAppDelegateProxyEnabled as Yes. that was it. it works now

@niros001 For background notifications to work on iOS, I use the following body:

{
    notification: {
        title: 'MyBabyBio',
        body: 'Bob commented on your memory of Josh',
        badge: 1,
        sound: 'default',
        color: '#84B2D9',
        icon: 'ic_notification',
        click_action: 'OPEN_MAIN_ACTIVITY',
    },
    data: {
        entryId: 1234,
        type: 'entry_comment_create',
    },
}

I'm not sure how to reproduce the same structure through the console, but it's important that the notification part is there. I'd suggest including a title as well and see if that helps?

How can you get click_action params on iOS?

Was this page helpful?
0 / 5 - 0 ratings