gpt4 book ai didi

ios - 无法显示推送通知中的 Sinch 消息

转载 作者:行者123 更新时间:2023-11-28 19:58:21 25 4
gpt4 key购买 nike

简而言之,我有三个 View :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];
}

当用户在MainView中点击rightbutton时,会调出ListView

在ChatView中,它实现了SINMessageClientDelegate委托(delegate)方法(See Documentation),即

– 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,找到 Alice 并打开对话。 Bob会发现Alice刚刚收到的消息没有显示在ChatView

问题

在 Sinch SDK 中,似乎没有办法手动检索消息,即使在接收推送通知时检索了 messageId。我说得对吗?

在“Not Working”的情况下,为什么消息丢失了?如果由 sinch client 负责中继消息,什么原因让它丢弃消息?

还是在“Not Working”的情况下,我如何才能持久化消息,然后再显示出来?我必须在别处实现 SINMessageClientDelegate 吗?

谢谢,

最佳答案

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

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

我建议您让一个非 ViewController 组件实现 SINMessageClientDelegate,并使该组件具有独立于您的 View Controller 的生命周期。 IE。在应用程序启动时创建充当委托(delegate)的组件,并使其保持事件状态。这样您就可以确保始终收到所有 onIncomingMessage:,并且您还可以将用于持久化消息的逻辑放在那里。

关于ios - 无法显示推送通知中的 Sinch 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25248610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com