簡體   English   中英

用於遠程和本地通知的iOS徽章

[英]iOS badges for both remote and local notifications

我的應用程序同時接收推送和本地通知兩種類型,我不知道管理徽章編號的最佳方法是什么。 在本地通知的情況下,每創建一個便會增加徽章計數器,但在推送通知的情況下……如果我沒有記錯,則僅當用戶通過點擊來啟動應用程序時,才會調用委托方法didReceiveRemoteNotification:警報或橫幅,或者該應用程序是否處於活動狀態。 否則,徽章的值將從通知的有效負載中獲取,不是嗎? 那么...考慮到本地通知+收到的遠程通知,我如何始終獲得更新的徽章值?

謝謝

您在遠程通知中得到了響應,如下所示。

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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM