iOS本地通知 - 在后台进行回调(iOS Local Notification - Callback when in Background)

上下文:我的iOS应用程序使用本地通知,我安排本地通知,然后在通知启动时通过didRecieveLocalNotifications接收回电话。

当应用程序处于前台时,此功能正常工作,但是当应用程序在后台运行时,我会收到iOS通知但没有回叫

我的问题:当应用程序处于后台时,如何从iOS获得回调。

这是回拨代码 -

- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification { //call your method that you want to do something in your app NSLog (@"Received Notification"); [UIApplication sharedApplication].applicationIconBadgeNumber = 1; [myRootController HandleLocalNotifications:notification]; }

Context: My iOS App uses local notifications I schedule local notifications and then I get a call back via didRecieveLocalNotifications when the notifications fire.

This works fine when the app is in foreground however when the App goes in the background I get the iOS notification but no call back.

My Question: How can I get a callback from iOS when the app is in background.

Here is the call back code -

- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification { //call your method that you want to do something in your app NSLog (@"Received Notification"); [UIApplication sharedApplication].applicationIconBadgeNumber = 1; [myRootController HandleLocalNotifications:notification]; }

最满意答案

您正在描述application:didReceiveLocalNotification: 这在iOS 10中已被弃用。您应该更新到UserNotification框架。

接着。

如果您的应用程序位于最前面 ,则用户不会收到警报。 相反, 得到application:didReceiveLocalNotification: .

如果您的应用不是最前面的 ,用户会收到警报。 因此,您没有获得application:didReceiveLocalNotification:因为通知被触发。 但是,如果用户收到通知通过点击通知提醒召唤您的应用 ,您就会得到它。 如果用户这样做,你会得到application:didReceiveLocalNotification:即使在后台。

但是,如果您的应用在后台终止,您将获得application:didFinishLaunchingWithOptions:您可以查看options以了解由于通知而启动的options )。

如果您的应用程序处于后台或未运行,并且用户点击通知并召唤您的应用程序,您将什么也得不到,并且无法知道该通知已被解雇。 这是因为通知不是为了您的利益,而是向用户发送消息的一种方式。 (听起来,从你的问题来看,好像你可能会误用本地通知作为向发送消息的一种方式,这不是他们的目的。)

You are describing application:didReceiveLocalNotification:. This is deprecated in iOS 10. You should be updating to the UserNotification framework.

Now then.

If you app is frontmost, the user gets no alert. Instead, you get application:didReceiveLocalNotification:.

If your app is not frontmost, the user gets an alert. Therefore, you do not get application:didReceiveLocalNotification: because the notification fired. But you do get it if the user received the notification and summoned your app by tapping the notification alert. If the user does that, you will get application:didReceiveLocalNotification: even in the background.

If your app was terminated in the background, however, you will get application:didFinishLaunchingWithOptions: (and you can check the options to learn that you're being launched because of the notification).

If your app was in the background or not running, and the user does not tap the notification and summon your app, you will get nothing at all and will have no way to know that the notification fired. This is because notifications are not for your benefit but are a way of sending a message to the user. (It sounds, from your question, as if you may be misusing local notifications as a way of sending a message to yourself. That is not what they are for.)

更多推荐