ios 应用终止时处理推送通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35058870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:22:36  来源:igfitidea点击:

Handling Push Notifications when App is Terminated

iosobjective-ciphonepush-notification

提问by Hasmukh D.

When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn't prompt the user with the Alert-View I set up, asking them whether they want to view the Notification's contents or not. It just launches, and sits there.

当我的应用程序未运行并收到推送通知时,如果我单击该通知,则该应用程序将启动 - 但随后它不会使用我设置的 Alert-View 提示用户,询问他们是否要查看通知的内容与否。它只是启动,然后坐在那里。

The Push Notifications do work perfectly when the App is running - either as the Active app or while in the background - but nothing works correctly when the app is not running.

当应用程序运行时,推送通知可以完美运行 - 无论是作为活动应用程序还是在后台运行 - 但当应用程序未运行时,一切都无法正常工作。

I tried logging-out the launchOptions NSDictionary in application: didFinishLaunchingWithOptions: to see what load its bringing - but it comes up as "(null)". So It basically contains nothing - which doesn't make sense cause shouldn't it contain the Notification's load?

我尝试注销应用程序中的 launchOptions NSDictionary:didFinishLaunchingWithOptions: 以查看其带来的负载 - 但它显示为“(空)”。所以它基本上什么都不包含 - 这没有意义,因为它不应该包含通知的负载吗?

Anybody have any ideas how to make Push Notifications work when they arrive while the App was NOT running?

任何人都知道如何在应用程序未运行时推送通知到达时使其工作?

I mean how to handle the Push notifications when the App is in not running state. What if, if you receive many notifications & you did not open the app, neither did you tap the system's notification panel. How are you preserving those push for a later retrieval.

我的意思是当应用程序处于非运行状态时如何处理推送通知。如果,如果您收到很多通知并且您没有打开应用程序,您也没有点击系统的通知面板,该怎么办?您如何保留这些推送以供以后检索。

采纳答案by Vizllx

As per your question, there is no way to hold all the notification when you open the app, better you call an api to get all notification as per time stamp from your back end/server that's how Facebook does.

根据您的问题,当您打开应用程序时,无法保留所有通知,最好调用 api 以根据来自后端/服务器的时间戳获取所有通知,这就是 Facebook 的做法。

回答by Rahul Patel

1)When application is running in backgroundand When application is running in foregroundapplication:didReceiveRemoteNotification:method will called as below.

1)当应用程序在后台运行时当应用程序在前台运行时,application:didReceiveRemoteNotification:方法将调用如下。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (application.applicationState == UIApplicationStateInactive)
    {
        // opened from a push notification when the app was on background
        NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
    }
    else if(application.applicationState == UIApplicationStateActive)
    {
        // a push notification when the app is running. So that you can display an alert and push in any view
        NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
    }
}

2)When application is not launched (close)then application:didFinishedLaunchingWithOptionsmethod will called.

2)当应用程序未启动(接近)然后application:didFinishedLaunchingWithOptions方法将被调用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions != nil)
    {
        // opened from a push notification when the app is closed
        NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil)
        {
             NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);
        }
    }
    else
    {
        // opened app without a push notification.
    }
}

3)There is no way to remove a specific notification as of. The way to remove all the notifications from your app so they don't show in the Notification Center when the user opens the app from one of them, is to set the app badge to 0.

3)目前无法删除特定通知。从您的应用程序中删除所有通知以便当用户从其中之一打开应用程序时它们不会显示在通知中心中的方法是将应用程序徽章设置为 0。

回答by Javier Calatrava Llavería

The app does not process push notification in the background, what it really does the OS is wake up the app once you press in the notification. You can catch this moment in the following way:

该应用程序不会在后台处理推送通知,操作系统真正做的是在您按下通知后唤醒应用程序。您可以通过以下方式捕捉这一刻:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    { 

        if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
      // Your app has been awoken by a notification...
        }
   }

回答by nyg

You can retrieve notifications delivered to your app by using the getDeliveredNotifications(completionHandler:)method. Note that this only returns notifications currently displayed in the Notification Center and not the ones that have been manually cleared by the user.

您可以使用该getDeliveredNotifications(completionHandler:)方法检索传送到您的应用程序的通知。请注意,这仅返回通知中心当前显示的通知,而不是用户手动清除的通知。

UNUserNotificationCenter.current().getDeliveredNotifications { notifications in

    // notifications: An array of UNNotification objects representing the local
    // and remote notifications of your app that have been delivered and are still
    // visible in Notification Center. If none of your app's notifications are
    // visible in Notification Center, the array is empty.

    // As said in the documentation, this closure may be executed in a background
    // thread, so if you want to update your UI you'll need to do the following:
    DispatchQueue.main.sync { /* or .async {} */ 
        // update UI
    }
}

回答by neo D1

There is no way of handling this on application end. You would need to maintain the unread badge count at the server. When the app is killed the badge value is updated from the server.

在应用程序端无法处理此问题。您需要在服务器上维护未读徽章计数。当应用程序被终止时,徽章值会从服务器更新。

So when you open the application any time , you would need to call a web service to get the required notifications and update the badges of tabbar(if used).

因此,当您随时打开应用程序时,您需要调用 Web 服务来获取所需的通知并更新标签栏的徽章(如果使用)。

回答by Bogdan Razvan

You can display the alert after launching a previously terminated app from a notification like this:

您可以在从这样的通知启动之前终止的应用程序后显示警报:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { // display the alert }

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { // display the alert }