【问题标题】:iOS badges for both remote and local notifications用于远程和本地通知的 iOS 徽章
【发布时间】:2014-07-03 06:49:30
【问题描述】:

我的应用接收推送和本地两种类型的通知,我不知道管理徽章编号的最佳方式是什么。在本地通知的情况下,每当我创建一个时,我都会增加徽章计数器,但在推送通知的情况下......如果我没记错的话,只有当用户通过点击启动应用程序时才会调用委托方法 didReceiveRemoteNotification:警报或横幅,或者应用程序是否处于活动状态。否则,徽章的值是从通知的有效负载中获取的,不是吗?那么......考虑到收到的本地通知 + 远程通知,我怎么能始终获得更新的徽章值?

谢谢

【问题讨论】:

    标签: ios apple-push-notifications uilocalnotification badge


    【解决方案1】:

    您在远程通知中得到如下响应。

    Connection OK sending message :{"aps":{"alert":"pushnotification testing","content-available":"","badge":"1","sound":"default"}} 134
    

    在 AppDelegte.h 类中

    取一个整数变量int i=0;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        application.applicationIconBadgeNumber = 0;
    
        UILocalNotification *localNoty=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    
        if (localNoty) {
            NSLog(@"Recieved Notification %@",localNoty);
        }
    
        return YES;
    }
    

    //收到通知

    - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
        // Handle the notificaton when the app is running
    
        app.applicationIconBadgeNumber=i++;
        NSLog(@"Recieved Notification %@",notif);
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    #if !TARGET_IPHONE_SIMULATOR
        NSString *badge = [apsInfo objectForKey:@"badge"];
        NSLog(@"Received Push Badge: %@", badge);
    
        // set it to Zero will clear it.
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
    #endif
    }
    
    -(void) pw_setApplicationIconBadgeNumber:(NSInteger) badgeNumber
    {
        [UIApplication sharedApplication].applicationIconBadgeNumber +=badgeNumber;
    }
    

    // 当应用激活时将徽章设置为零。

    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
        application.applicationIconBadgeNumber =nil;
    
    }
    

    【讨论】:

    • 谢谢。 pw_setApplicationIconBadgeNumber: 是一种与Pushwoosh 一起使用的方法,对吧?我不使用它。我发现的问题是我的应用程序显示推送通知以及本地通知,即使我的服务器跟踪它需要发送的推送通知的徽章编号,我也不知道如何添加当前本地通知已启动...
    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    相关资源
    最近更新 更多