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

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

问题描述

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

当应用挂起时,推送通知导致应用唤醒并执行application:didReceiveRemoteNotification:fetchCompletionHandler,主屏幕出现banner,通知一直留在通知中心,直到用户点击它以启动应用程序.它完全按预期工作.

当应用程序未运行时,只要用户没有强制退出,就会有通知启动应用程序(请参阅 apple 的文档),应用程序执行 application:didFinishLaunchingWithOptionsapplication:didReceiveRemoteNotification:fetchCompletionHandler.横幅出现在主屏幕上,但随后通知消失.它不会留在通知中心.此外,如果设备被锁定,有时通知甚至在发出警报声之前就消失了.

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

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

解决方案

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

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

导致我出现问题的代码是:

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) ->布尔值{//初始化 Kinvey SDK 单例KCSClient.sharedClient().initializeKinveyServiceForAppKey("myappid",withAppSecret: "mysecret",usingOptions: nil)KCSPush.registerForPush()//其余方法...}

我把问题改成:

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) ->布尔值{//初始化 Kinvey SDK 单例KCSClient.sharedClient().initializeKinveyServiceForAppKey("myappid",withAppSecret: "mysecret",usingOptions: nil)如果让 _ =launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] 作为?NSD字典{让 appState: UIApplicationState = UIApplication.sharedApplication().applicationState如果 appState == .Active ||appState == .Inactive {KCSPush.registerForPush()}} 别的 {KCSPush.registerForPush()}//其余方法...}

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

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.

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.

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?

解决方案

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.

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...
}

I solved the problem by changing it to:

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...
}

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天全站免登陆