【问题标题】:How to update the badge number in IOS push notification?如何在 IOS 推送通知中更新徽章编号?
【发布时间】:2014-06-02 11:52:44
【问题描述】:

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

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

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

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

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

application.applicationIconBadgeNumber = 0;

很明显它每次都会变成0。

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

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

哪种方法是正确的?

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

2) 还是像这样从服务器负载中获取?

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

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

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

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

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

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

抱歉,IOS 编程新手,请有经验的程序员指导我通过推送通知实现徽章编号更新?谢谢。

【问题讨论】:

    标签: ios amazon-web-services notifications push-notification apple-push-notifications


    【解决方案1】:

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

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

    application.applicationIconBadgeNumber = 0 用于在打开应用程序后清除徽章。在didReceiveRemoteNotificationdidFinishLaunchingWithOptions都应该这样做,这样在启动应用程序时,或者当应用程序处理推送通知时,徽章编号将始终被清除。

    【讨论】:

    • 感谢您的回复,这意味着我应该使用我的问题中提到的方法 2)。但是如何将徽章编号发布到服务器?由于 didReceiveRemoteNotification 不在后台运行。确切地说,我应该把帖子徽章编号功能放在哪里?谢谢
    • @user782104 当用户点击通知或点击应用程序图标启动应用程序时,您应该将徽章编号更新发布到您的服务器,因为在任何一种情况下,您都希望通知服务器用户查看了所有消息。因此,您应该在 didReceiveRemoteNotificationdidFinishLaunchingWithOptions 中都这样做。
    • 对不起,我很困惑。如果它仅在用户点击任一通知/应用程序图标时更新,如果应用程序处于后台时有两个或更多通知怎么办?那么它不会更新吗?谢谢
    • @user782104 如果用户不与您的应用进行交互,则该数字只会随着服务器发送带有增加的徽章编号的通知而增加(由于新数据在服务器上可用),或者如果服务器发送带有减少徽章编号的通知(如果用户通过其他设备查看数据)。只有当用户通过您的应用查看通知时(为此,用户必须点击通知/应用图标),您才应清除徽章并通知服务器重置该用户的徽章编号。
    • 如何通知服务器,查看推送通知后,是否发送带有新徽章计数的 POST 请求?
    【解决方案2】:

    你可以做这样的事情。这里我使用的是 AWS Lambda 和 AWS SNS。

    var params = {
    'TargetArn' : 'Your Target Device',
    'MessageStructure' : 'json',
    'Message' : JSON.stringify({
      'default' : 'New Request from User',
      'APNS_SANDBOX' : JSON.stringify({
        'aps' : { 'alert' : 'New Request from User', 'badge':1,'category' : 'MESSAGE_CATEGORY' },
        'badge' : '1',
        'sound' : 'default',
        'attribute1' : "attribute 1 value",
        'attribute2' : "attribute 2 value",
        'attributeN' : "attribute N value"
      })
    })
    };
    
    sns.publish(params, function(err, data) {
        if (err) {
            console.log(err.stack);
            return;
        }
        console.log('push sent');
        console.log(data);
        context.done(null, 'Function Finished!');  
    });
    

    【讨论】:

      【解决方案3】:

      好的,我知道如何在 Amazon SNS 中使用徽章编号,如果您使用 PHP 发布通知,那么您可以像这样实现徽章

                              $sns->publish(array(
                              'MessageStructure' => 'json',
                              'TargetArn' => $endpointArn,
                              'Message' => json_encode(array(
                                  'APNS_SANDBOX' => json_encode(array(
                                      'aps' => array(
                                          'alert' => $push_message,
                                          'badge' => 1
                                      )
                                  ))
                              )),
                          ));
      

      别忘了在你的 appdelegate.m、onReceive、onLaunchwithoption 和 onCompletefromBackground 中将徽章设置为零(不记得 excat 的拼写)

      【讨论】:

        【解决方案4】:

        请注意,这里要识别的关键是您发送到 AWS 的 SNS 服务的 JSON 中的属性名称。在您的开发环境中,您将通过 APNS 沙箱进行发送,因此您的 JSON 格式如下:

        {
            "default": "Test push-to-token notification.",
            "APNS_SANDBOX":"{\"aps\":{\"alert\":\"Test push-to-token notification.\",\"sound\":\"default\",\"badge\": 1}}",
            "GCM":"{\"data\":{\"body\":\"Test push-to-token notification.\",\"action_button_text\":\"OK\",\"sound\":\"default\"}}"
        }
        

        当您投入生产时,您需要将 "APNS_SANDBOX" 更改为 "APNS"。比如:

        {
            "default": "Test push-to-token notification.",
            "APNS":"{\"aps\":{\"alert\":\"Test push-to-token notification.\",\"sound\":\"default\",\"badge\": 1}}",
            "GCM":"{\"data\":{\"body\":\"Test push-to-token notification.\",\"action_button_text\":\"OK\",\"sound\":\"default\"}}"
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-02
          • 2016-11-23
          相关资源
          最近更新 更多