繁体   English   中英

应用程序在后台时未收到通知(iOS react-native)

[英]Not receiving notification when app in background (iOS react-native)

我正在为我的一个项目实施react-native-firebase,并尝试在其中添加通知功能。 但是我在获取通知时遇到问题。 当我使用firebase api“ https://fcm.googleapis.com/fcm/send ”发送通知时,当应用程序处于前台时,我会收到通知。 但是,当应用程序在后台发送通知时,我什么也没收到。 但是,一旦我打开应用程序,我就会收到通知。

我已经复制了googleservice-info.plist文件,并在Firebase中上传了auth密钥。

我的主要js代码:


        messagaing.hasPermission()
            .then((enabled) => {
                if (enabled) {
                    messagaing.getToken()
                        .then(async token => {
                            const deviceId = await AsyncStorage.getItem('deviceId');
                            if (!deviceId) {
                                this.registerTokenOnServer(token)
                            } else {
                                this.setState({
                                    deviceId
                                })
                            }
                            console.log('fcm token', token)
                        })
                        .catch(error => { console.log('fcm error', error) })
                } else {
                    messagaing.requestPermission()
                        .then(() => { console.log('fcm token') })
                        .catch(() => { console.log('fcm error') })
                }
            })
            .catch(error => {
                console.log('fcm error', error)
            })

        messagaing.onMessage((message) => {
            console.log('fcm noti on message data', message)

            let notification_to_be_displayed = new firebase.notifications.Notification({
                sound: 'default',
                show_in_foreground: true,
                title: message._data.title,
                body: message._data.message,
            });
            firebase.notifications().displayNotification(notification_to_be_displayed);

            firebase.notifications().onNotificationOpened((notificationOpen) => {
                const notification = notificationOpen.notification;
                console.log('fcm open ', notification)
                if (notification.data.obj_type == '1') {
                    this.props.navigation.navigate('ProductDetailPage', { prodId: notification.data.obj_id, })
                } else {
                    this.props.navigation.navigate('SubCategoryPage', { catId: notification.data.obj_id, catName: 'Categories' })
                }
            })
        });

我的AppDelegate.h:

#import <UserNotifications/UserNotifications.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

我的AppDelegate.m:

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#if __has_include(<React/RNSentry.h>)
#import <React/RNSentry.h> // This is used for versions of react >= 0.40
#else
#import "RNSentry.h" // This is used for versions of react < 0.40
#endif
#import "RNGoogleSignin.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <Firebase.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"

@implementation AppDelegate

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

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"reactenduser"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];

  [RNSentry installWithRootView:rootView];

  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  // define UNUserNotificationCenter
  [FIRApp configure];
  [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
  [RNFirebaseNotifications configure];

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

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

  return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

  return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                        openURL:url
                                              sourceApplication:sourceApplication
                                                     annotation:annotation
          ]
  || [RNGoogleSignin application:application
                         openURL:url
               sourceApplication:sourceApplication
                      annotation:annotation
      ];
}

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

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

- (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

当我的应用程序进入后台时,发送通知时没有收到任何数据或错误。 只有当它进入前台时,我才能看到日志。

任何帮助,将不胜感激。 谢谢!

您需要打开远程消息的后台功能。

检查图像 在此处输入图片说明

请注意,在我的项目中,我有后台获取功能,我想您可以跳过它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM