在 ios 8 推送通知中没有显示当前应用程序的徽章

Posted

技术标签:

【中文标题】在 ios 8 推送通知中没有显示当前应用程序的徽章【英文标题】:In ios 8 push notification not showing me badges for current app 【发布时间】:2015-06-01 06:33:59 【问题描述】:

您好朋友,我是推送通知的新手,请帮帮我。 我正在尝试获取我的应用程序的徽章计数,但“NSDictionary userInfo”仅包含 alert 和 sound 。 这是注册设备推送通知的代码

  -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

 if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])

    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeAlert |  UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

else

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];

现在 当我从服务器收到消息时。

 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    
    NSLog(@"%@",userInfo);
    if (application.applicationState == UIApplicationStateActive)
    

        /***********code to show alert********/
        if (![[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]] isEqualToString:@""] && [[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]]!=nil)
        
            NSString *MSG =[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:MSG delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [alert show];
        

        else
        
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Notification Received." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [alert show];
        
    
        application.applicationIconBadgeNumber =[[[userInfo objectForKey:@"aps"] objectForKey: @"badge"]integerValue];

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

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

    

现在输出是

 aps =     
    
        alert = "MAgic 2 SAlon App!";
        sound = default;
    ;

我的 php 代码是 `

// Put your device token here (without spaces):
$deviceToken = 'xxxxxxxxx';

// Put your private key's passphrase here:
$passphrase = 'xxxxxx';

// Put your alert message here:
$message = 'xxxxxxx!'; 

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
 $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n',  strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);`

【问题讨论】:

【参考方案1】:

到目前为止,您的 iOS 代码没有问题,但在您的 php 脚本中,您缺少徽章计数。


在所有 ios 应用程序中,未读通知计数都是从后端/服务器维护的。当后端/服务器向特定设备发送推送通知时,服务器将徽章计数与有效负载一起发送。

所以我猜后端没有向您发送徽章计数。

后端/服务器团队应遵循此格式,以便您可以通过编程方式获取徽章计数:-


    "aps" :  
    
        "alert" : "msg",
        "badge" : 1 ,
        "sound" : "demo.aiff"
    

这里是教程链接,如何在 php 脚本中添加徽章计数:-

http://b2cloud.com.au/tutorial/ios-push-notifications-in-php/

http://learn-php-by-example.blogspot.in/2013/01/working-with-apple-push-notification.html

【讨论】:

请告诉我如何获得徽章计数。我想在应用程序图标上显示它。谢谢您的帮助 现在我知道该有效负载没有发送任何徽章 但是你能告诉我如何在负载中发送徽章 我猜想在您的 php 脚本中添加徽章计数非常容易。如果您仍然没有获得徽章计数,那么您为什么不遵循之后可用的教程,然后发布问题。 @user3189586

以上是关于在 ios 8 推送通知中没有显示当前应用程序的徽章的主要内容,如果未能解决你的问题,请参考以下文章

如何使用swift为IOS推送通知打开新的WebView

ios 8 Simulator,是不是可以向模拟器发送推送通知[重复]

是否可以仅显示 Firebase 推送通知的徽章,而在 IOS(Swift 或 Obj-C)中没有警报?

iOS 静默推送通知失败并显示消息

有没有办法在 ios 中捕获推送通知?

我如何知道在 iOS 中是不是发送了推送通知?