In a nut shell, I have three views: Main View
, List View
(to display the buddies list) and Chat View
(where you chat).
The push notification is correctly setup in this app, so I have no problems in receiving the push notification payload, my question lies on why in a specific scenario the received message seemed lost and doesn't appear in Chat View
I have a strange problem, so please carry on read the whole description:
So here I put some code to show how I handle the push notifications:
In 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);
}
In Main View, handles ReceivedRemoteNotification
and 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];
}
When user tap rightbutton
in MainView, will call up the ListView
.
In ChatView, it implements SINMessageClientDelegate
delegate methods (See Documentation), namely
– messageClient:didReceiveIncomingMessage:
– messageSent:recipientId:
– messageDelivered:
– messageFailed:info:
– message:shouldSendPushNotifications:
The Working Scenario
Assume user Alice
send message to Bob
. Bob
received this message via push notification. So Bob
open and tap the notifications, the app will show up and automatically showing a modal view of ChatView
.
In this case, the message from the push notifications can be displayed in Chat View
The Not Working Scenario
Assume user Alice
send msg to Bob
again, this time Bob
received the notification while the app is in foreground. So this method will be executed:
- (void)setUnreadIcon: (NSNotification *) notification {
AudioServicesPlaySystemSound (1300);
shouldDismiss = YES;
[self.rightbutton setCustomView:notifyButton];
}
Then Bob
will tap rightbutton
to call up ListView
, find Alice and open the conversation. Bob
will find that the message just received from Alice
is not displayed in ChatView
Questions
In Sinch SDK, there seemed no approach to manually retrieve the messages even the messageId
is retrieved while receiving the push notification. Am I correct?
In the "Not Working" case, why the message is lost? If the sinch client is responsible for relaying the messages, what reason to make it discard the messages?
Still in the "Not Working" case, how I can persist the messages, and then later display it? Must I implement the SINMessageClientDelegate
elsewhere?
Thanks,