如何在iOS推送通知中增加徽章 [英] How to do badge increment on iOS push notification

查看:139
本文介绍了如何在iOS推送通知中增加徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从有效载荷中获取徽章.但是我该如何更新该值.

I'm currently getting badge from payload. But how can i update that value.

{"aps":
    {"alert":"Notification Hub test notification2",
     "badge":1,
     "sound":"Default"}
}

当我发送此消息时,其徽章编号始终显示1,但是当我进入应用程序并退出时,它将得到更新.正是由于这个原因,

when i send this it is always shows 1 in the badge number but when i go inside app and exit it's getting updated.It is because of this,

func applicationDidBecomeActive(application: UIApplication) {
        UIApplication.sharedApplication().applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    }

但是当我发送新的推送通知时,它再次在图标中显示1作为徽章编号. 我应该怎么做,据我了解,当我发送有效载荷时,我应该发送更新的徽章.为此,我必须处理后端.

But when i send a new push notification it's again shows 1 as badge number in the icon. How should i do this, to my understand i should send updated badge when i send the payload. For that i have to deal with backend.

除此以外,还有其他建议可以处理应用内部吗?

Is there any suggestion other than this to handle inside app?

推荐答案

我会给你的方法:

我的计划是使用KVO收听[UIApplication sharedApplication].applicationIconBadgeNumber

My plan is to use KVO to listen the [UIApplication sharedApplication].applicationIconBadgeNumber

- (void)setupBadgeOperation {
    [[UIApplication sharedApplication] addObserver:self forKeyPath:@"applicationIconBadgeNumber" options:NSKeyValueObservingOptionNew context:nil];
}

值更改后,我使用[[NSNotificationCenter defaultCenter] postNotificationName:STATUS_BADGENUMBER_CHANGED object:nil]通知UI,需要在哪里修改徽章的更改.

And once value changed, I use [[NSNotificationCenter defaultCenter] postNotificationName:STATUS_BADGENUMBER_CHANGED object:nil] to inform the UI where needs to be modified for the change of badge.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"applicationIconBadgeNumber"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:STATUS_BADGENUMBER_CHANGED object:nil];
    }
}

在三种情况下会收到远程通知以更改[UIApplication sharedApplication].applicationIconBadgeNumber.

There is three case of receiving a remote notification to change the [UIApplication sharedApplication].applicationIconBadgeNumber.

a.应用程序是前景
b.应用程序是背景
C.应用未启动

a. app is foreground
b. app is background
c. app is not launch

在a和b的情况下,此方法将被称为:

in a and b case, this method will be called:

- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
          fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    void(^tapBlock)(void(^completeHandler)()) = ^(void(^completeHandler)()) { 
    // here I remove some code not related with the question
    NSNumber *badgeNumber = userInfo[@"aps"][@"badge"];
    [UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumber.integerValue;
} 

在c情况下,无法获取回调,因此我将手动进行.

in c case, there is no way to get the callback, so I will do it manually.

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[NSNotificationCenter defaultCenter] postNotificationName:STATUS_BADGENUMBER_CHANGED object:nil];
}

仅此而已,对我来说很好.

That's all, and works well for me.

这篇关于如何在iOS推送通知中增加徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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