简而言之,我有三个视图:Main View
、List 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);
}
在主视图中,句柄ReceivedRemoteNotification
和SetUnreadIcon
-(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:
工作场景
假设用户Alice
向Bob
. 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
其他地方实施吗?
谢谢,