且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS推送通知:当应用程序处于后台时,如何检测用户是否点击了通知?

更新时间:2022-12-27 10:47:21

好的我终于想通了。



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



如果取消选中框,应用程序:didReceiveRemoteNotification:只有在您点击通知时才会触发。



有点奇怪选中此框将改变h其中一个app委托方法的行为。如果选中此框会更好,Apple会使用两种不同的委托方法进行通知接收和通知点击。我认为大多数开发人员总是想知道是否有通知。



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


There are a lot of *** 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.