如何更新IOS推送通知中的徽章编号? [英] How to update the badge number in IOS push notification?

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

问题描述

我最近使用 Amazon SNS 为我的 IOS 应用推送通知.

I recently use the Amazon SNS to push notification for my IOS app.

效果很好,我遇到的唯一问题是当我收到通知时,徽章编号不会更新,我是这样实现的:

It works well, the only problem I have encountered is when I receive the notification , the badge number will not be updated, here is how I implement:

首先我按照这里的例子https://aws.amazon.com/articles/9156883257507082这是教程中的示例代码.

First I follow the example here https://aws.amazon.com/articles/9156883257507082 Here is the example code from the tutorial.

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    application.applicationIconBadgeNumber = 0;
    NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
    NSLog(@"%@",msg);
    [[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];
}


-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Register for push notification
    application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    if(launchOptions!=nil){
        NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
        NSLog(@"%@",msg);
        [[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];
    }

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    Message_BoardViewController *boardViewController = [Message_BoardViewController new];
    UINavigationController *navigationController = [UINavigationController new];

    navigationController.navigationBar.translucent = NO;

    [navigationController pushViewController:boardViewController animated:NO];
    [boardViewController release];

    self.window.rootViewController = navigationController;
    [navigationController release];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];

    [self.window makeKeyAndVisible];

    // Logging Control - Do NOT use logging for non-development builds.
#ifdef DEBUG
    [AmazonLogger verboseLogging];
#else
    [AmazonLogger turnLoggingOff];
#endif

    [AmazonErrorHandler shouldNotThrowExceptions];

    return YES;
}

如您所见,从教程代码中可以看出

As you can see, from the tutorial code it state

application.applicationIconBadgeNumber = 0;

所以很明显每次都会变成0.

so obviously it will become 0 every time.

======================================================

======================================================

我想知道更新徽章编号的标准方法是什么?

哪种方法是正确的?

1) 通过这样的编程application.applicationIconBadgeNumber = 0;

2) 或者像这样来自服务器有效负载?

2) Or from the server payload like this?

    $body = array(
'alert' => $message,
'sound' => 'sound.caf',
'badge' => $badge_count // Should be int type
);

========================================================

========================================================

但是,我发现每个应用程序都有障碍,对于 1),didReceiveNotification 在应用程序处于后台时不会触发,所以我做不到类似于 application.applicationIconBadgeNumber++; 来更新徽章编号.

However, I found there is obstacle for each apporach, for the 1), the didReceiveNotification is not fire when the app is at background , so I can not do something like application.applicationIconBadgeNumber++; to update the badge number.

对于2),Amazon SNS服务只是返回

for the 2) , the Amazon SNS service is only return

$body = array(
'alert' => $message,
); 

以及服务器如何知道徽章编号并将其添加到服务器有效负载中,看来我仍然需要在 didReceiveNotification 中将更新徽章编号发布到亚马逊并将其添加到有效负载中.但同样,它不会在后台调用.

and simply how can the server knows the badge number and add it at server payload, it seems I still need to post the update badge number to Amazon in didReceiveNotification and add it to payload. But again, it does not call at background.

对不起,IOS 编程新手,有经验的程序员可以指导我通过推送通知实现徽章编号更新吗?谢谢.

Sorry for new to IOS progamming, would some experience programmer kindly guide me to implement the badge number update with push notification? Thanks.

推荐答案

您应该在推送通知的负载中发送徽章编号.您的服务器应该设置徽章计数.这样,无论应用程序是否正在运行(也无论用户是否点击通知以打开应用程序),徽章计数都会更新.您的服务器应该知道要发送的号码.

You are supposed to send the badge number in the payload of the push notification. Your server should set the badge count. This way the badge count will be updated regardless of whether the app is running or not running (and regardless of whether the user taps the notification to open the app or not). Your server should know what number to send.

至于您的服务器应该如何知道要发送的徽章编号 - 我不知道您的应用程序的逻辑,但让我们以电子邮件应用程序为例-假设您希望徽章编号显示有多少未读消息存在.应用程序应该在读取消息时通知服务器,以便服务器可以为每个用户维护正确的徽章编号.这样,即使用户有多种方式访问​​数据(iPhone 应用程序、iPad 应用程序、桌面浏览器等),服务器也会知道当前的徽章计数,并且每个设备都会显示正确的徽章计数.

And as to how your server is supposed to know what badge number to send - I don't know your app's logic, but lets take an email application as an example - suppose you want the badge number to show how many unread messages exist. The app should notify the server whenever a message is read, so the server can maintain the correct badge number for each user. This way, even if the user has multiple ways to access the data (iPhone app, iPad app, desktop browser, etc...), the server would know the current badge count and each device would show the correct badge count.

application.applicationIconBadgeNumber = 0 用于在打开应用程序后清除徽章.它应该在 didReceiveRemoteNotificationdidFinishLaunchingWithOptions 中完成,以便在应用程序启动时或应用程序处理推送通知时始终清除徽章编号.

The application.applicationIconBadgeNumber = 0 is meant to clear the badge once the application is opened. It should be done in both didReceiveRemoteNotification and didFinishLaunchingWithOptions, so that the badge number will always be cleared when the application is launched, or when the push notification is handled by the app.

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

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