在坚果壳中,我有三个视图:
Main View
,
List 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
委托方法(
See Documentation
),即
â messageClient:didReceiveIncomingMessage:
â messageSent:recipientId:
â messageDelivered:
â messageFailed:info:
â message:shouldSendPushNotifications:
工作场景
假设用户
Alice
将消息发送到
Bob
.
上下快速移动
通过推送通知收到此消息。所以
上下快速移动
打开并点击通知,应用程序将显示并自动显示
ChatView
.
在这种情况下,推送通知中的消息可以显示在
聊天视图
非工作场景
假设用户
爱丽丝
将消息发送到
上下快速移动
再次,这次
上下快速移动
在应用程序处于前台时收到通知。因此,将执行此方法:
- (void)setUnreadIcon: (NSNotification *) notification {
AudioServicesPlaySystemSound (1300);
shouldDismiss = YES;
[self.rightbutton setCustomView:notifyButton];
}
然后
上下快速移动
将轻敲
右按钮
打电话来
列表视图
,找到爱丽丝并打开对话。
上下快速移动
将发现刚刚从
爱丽丝
未在中显示
聊天视图
问题
在Sinch SDK中,似乎没有手动检索消息的方法,即使是
messageId
在接收推送通知时检索。我说得对吗?
在“不工作”的情况下,为什么消息会丢失?如果sinch客户端负责转发消息,那么有什么理由让它丢弃消息?
仍然在“不工作”的情况下,我如何保存消息,然后稍后显示它?我必须实施
SINMessageClient委托
在别处
谢谢