在IOS5上收到“ kCTMessageReceivedNotification”通知时如何获取消息

使用ios4.x,我可以使用以下代码在收到“ kCTMessageReceivedNotification”通知时获取消息


CTTelephonyCenterAddObserver( ct, NULL, callback,NULL,NULL, CFNotificationSuspensionBehaviorHold); 


if ([notifyname isEqualToString:@"kCTMessageReceivedNotification"])//receive message

    {


        NSDictionary *info = (NSDictionary *)userInfo;

        CFNumberRef msgID = (CFNumberRef)[info objectForKey:@"kCTMessageIdKey"];

        int result;

        CFNumberGetValue((CFNumberRef)msgID, kCFNumberSInt32Type, &result);   

        Class CTMessageCenter = NSClassFromString(@"CTMessageCenter");

        id mc = [CTMessageCenter sharedMessageCenter];

        id incMsg = [mc incomingMessageWithId: result];}

但是使用ios5却无法完成,因为incMsg为nil,那么我该怎么做才能得到消息?


MYYA
浏览 607回答 3
3回答

回首忆惘然

这是我发现的...仅查看转储的私有API,看起来ChatKit.framework可能会有所帮助。看看 CKSMSService.h或CKMadridService.h用于iMessage消息。我确实尝试尝试使用自己的方法,其中包括以下几种方法CKSMSService:- (void)_receivedMessage: (id)arg1 replace:(BOOL)arg2 replacedRecordIdentifier:(int)arg3 postInternalNotification:(BOOL)arg4;- (void)_receivedMessage: (id)arg1 replace:(BOOL)arg2 postInternalNotification:(BOOL)arg3;但是在iOS 5.0.1上,我没有看到任何一个被调用(也许是我的错误?)。因此,我试图直接从sqlite SMS数据库获取消息。注意...我没有构建完整的应用程序来注册通知。我假设您的代码可以kCTMessageReceivedNotification正常使用...只是不再为您提供SMS 内容。因此,如果将以下代码放在通知处理程序中,则应该能够看到消息文本:- (NSString *) mostRecentSMS  {     NSString *text = @"";    sqlite3 *database;    if(sqlite3_open([@"/private/var/mobile/Library/SMS/sms.db" UTF8String], &database) == SQLITE_OK) {        sqlite3_stmt *statement;        // iOS 4 and 5 may require different SQL, as the .db format may change        const char *sql4 = "SELECT text from message ORDER BY rowid DESC";  // TODO: different for iOS 4.* ???        const char *sql5 = "SELECT text from message ORDER BY rowid DESC";        NSString *osVersion =[[UIDevice currentDevice] systemVersion];                 if([osVersion hasPrefix:@"5"]) {            // iOS 5.* -> tested            sqlite3_prepare_v2(database, sql5, -1, &statement, NULL);        } else {            // iOS != 5.* -> untested!!!            sqlite3_prepare_v2(database, sql4, -1, &statement, NULL);        }        // Use the while loop if you want more than just the most recent message        //while (sqlite3_step(statement) == SQLITE_ROW) {        if (sqlite3_step(statement) == SQLITE_ROW) {            char *content = (char *)sqlite3_column_text(statement, 0);            text = [NSString stringWithCString: content encoding: NSUTF8StringEncoding];            sqlite3_finalize(statement);        }        sqlite3_close(database);    }    return text;}    现在,请确保将此应用程序安装在/ Applications /中。如果您仅构建此应用程序,并使用Xcode正常安装,则由于应用程序沙箱操作,打开sqlite数据库时会出现拒绝权限错误。我的代码段仅获取最新的文本内容。 这是使用数据库做更多事情的示例。看一下QuerySMS方法。另外,这是sms.db 的数据库格式的链接。您可以在那里找到其他所需的东西。或者,只需将sms.db复制到您的计算机,然后使用Firefox SQLiteManager插件之类的文件进行浏览。祝好运!

噜噜哒

我设法在没有越狱的iOS8设备上获得最后一条消息:CKDBMessage.h从ChatKit标题获取并将文件添加到您的项目。kCTMessageReceivedNotification通过注册CTTelephonyCenterAddObserver使用此功能可获取最后收到的消息的信息:void SmsReceived(){    NSLog(@"GOT SMS");    //open IMDPersistence framework    void *libHandle =     dlopen("/System/Library/PrivateFrameworks/IMDPersistence.framework/IMDPersistence", RTLD_NOW);    //make/get symbol from framework + name    IMDMessageRecordGetMessagesSequenceNumber = (int (*)())dlsym(libHandle, "IMDMessageRecordGetMessagesSequenceNumber");    // get id of last SMS from symbol    int lastID = IMDMessageRecordGetMessagesSequenceNumber();    NSLog(@"%d", lastID);    // close (release?) framework -> needed??    dlclose(libHandle);    // get message object    dlopen("/System/Library/PrivateFrameworks/ChatKit.framework/ChatKit", RTLD_LAZY);    Class CKDBMessageClass = NSClassFromString(@"CKDBMessage");// objc_getClass("CKDBMessage");    CKDBMessage *msg = [[CKDBMessageClass alloc] initWithRecordID:lastID];    NSString *text = msg.text;    NSLog(@"text: %@", text);}
打开App,查看更多内容
随时随地看视频慕课网APP