0

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,

dumbfingers
  • 7,001
  • 5
  • 54
  • 80

1 Answers1

3

Regarding your first question, there is no way to query for a particular message in that way you are describing.

The problem for your "Not Working"-case is likely that by the time the Sinch SDK fetches the instant message and is about to notify the message client delegate, your Chat View has not yet been instantiated / assigned to be the delegate, and because of that the message is "missed" by the delegate.

I would recommend you to have a non-ViewController component implement the SINMessageClientDelegate, and make that component have a life-cycle that is independent of your view controllers. I.e. have the component acting as the delegate be created at application launch, and keep it alive. Then you can be sure to always receive all onIncomingMessage:, and you can also place logic for persisting messages there.

VMai
  • 10,156
  • 9
  • 25
  • 34
cahlbin
  • 1,039
  • 10
  • 17
  • thanks for the help. I understand and have the same thought about why the Sinch Client is "missing" the message. Could you point me to a correct direct on how to implement such a "non-view controller" component? Is it like a "delegate" that implement the SinchMessageClient? thanks – dumbfingers Aug 12 '14 at 20:33
  • I end up using a delegate to handle all the Sinch Events. thx for your help – dumbfingers Aug 16 '14 at 12:16