简体   繁体   English

在后台重新加载推送通知徽章

[英]Reloading the push notification badge in the background

I am creating a notification service using swift3 in xcode 10. The problem now is that when a push notification comes in the background (even when the app is closed), the badge does not increase at first, but increases by 1 from the second push notification. 我正在xcode 10中使用swift3创建通知服务。现在的问题是,当推送通知进入后台(即使关闭应用程序时),徽章最初不会增加,但是从第二次推送开始便增加1。通知。 Furthermore, when I enter the app and come back in the background, the number of badges will be normal, but the above problem will happen again. 此外,当我进入应用程序并在后台返回时,徽章的数量将是正常的,但是上述问题将再次发生。

I tried to check the problem through delay or local notifications, but I have not been able to figure out what the problem is. 我试图通过延迟或本地通知检查问题,但是我无法弄清楚问题是什么。

Below are the notifications related to the notifications within the AppDelegate. 以下是与AppDelegate中的通知相关的通知。 Push Notification Click event also works normally. 推送通知单击事件也正常运行。

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate, NaverThirdPartyLoginConnectionDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert,], completionHandler: {(granted, error) in
            if (granted)
            {
                application.registerForRemoteNotifications()
            }
            else{
            }
        })
        return true
}

...
... 
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.badge, .alert, .sound])
        UIApplication.shared.applicationIconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1
        }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("userInfo: \(response.notification.request.content.userInfo)")
        var userInfo:[AnyHashable: Any]?
        let pushId:Int32 = userInfo?["uid"] as! Int32
        self.moveView(pushId)// My app load method
    }
}

Running app in the background state is just a brief stop on the way to the app being suspended. 在后台状态下运行应用程序只是暂停应用程序的短暂停留。 While suspended, an app remains in memory but does not execute any code. 挂起时,应用程序仍保留在内存中,但不执行任何代码。 For this reason your code is not executing and thus badge value does not update. 因此,您的代码未执行,因此徽章值不会更新。 See these below link to about application state and background execution. 请参阅下面的链接,了解有关应用程序状态和后台执行的信息。

So better approach to solve this problem is to send send badge value inside of push notification payload. 因此,解决此问题的更好方法是在推送通知有效负载内发送发送徽章值。 eg 例如

{
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
            "action-loc-key" : "PLAY"
        },
        "badge" : 5
    },
    "acme1" : "bar",
    "acme2" : [ "bang",  "whiz" ]
}

See this link to create remote notification payload 查看此链接以创建远程通知有效负载

Don't increase badge value programmatically unless you need to show badge for local notification. 除非您需要为本地通知显示徽章,否则不要以编程方式增加徽章的价值。 If you want to execute code on background while push notification receive, use VoIP push notification which has few restriction eg app must be related VoIP services. 如果要在接收推送通知时在后台执行代码,请使用VoIP推送通知 ,该通知没有什么限制,例如,应用程序必须是相关的VoIP服务。

I recommend to change the push notification payload. 我建议更改推送通知有效负载。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM