当前位置: 首页 > 知识库问答 >
问题:

应用程序在前台iOS中时获取推送通知

楚嘉胜
2023-03-14
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

我想在通知屏幕上显示通知,无论应用程序是在后台还是前台。我为寻找解决办法而疲惫不堪。非常感谢任何帮助。

共有1个答案

杜阳炎
2023-03-14

如果应用程序在前台时显示横幅消息,请使用以下方法。

iOS 10、Swift 3/4:

// This method will be called when app received push notifications in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler([.alert, .badge, .sound])
}

iOS 10、Swift 2.3:

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
    //Handle the notification
    completionHandler(
       [UNNotificationPresentationOptions.Alert,
        UNNotificationPresentationOptions.Sound,
        UNNotificationPresentationOptions.Badge])
}
import UserNotifications

// snip!

class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate

// snip!

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

      // set the delegate in didFinishLaunchingWithOptions
      UNUserNotificationCenter.current().delegate = self
      ...
   }
 类似资料:
  • 这是苹果公司设计的吗?或者我错过了一些我可以申请的设置?

  • 我已经在我的应用程序(Android和IOS)中实现了Appcelerator钛推送通知,用于接收消息: 当应用程序在后台和前台时,我在android中收到通知。我在android中使用下面的属性来显示应用程序在前台时的通知(CloudPush.showTrayNotificationsWhenFocus=true;) 问题:当应用程序位于前台时,我在IOS(9.1版iPhone 5)中收到通知时

  • 我已经集成了Firebase,用于在flutter中推送通知。如果应用程序在前台,我已经显示了一个通知详细信息的对话框。我收到通知正确的Android。同样在iOS通知在后台和设备锁定时工作正常。我还启用了推送通知和后台抓取。对此有什么解决办法吗?有人面临过这样的问题吗? 我使用这个Firebase插件https://pub.dev/packages/firebase_messaging

  • 我看了很多问题和答案为同一问题。 我现在知道的是 当应用程序是后台应用程序时,不会调用“DidReceiveEmotonification” DidReceiveEmotentification只在应用程序位于前台时调用,或者当用户点击应用程序位于后台的通知时调用 应用程序:didFinishLaunchingWithOptions:在用户点击通知打开应用程序(如果应用程序被终止)时调用 我的情况

  • 我使用firebase在iOS中实现推送通知,目标为C。我有方法,根据其描述,当应用程序处于后台且用户点击通知时,以及当应用程序处于前台时,都应该触发该方法。问题是它只能在后台工作(或者当应用程序没有运行时)。我是不是忘了什么?

  • 当应用程序在后台时,如何更新包含数据负载的Firebase推送通知?有没有办法在通知中指定通知id给Firebase API? 完整的项目在github https://github.com/akshatashan/FireBaseCloudMessagingDemo中