如果在应用未运行时收到推送通知,则通知会消失 [英] Push notifications disappear if received while app not running

查看:111
本文介绍了如果在应用未运行时收到推送通知,则通知会消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个可以接收推送通知的应用程序,在某些情况下,该通知会触发后台获取操作.因此,我为我的应用启用了remote-notification后台功能.

I'm developing an app that can receive push notifications which, in some circumstances, trigger background fetch operations. Therefore, I've enabled the remote-notification background capability for my app.

当应用程序被挂起时,推送通知导致应用程序唤醒并执行application:didReceiveRemoteNotification:fetchCompletionHandler,横幅显示在主屏幕上,并且该通知将保留在通知中心中,直到用户点击它以启动应用程序为止.它完全可以正常工作.

When the app is suspended, push notifications cause the app to wake up and execute application:didReceiveRemoteNotification:fetchCompletionHandler, the banner appears on the homescreen, and the notification remains in the Notification Center until the user taps it to launch the app. It works exactly as it should.

当应用未运行时,只要没有被用户强制退出,便会启动通知(请参见

When the app is not running, a notification will launch the app as long as it was not force-quit by a user (see apple's documentation), and the app executes application:didFinishLaunchingWithOptions and application:didReceiveRemoteNotification:fetchCompletionHandler. The banner appears over the homescreen, but then the notification disappears. It does not remain in the Notification Center. Furthermore, if the device is locked, sometimes the notification disappears before it even finishes making the alert sound.

有趣的是,如果我禁用了远程通知后台模式,则一切正常.在这种情况下,当推送通知到达时,该应用将无法启动.

Interestingly, if I disable the remote notification background mode, everything works fine. In that situation, the app is not launched when the push notification arrives.

当远程通知后台模式打开且传入通知启动未运行的应用程序时,如何防止通知消失?我是否需要在application:didFinishLaunchingWithOptions中添加一些内容,以使应用程序知道它是在后台启动的,并且该通知不应该被丢弃?

How can I prevent the notifications from disappearing when remote notification background mode is on, and an incoming notification launches the not-running app? Do I need to include something in application:didFinishLaunchingWithOptions that lets the app know it's being launched in the background, and the notification shouldn't be discarded?

推荐答案

推送通知似乎消失了,因为当应用在后台启动并执行application:didFinishLaunchingWithOptions:时,它正在重新注册以获取远程通知.重新注册似乎会丢弃所有排队的消息.

It seems like the push notifications were disappearing because when the app was launched in the background and executed application:didFinishLaunchingWithOptions: it was re-registering for remote notifications. Re-registering seems to discard any queued messages.

我的解决方案是在调用推送注册方法之前检查是否由于推送通知在后台启动了该应用程序.我使用的是Kinvey SDK,因此以下代码使用Kinvey的方法,但我强烈怀疑此解决方案将适用于其他推式注册方法,包括标准的UIApplication.registerForRemoteNotifications.

My solution was to check if the app was being launched in the background due to a push notification before calling the push registration method. I'm using the Kinvey SDK, so the following code uses Kinvey's methods, but I strongly suspect this solution will apply to other push registration methods, including the standard UIApplication.registerForRemoteNotifications.

导致我的问题的代码是:

The code that was causing my problem was:

func application(application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
    // Initialize Kinvey SDK singleton
    KCSClient.sharedClient().initializeKinveyServiceForAppKey("myappid",
        withAppSecret: "mysecret",
        usingOptions: nil)

    KCSPush.registerForPush()
    // rest of method...
}

我将其更改为:

func application(application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
    // Initialize Kinvey SDK singleton
    KCSClient.sharedClient().initializeKinveyServiceForAppKey("myappid",
        withAppSecret: "mysecret",
        usingOptions: nil)

    if let _ = 
        launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {

        let appState: UIApplicationState = UIApplication.sharedApplication().applicationState

        if appState == .Active || appState == .Inactive {
            KCSPush.registerForPush()
        }
    } else {
        KCSPush.registerForPush()
    }
    // rest of method...
}

现在,当应用程序通过传入的推送通知在后台启动时,它不会重新注册推送,并且该通知将保留在iOS通知中心中,直到用户点击它或手动启动该应用程序为止.

Now when an app is launched into the background by an incoming push notification, it doesn't re-register for push, and the notification remains in the iOS Notification Center until the user taps it or launches the app manually.

这篇关于如果在应用未运行时收到推送通知,则通知会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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