打开应用程序时如何使推送通知显示在前台

时间:2020-02-24 10:01:53

标签: ios react-native onesignal

我在我的应用程序中使用了推送通知服务。当应用程序处于后台时,我可以在通知屏幕上(从iOS设备顶部向下滑动时显示的屏幕)或之后关闭我的应用程序时收到通知,但是如果应用程序处于打开状态,我什至无法获取/查看通知。请如果有人知道如何解决此问题,将不胜感激。

这是AppDelegate文件中的外观

#import <Firebase.h>
#import "AppDelegate.h"
#import <GoogleMaps/GoogleMaps.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    [FIRApp configure];
    [GMSServices provideAPIKey:@"AIzcasdsr4ZdsdsA7BIxJxb6g3XE"];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
  [super applicationWillEnterForeground:application];
}


#pragma mark - Handling URLs

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [super application:app openURL:url options:options];
}



#pragma mark - Notifications

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
{
  [super application:application didRegisterForRemoteNotificationsWithDeviceToken:token];
}



@end

1 个答案:

答案 0 :(得分:1)

在AppDelegate中实现以下代码,以便在应用程序位于前景中时接收通知。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .sound])
}

确保AppDelegate符合UNUserNotificationCenterDelegate协议,并在UNUserNotificationCenter.current().delegate = self方法中设置didFinishLaunchingWithOptions

请找到文档here

编辑2:

#import <Firebase.h>
#import "AppDelegate.h"
#import <GoogleMaps/GoogleMaps.h>
#import <UserNotifications/UserNotifications.h>

@interface NFAppDelegate () <UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    [FIRApp configure];
    [GMSServices provideAPIKey:@"AIzcasdsr4ZdsdsA7BIxJxb6g3XE"];
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
  [super applicationWillEnterForeground:application];
}


#pragma mark - Handling URLs

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [super application:app openURL:url options:options];
}



#pragma mark - Notifications

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
{
  [super application:application didRegisterForRemoteNotificationsWithDeviceToken:token];
}

#pragma mark - UNNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
}


@end