【问题标题】:Notification not triggered when app in foreground - iOS应用程序在前台时未触发通知 - iOS
【发布时间】:2017-12-04 09:16:38
【问题描述】:

我正在使用 firebase 在 iOS 中实现推送通知,目标是 c。 我有方法application:didReceiveRemoteNotification:fetchCompletionHandler,根据其描述,当应用程序处于后台并且用户点击通知以及应用程序处于前台时应该触发该方法。事情是它只在后台工作(或者当应用程序没有运行时)。 我是不是忘记了什么?

感谢您的帮助。

【问题讨论】:

  • 尽管这里的duplicate 问题是一个解决方案stackoverflow.com/a/40756206/3632832
  • 是的,当您在前台时它不会触发,您必须自己处理它,例如在 iOS 小于 10 和大于 10 的警报中显示它,您有一个自定义委托方法来处理它.

标签: ios objective-c firebase push-notification


【解决方案1】:

对于 iOS 10 及更高版本,当应用程序在前台时调用以下方法:

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

所以你需要观察这个方法来处理应用在前台时的通知

【讨论】:

  • 我知道它已经关闭但有什么方法可以获取通知信息吗?我的意思是,在 didReceiveRemoteNotification:fetchCompletionHandler 你有一个带有通知参数的 nsdictionary (userinfo)。
  • @JaimeAlcántaraArnela 是的,您可以通过 userNotificationCenter 中的 notification.userInfo 获得相同的字典: willPresentNotification: withCompletionHandler 方法 如果这是您要查找的内容,请支持我的答案。 :)
  • 谢谢@vivek,这正是我所需要的。
  • 如果我的回答对你有帮助,那么别忘了给我的回答点赞@JaimeAlcántaraArnela
【解决方案2】:

iOS Firebase 通知的标准示例在 AppDelegate 中实现,例如 completionHandler(UIBackgroundFetchResultNewData);

如果你喜欢前台,你必须实现它。

【讨论】:

  • 因为标准设置是通知链接到您的应用程序。
  • 很好的建议,但如何?你能解释一下吗?
【解决方案3】:

您可以使用以下代码:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
        UIApplicationState state = [application applicationState];
        if (state == UIApplicationStateActive)
            {
                //app is in foreground
                //the push is in your control
                UILocalNotification *localNotification = 
               [[UILocalNotification alloc] init];
               localNotification.userInfo = userInfo;
               localNotification.soundName = 
               UILocalNotificationDefaultSoundName;
               localNotification.alertBody = message;
               localNotification.fireDate = [NSDate date];
                [[UIApplication sharedApplication] 
               scheduleLocalNotification:localNotification];
            }
            else
            {
               //app is in background:
               //iOS is responsible for displaying push alerts, banner etc..
            }
        }

【讨论】:

  • 消息未定义。你从哪里得到的?
猜你喜欢
  • 1970-01-01
  • 2018-06-15
  • 2018-06-11
  • 2018-01-07
  • 2019-07-24
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
相关资源
最近更新 更多