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{

        return;

    }

    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];

 

}

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM