iphone - 应用程序关闭时更新徽章图标

标签 iphone ipad apple-push-notifications

当我收到 PN 时,我正在尝试更新我的应用程序的徽章图标(已关闭)。

我尝试添加代码,但当我的应用程序关闭时它不起作用。当应用程序在前台运行时它会起作用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
       NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

//Accept push notification when app is not open
    if (remoteNotif) {
      [application setApplicationIconBadgeNumber:100];
    return YES;
    }

}
    -(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    {

            [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 30];

    }

最佳答案

如果您的应用程序已关闭或在后台运行,则推送通知不会唤醒它。您需要在服务器端执行此操作,并在通知负载中包含您希望在图标上看到的数字:

{
    "aps" : {
        "alert" : "Your notification message",
        "badge" : 1
    }
}

查看 Push Notification programming guide 上的 Apple 文档

关于iphone - 应用程序关闭时更新徽章图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17781130/

相关文章:

iphone - 在 iPhone 5 上选择后置麦克风

ios - 如何更改 UIActivityViewController 中的文本颜色和图标颜色

ios - 带有 UISplitViewController 的横向 iPad 应用程序

ios - Flutter IOS FCM 推送通知未进入通知栏

iphone - 将通知更新推送到现有应用程序 : Will Users Be asked to allow or not?

ios - 无法在 iOS 设备上从 Apple Store 接收通知(fcm)

IOS:在自定义 View Controller 和 View Controller 之间传回数据

iphone - 添加 subview 后未收到touchesEnded/Moved/Cancelled

ios - 如何调试接收幻象点击的 iPhone?

iphone - 沿曲线滚动