天天看點

IM-iOS退出背景接受消息,app退出背景能接收到推送

App被失活狀态的時候可以走蘋果的APNS;但是在活躍的時候卻接受不到推送!

那就用到本地推送:UILocalNotification 消息神器。

處理不好可能會有很多本地推送到來,那麼問題來了要在什麼地方去注冊通知?什麼地方去移除通知?

一、要在什麼地方去注冊通知

- (void)applicationDidEnterBackground:(UIApplication *)application;

手機剛進入背景會走的方法,applicationDidEnterBackground;

我會注冊一個通知:名字宏定義

/**應用擷取到重新整理推送消息提醒*/

#define kString_NSNotificationCenterRefreshMessageData    @"kString_NSNotificationCenterRefreshMessageData"

在AppDelegate.m的 applicationDidEnterBackground方法裡邊添加通知

- (void)applicationDidEnterBackground:(UIApplication *)application{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageCome:) name:kString_NSNotificationCenterRefreshMessageData object:nil];

}

- (void)messageCome:(NSNotification *)notifi{

    if (![notifi.name isEqualToString:kString_NSNotificationCenterRefreshMessageData]) {

        return;

    }

    dispatch_async(dispatch_get_main_queue(), ^{

        [self notifi:notifi];

    });

- (void)notifi:(NSNotification *)notifi{

    NSMutableString * notifiMessage = nil;

    RCMessage *message = notifi.object;

    if (message.conversationType == ConversationType_SYSTEM) {

        notifiMessage = [[NSMutableString alloc]initWithString: @"獵上網:"];

    }else if(message.conversationType == ConversationType_PRIVATE){

        MessageUser *user =  [[MyFMDB sharedMyFMDB] findUserWithID:[message.senderUserId intValue]];

        if (user.name&&![user.name isEqualToString:@""]) {

            notifiMessage = [[NSMutableString alloc]initWithString: [NSString stringWithFormat:@"%@:",user.name]];

        }

    }else{

    NSMutableDictionary * inforDic = [NSMutableDictionary dictionary];

    UILocalNotification * locNoti = [[UILocalNotification alloc]init];

    if ([message.content isKindOfClass:[RCTextMessage class]]) {

        RCTextMessage *textMessage = (RCTextMessage *)message.content;

        [notifiMessage appendString:textMessage.content];

        [inforDic setValue:textMessage.content forKey:@"name"];

    }else if([message.content isKindOfClass:[RCImageMessage class]]){

        [notifiMessage appendString:@"圖檔"];

        [inforDic setValue:@"圖檔" forKey:@"name"];

    }else if([message.content isKindOfClass:[RCVoiceMessage class]]){

        [notifiMessage appendString:@"語音"];

        [inforDic setValue:@"語音" forKey:@"name"];

    }else if([message.content isKindOfClass:[IMPositionMessage class]]){

        [notifiMessage appendString:@"職位名片"];

        [inforDic setValue:@"職位名片" forKey:@"name"];

    }else if([message.content isKindOfClass:[IMSwapPhoneMessage class]]){

        [notifiMessage appendString:@"交換電話"];

        [inforDic setValue:@"交換電話" forKey:@"name"];

    }else if([message.content isKindOfClass:[IMResumeMessage class]]){

        [notifiMessage appendString:@"履歷名片"];

        [inforDic setValue:@"履歷名片" forKey:@"name"];

    }else if([message.content isKindOfClass:[TaskedPositionToHunteron class]]){

        TaskedPositionToHunteron *textMessage = (TaskedPositionToHunteron *)message.content;

        [notifiMessage appendString:[NSString stringWithFormat:@"PA(%@)為您定向推薦了一個新的職位( #%lld %@)。",textMessage.paName,textMessage.positionId,textMessage.positionName]];

        [inforDic setValue:textMessage.paName forKey:@"paName"];

        [inforDic setValue:[NSString stringWithFormat:@"%lld",textMessage.positionId]  forKey:@"positionId"];

        [inforDic setValue:textMessage.positionName forKey:@"positionName"];

    //1.1 設定通知的内容

    locNoti.alertAction = notifiMessage; // 鎖屏狀态下顯示: 滑動來快點啊

    locNoti.alertBody = notifiMessage;

    //1.2 設定通知的發送時間

    locNoti.fireDate = [NSDate date];

    locNoti.userInfo =inforDic;

    //1.3 設定時區,一般預設

    locNoti.timeZone = [NSTimeZone defaultTimeZone];

    // 設定通知發送時, 提醒數字(==0, 會自動消失)

    locNoti.applicationIconBadgeNumber = 0;

    locNoti.repeatInterval = 0;

    // 2. 發送通知

    [[UIApplication sharedApplication]scheduleLocalNotification:locNoti];

    NSLog(@"====%d",[NSThread isMainThread]);

    [[UIApplication sharedApplication]cancelLocalNotification:locNoti];

二、什麼地方去移除通知

手機剛進入前台會走的方法

- (void)applicationWillEnterForeground:(UIApplication *)application{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:kString_NSNotificationCenterRefreshMessageData object:nil];

因為手機不活躍的時候不能立即發通知!記住是立即,又不是延遲發本地推送,是以不需要處理已經不活躍的情況!要在進入前台的時候移除通知,要不然下次在進入背景會在此注冊通知!就會顯示兩條本地推送!

繼續閱讀