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

查看:249
本文介绍了如何更新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
下面是本教程的例子code。

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

正如你可以看到,从教程code将其状态

As you can see, from the tutorial code it state

application.applicationIconBadgeNumber = 0;

所以很明显这将成为0每一次。

so obviously it will become 0 every time.

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

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

我不知道什么是更新工号的标准方法?

这appoarch是正确的?

Which appoarch is correct ?

1)通过这样的节目 application.applicationIconBadgeNumber = 0;

1) Through programming like this 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
);

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

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

然而,我发现有每个apporach 的障碍,为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),亚马逊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 意味着一旦应用程序被打开,清除徽章。它应该在这两个 didReceiveRemoteNotification didFinishLaunchingWithOptions ,让工号将永远被清除时,启动应用程序来完成时,或当推通知是由该应用处理。

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