iOS 推送通知:如何在应用程序处于后台时检测用户是否点击了通知? [英] iOS push notification: how to detect if the user tapped on notification when the app is in background?

查看:35
本文介绍了iOS 推送通知:如何在应用程序处于后台时检测用户是否点击了通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于这个主题的stackoverflow线程,但我仍然没有找到一个好的解决方案.

如果应用程序不在后台,我可以在 application:didFinishLaunchingWithOptions: 调用中检查 launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] 以查看它是否是从通知中打开的.>

如果应用程序在后台,所有帖子都建议使用application:didReceiveRemoteNotification: 并检查应用程序状态.但正如我所试验的(也正如这个 API 的名称所暗示的那样),这个方法在收到通知时被调用,而不是被点击.

所以问题是,如果应用程序启动然后后台运行,并且您知道从 application:didReceiveNotification 收到通知(application:didFinishLaunchWithOptions: 不会此时触发),你怎么知道用户是通过点击通知还是点击应用程序图标来恢复应用程序?因为如果用户点击通知,则期望打开该通知中提到的页面.否则不应该.

我可以将 handleActionWithIdentifier 用于自定义操作通知,但这只会在点击自定义操作按钮时触发,而不是在用户点击通知主体时触发.

谢谢.

阅读下面的一个答案后,我想这样我就可以澄清我的问题了:

我们如何区分这两种情况:

(A) 1.app进入后台;2.收到通知;3. 用户点击通知;4. 应用进入前台

(B) 1.app进入后台;2.收到通知;3. 用户忽略通知,稍后点击应用图标;4. 应用进入前台

因为 application:didReceiveRemoteNotification: 在这两种情况下都在第 2 步被触发.

或者,是否应该仅在步骤 3 中为 (A) 触发 application:didReceiveRemoteNotification:,但我不知何故配置了我的应用程序错误,所以我在步骤 2 中看到它?

解决方案

好吧,我终于想通了.

在目标设置➝功能选项卡➝后台模式中,如果您选中远程通知",application:didReceiveRemoteNotification: 将在通知到达时立即触发(只要应用程序在背景),在这种情况下,无法判断用户是否会点击通知.

如果您取消选中该框,application:didReceiveRemoteNotification: 只会在您点击通知时触发.

有点奇怪的是,选中此框将更改应用程序委托方法之一的行为方式.如果选中该框会更好,Apple 使用两种不同的委托方法来接收通知和点击通知.我想大多数开发者总是想知道是否点击了通知.

希望这对遇到此问题的其他人有所帮助.Apple 也没有明确记录它

There are a lot of stackoverflow threads regarding this topic, but I still didn't find a good solution.

If the app is not in the background, I can check launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] in application:didFinishLaunchingWithOptions: call to see if it's opened from a notification.

If the app is in the background, all the posts suggest to use application:didReceiveRemoteNotification: and check the application state. But as I experimented (and also as the name of this API suggests), this method gets called when the notification is received, instead of tapped.

So the problem is, if the app is launched and then backgrounded, and you know a notification is received from application:didReceiveNotification (application:didFinishLaunchWithOptions: won't trigger at this point), how do you know if user resumed the app from by tapping the notification or just tapping the app icon? Because if the user tapped the notification, the expectation is to open the page mentioned in that notification. Otherwise it shouldn't.

I could use handleActionWithIdentifier for custom action notifications, but this only gets triggered when a custom action button is tapped, not when the user taps on the main body of the notification.

Thanks.

EDIT:

after reading one answer below, I thought in this way I can clarify my question:

How can we differentiate these 2 scenarios:

(A) 1.app goes to background; 2.notification received; 3. user taps on the notification; 4. app enters foreground

(B) 1.app goes to background; 2.notification received; 3. user ignores the notification and taps on the app icon later; 4. app enters foreground

Since application:didReceiveRemoteNotification: is triggered in both cases at step 2.

Or, should application:didReceiveRemoteNotification: be triggered in step 3 for (A) only, but I somehow configured my app wrong so I'm seeing it at step 2?

解决方案

OK I finally figured out.

In the target settings ➝ Capabilities tab ➝ Background Modes, if you check "Remote Notifications", application:didReceiveRemoteNotification: will get triggered as soon as notification arrives (as long as the app is in the background), and in that case there is no way to tell whether the user will tap on the notification.

If you uncheck that box, application:didReceiveRemoteNotification: will be triggered only when you tap on the notification.

It's a little strange that checking this box will change how one of the app delegate methods behaves. It would be nicer if that box is checked, Apple uses two different delegate methods for notification receive and notification tap. I think most of the developers always want to know if a notification is tapped on or not.

Hopefully this will be helpful for anyone else who run into this issue. Apple also didn't document it clearly here so it took me a while to figure out.

这篇关于iOS 推送通知:如何在应用程序处于后台时检测用户是否点击了通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆