gpt4 book ai didi

ios - 推送通知 applicationIconBadgeNumber 未在 ios7 中更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:53 26 4
gpt4 key购买 nike

我在应用程序委托(delegate)中使用以下代码来接收远程通知,但是当应用程序在后台运行时,它的 applicationIconBadgeNumber(以红色/白色显示在应用程序图标的左上角)没有更新。当推送通知收到并出现在屏幕的上角并带有幻灯片动画时,通知负载中的角标(Badge)计数从服务器端完美地传来。

didFinishLaunchingWithOptions 中,我输入了以下代码

[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

远程通知委托(delegate):

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *deviceTokenTrimmed = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
currentdeviceToken=deviceTokenTrimmed;
[[NSUserDefaults standardUserDefaults] setValue:deviceTokenTrimmed forKey:@"pushtoken"];
NSLog(@"Device Token didRegisterForRemoteNotificationsWithDeviceToken : %@",deviceTokenTrimmed);

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Device Token in FailToRegister RemoteNotifications ERROR %@",error);
}

- (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];
}
}else{
application.applicationIconBadgeNumber =[[[userInfo objectForKey:@"aps"] objectForKey: @"badge"]integerValue];
}

}

提前致谢。

最佳答案

我发现 sol,实际上 application:didReceiveRemoteNotification 在应用程序在后台时不被调用,当应用程序在后台时它的 os 负责更新 applicationIconBadgeNumber,在我的例子中,实际问题出在有效负载上。从服务器端它被设置为字符串,但它应该是整数。我正在发布用 php 编写的有效负载正确代码。

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'sound.caf',
'badge' => $badge_count // Should be int type
);

谢谢,mokujin 和 Himanshu

关于ios - 推送通知 applicationIconBadgeNumber 未在 ios7 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22248149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com