当app在前台时,横幅iO推送通知

时间:2017-03-24 12:30:09

标签: objective-c react-native push-notification

当应用程序在前台运行时,是否可以触发横幅推送通知的显示,就好像它在后台一样?

我正在使用React-Native版本41.2和react-native-push-notification 2.2.1

我从搜索中注意到,通过在AppDelegate中添加以下内容,iOs 10应该可以实现:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
  completionHandler(UNNotificationPresentationOptionAlert);
}

然而,这似乎没有受到影响,而是正在点击下面的didReceiveLocalNotification方法:

// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RCTPushNotificationManager didReceiveLocalNotification:notification];
}

React-Native是否可以访问userNotificationCenter?或者是否可以使用其他方法获得类似的结果?

谢谢。

2 个答案:

答案 0 :(得分:7)

以下是我设法使其发挥作用的方式。

AppDelegate.h文件中,您需要导入UserNotifications并设置UNUserNotificationCenterDelegate,如下所示:

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

@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>

然后在AppDelegate.m文件中,您需要在[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];方法中添加didFinishLaunchingWithOptions

然后,您必须删除didReceiveLocalNotificationdidReceiveRemoteNotification,因为它们似乎优先于userNotificationCenter

然后添加userNotificationCenter处理程序,如下所示。

- (void)userNotificationCenter:(UNUserNotificationCenter* )center willPresentNotification:(UNNotification* )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    completionHandler(UNNotificationPresentationOptionAlert);

  }

希望这可以帮助其他一些反应原生的开发人员像我一样不熟悉iOs开发。

答案 1 :(得分:0)

Swift 3代码,ios 10及以上

在您的appdelegate中

import UserNotifications并设置UNUserNotificationCenterDelegate 在您应用的didFinishLaunchingWithOptions功能添加

let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()

然后当app收到前台状态的通知并显示banner时,将调用以下UserNotificationDelegate方法

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