简体   繁体   English

增加推送通知徽章 iPhone

[英]Increment the Push notification Badge iPhone

Is it possible to increment the badge value on receiving the notification.是否可以在收到通知时增加徽章值。 OR Should I send the count as payload?或者我应该将计数作为有效载荷发送吗?

If i am sending the badge value as "1" every time, how could i increment the badge-value in the icon of the app if the app is not open.如果我每次都将徽章值发送为“1”,如果应用程序未打开,我如何增加应用程序图标中的徽章值。

i have used this code but doesn't work.我已经使用了此代码但不起作用。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; 
}

Usually in all apps the unread notification counts are maintained in the server.通常在所有应用程序中,未读通知计数都保存在服务器中。 When the server sends a push notification to a particular device token they send the badge count along with the payload.当服务器向特定设备令牌发送推送通知时,它们会随负载一起发送徽章计数。 Once the device is notified and your app is in background(or killed) the OS automatically update the badge count to your app icon.一旦设备收到通知并且您的应用程序处于后台(或被终止),操作系统会自动将徽章计数更新为您的应用程序图标。 In case whether you have your app running, you will get notified in the如果您的应用程序是否正在运行,您将在

application:didReceiveRemoteNotification:

delegate and thus you are able to receive the badge count from the (NSDictionary *)userInfo.委托,因此您可以从 (NSDictionary *)userInfo 接收徽章计数。 And thus you are able to update the app icon badge count using the function因此,您可以使用该功能更新应用程序图标徽章计数

[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];

Think this should help you.认为这应该对您有所帮助。

如果应用程序未打开,除了有效载荷之外,您将无法增加徽章。

When a Push Notification comes while your application is in background mode & you want to increment the Badge Number, you should send a badgeCount to the server, so that the server knows the current count.当一个推送通知来当你的应用程序在后台模式和要递增证件号码,你应该送badgeCount到服务器,让服务器知道当前计数。

If you manage the badge count from the Server Side then this code is enough:-如果您从服务器端管理徽章计数,那么此代码就足够了:-

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  {
    NSLog(@"remote notification: %@",[userInfo description]);

    if (userInfo) {
        NSLog(@"%@",userInfo);

        if ([userInfo objectForKey:@"aps"]) { 
            if([[userInfo objectForKey:@"aps"] objectForKey:@"badgecount"]) {
                [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
            }
        }
    }
}

Urban Airship 使用他们的“自动徽章”功能支持这一点

After receiving remote Notification when you open App,打开App时收到远程通知后,

get current Badge number in "didBecomeActive" Method of your Appdelegate File using below code:使用以下代码在 Appdelegate 文件的“didBecomeActive”方法中获取当前徽章编号:

int badgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badgeCount = badgeCount + 1;

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

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