0

简而言之,我有三个视图:Main ViewList View(显示好友列表)和Chat View(您在哪里聊天)。

推送通知已在此应用程序中正确设置,因此我在接收推送通知有效负载时没有问题,我的问题在于为什么在特定情况下接收到的消息似乎丢失并且没有出现在Chat View

我有一个奇怪的问题,所以请继续阅读整个描述:

所以在这里我放了一些代码来展示我如何处理推送通知:

在 AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Extract the Sinch-specific payload from the Apple Remote Push Notification
    NSString* payload = [userInfo objectForKey:@"SIN"];
    [self initSinchClientWithUserId:userid];

    // Get previously initiated Sinch client
    id<SINNotificationResult> result = [_client relayRemotePushNotificationPayload:payload];
    if ([result isMessage]) {
        // Present alert notifying
        NSString *messageId = [[result messageResult] messageId];
        NSLog(@"Received messageid: %@", messageId);

        if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground ||
           [UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
            self.messageSenderId = [[result messageResult] senderId];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedRemoteNotification" object:self userInfo:nil];

            NSLog(@"handle remote notification in modal");

        } else {
            self.messageSenderId = [[result messageResult] senderId];
            // this will launch a modal of private chat, so better to invoke this when running in background
            [[NSNotificationCenter defaultCenter] postNotificationName:@"SetUnreadIcon" object:self userInfo:nil];
            NSLog(@"handle remote notification by marking icons");

        }

    } else if (![result isValid]) {
        // Handle error
    }
    NSLog(@"Received Payload: %@", payload);
}

在主视图中,句柄ReceivedRemoteNotificationSetUnreadIcon

-(void) viewDidLoad {

[super viewDidLoad];
....
// setup a local notification handler
    NSNotificationCenter *notifCentre = [NSNotificationCenter defaultCenter];
    [notifCentre addObserver:self selector:@selector(invokeRemoteNotifications:) name:@"ReceivedRemoteNotification" object:nil];
    [notifCentre addObserver:self selector:@selector(setUnreadIcon:) name:@"SetUnreadIcon" object:nil];
....
}

- (void)invokeRemoteNotifications: (NSNotification *) notification {
    shouldDismiss = NO;
    [self invokeNotifications];
}

- (void)invokeNotifications {
    [self performSegueWithIdentifier:@"modalToChat" sender:self];
}

- (void)setUnreadIcon: (NSNotification *) notification {
    AudioServicesPlaySystemSound (1300);
    shouldDismiss = YES;
    [self.rightbutton setCustomView:notifyButton];
}

当用户rightbutton在 MainView 中点击时,将调用ListView.

在 ChatView 中,它实现了SINMessageClientDelegate委托方法(参见文档),即

– messageClient:didReceiveIncomingMessage:
– messageSent:recipientId:
– messageDelivered:
– messageFailed:info:
– message:shouldSendPushNotifications:

工作场景

假设用户AliceBob. Bob通过推送通知收到此消息。因此,Bob打开并点击通知,应用程序将显示并自动显示ChatView.

在这种情况下,来自推送通知的消息可以显示在Chat View

不工作的场景

假设用户再次Alice发送消息Bob,这次Bob在应用程序处于前台时收到通知。所以这个方法会被执行:

- (void)setUnreadIcon: (NSNotification *) notification {
        AudioServicesPlaySystemSound (1300);
        shouldDismiss = YES;
        [self.rightbutton setCustomView:notifyButton];
    }

然后Bob将点击rightbutton调用ListView,找到爱丽丝并打开对话。Bob会发现刚刚收到的消息Alice没有显示在ChatView

问题

messageId在 Sinch SDK 中,即使在接收推送通知时检索消息,似乎也没有手动检索消息的方法。我对么?

在“不工作”的情况下,为什么消息会丢失?如果 sinch 客户端负责中继消息,有什么理由让它丢弃消息?

仍然在“不工作”的情况下,我怎样才能持久化消息,然后再显示它?我必须在SINMessageClientDelegate其他地方实施吗?

谢谢,

4

1 回答 1

3

关于您的第一个问题,无法以您描述的方式查询特定消息。

您的“不工作”案例的问题很可能是当 Sinch SDK 获取即时消息并即将通知消息客户端委托时,您Chat View尚未被实例化/分配为委托,因此代表“错过”了该消息。

我建议您让非 ViewController 组件实现SINMessageClientDelegate,并使该组件具有独立于您的视图控制器的生命周期。即,在应用程序启动时创建充当代理的组件,并使其保持活动状态。然后您可以确保始终接收 all onIncomingMessage:,并且您还可以将用于持久消息的逻辑放置在那里。

于 2014-08-12T08:14:25.220 回答