Sinch messages in push notifications can't be displayed - ios

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,

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.

Related

Get pending notifications when app enters foreground

You get the notification info in the code if you tap the notification from notification center, but what if user won't tap this, and will just tap the app icon on the home screen?
For example because the notification payload contained also badge property. Then user can tap the app icon, because there is badge number shining, but how do you manage the waiting notifications in the code in this case?
If there is no method for this, then the badge property in notification payload is bit useless, isn't it. Because if user taps the icon with badge number he expects that something will happen, but app is unable to do anything with pending notifications, thus to do anything. Or?
It seems that getDeliveredNotifications method allows this.
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"msg applicationWillEnterForeground");
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// https://stackoverflow.com/a/52840551
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
NSLog(#"msg getDeliveredNotificationsWithCompletionHandler count %lu", [notifications count]);
for (UNNotification* notification in notifications) {
// do something with object
NSLog(#"msg noti %#", notification.request);
}
}];
#endif
}

NSNotificationCenter and didReceiveRemoteNotification

I have some problems with NSNotificationCenter and didReceiveRemoteNotification. I want to open my ViewController when i receive new notification from APNS. Inside body notification i have objectId - it's key.
I try to open my ViewController into didReceiveRemoteNotification but it's not working ((
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter]
postNotificationName:kDidReceiveRemoteNotification
object:userInfo];
}
NewsDetailViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(didReceiveRemoteNotification:)
name:kDidReceiveRemoteNotification
object:nil];
}
- (void)didReceiveRemoteNotification:(NSNotification *)notification
{
NSLog(#"%s %#",__func__,[notification.userInfo description]);
}
Const.h
#define kDidReceiveRemoteNotification #"UIApplicationDidReceiveRemoteNotification"
ViewController not loaded. i dont know what to do.
The current flow of the sample code you've attached is:
When receiving push notification, post a notification to NSNotificationCenter (something completely different and unrelated to push notifications).
When another view is loaded (someone needed to load this view before the push notification was received), subscribe to this NSNotificationCenter notification.
When the notification is posted, print it to log.
It was hard for me to understand this from your question, but if the view controller you're trying to launch as a result of receiving the push notification is NewsDetailViewController, then your code doesn't do that. What you're code does is prints out the notification to log (assuming that somebody else made sure that NewsDetailViewController is loaded before the push notification was received).
In order to load the NewsDetailViewController when the push notification is received, you don't need to post the notification to NSNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NewsDetailViewController *newsVC = [[NewsDetailViewController alloc] initWithNibName:#"NewsDetailViewController" bundle:nil];
[self.window.rootViewController.view addSubview:newsVC.view];
}
Or any other loading logic that works better for you. But in the current code you posted, there is no connection between receiving the push notification and loading the ViewController.
I hope this helps. Good luck!

IOS - Listen for whatsapp event notification in background

I'm trying to fire an event every time when the cellphone receives a message from whatsapp. I'm not sure if this is posible. This is what I'm doing so far to run a thread on background.
ViewDidLoad:
[self performSelector:#selector(MyFunction)]
Method:
-(void)MyFunction {
sleep(3);
NSLog(#"Hello World!");
[self performSelector:#selector(func1)];
}
Now I'm waiting for an incoming whatsapp message using this code inside MyFunction method:
...
...og(#"Hello World!");
NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
[notifyCenter addObserverForName:nil
object:nil
queue:nil
usingBlock:^(NSNotification* notification){
// Explore notification
NSLog(#"Notification found with:"
"\r\n name: %#"
"\r\n object: %#"
"\r\n userInfo: %#",
[notification name],
[notification object],
[notification userInfo]);
}];
But this last block of code doesn't work, is not printing anything. Maybe I'm not using the method correctly or maybe is not posible to listen whatsapp incoming messages. Has anyone been through this before? because a need a litle help here.
NSNotificationCenter lets objects in your application send notifications to each other (You can also receive some notifications from iOS such as when your app is entering the background or forefround) but it is nothing to do with notifications that are displayed to the user.
On iOS an application has no access to user notification data. The only exception to this is when a user taps on a local or push notification that is associated with your app, you receive that notification in your UIApplicationDelegate class.
This means that on WhatsApp can receive notifications about WhatsApp messages and it isn't possible to do do what you are trying to do.

iOS: How to get all of the push notification userinfo while your App is completely closed and reopened?

I am not sure if this is possible, but I need to grab all of the push notification userinfo when the user opens up the App. I can get all of the push notification userinfo when the App is opened or in the background, but not when the App is completely closed. Any way around this? The code below is how I get the userInfo currently.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
id data = [userInfo objectForKey:#"data"];
NSLog(#"data%#",data);
}
Unfortunately, it's not currently possible client side with that method to query old notifications that have occurred while the app was completely closed. See this question: didReceiveRemoteNotification when in background.
A way around it is to keep track of which notifications you send from your server per user. When didReceiveRemoteNotification: is called, you can take that notification and compare it against the server's messages for the current user. If one of them matches, mark it some way on the server. That way, if there are messages sent when your app is backgrounded, you can query for messages that haven't been marked from the server and get all 'missed' notifications.
The method you are implementing cannot handle both cases. See the "Local and Push Notification Programming Guide":
If your app is frontmost, the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method is called on its app delegate. If your app is not frontmost or not running, you handle the notifications by checking the options dictionary passed to the application:didFinishLaunchingWithOptions: of your app delegate...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Notifications
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo){
//open from notification message
}
return YES;
}
You can add this code to your AppDelegate's applicationWillEnterForeground method:
-(void)applicationWillEnterForeground:(UIApplication *)application {
// this method is called when staring an app that was closed / killed / never run before (after applicationDidFinishLaunchingWithOptions) and every time the app is reopened or change status from background to foreground (ex. returning from a mobile call or after the user switched to other app and then came back)
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
NSLog(#"AppDelegate-getDeliveredNotificationsWithCompletionHandler there were %lu notifications in notification center", (unsigned long)[notifications count]);
for (UNNotification* notification in notifications) {
NSDictionary *userInfo = notification.request.content.userInfo;
if (userInfo) {
NSLog(#"Processed a notification in getDeliveredNotificationsWithCompletionHandler, with this info: %#", userInfo);
[self showPushNotificationInAlertController:userInfo]; // this is my method to display the notification in an UIAlertController
}
}
UIApplication.sharedApplication.applicationIconBadgeNumber = 0;
}];
}
}
Remove this line from the method application didFinishLaunchingWithOptions: if you had included it there, because it clears the badge number and also all notifications in notifications center:
UIApplication.sharedApplication.applicationIconBadgeNumber = 0;
This is currently working in iOS 12, hadn't had the chance to test it in earlier versions.

Which Push Notification caused the app to resume from the background

My App receives APNS Push Notifications, and when a user receive more than one NSNotification, he should be able to open the app in a specific view according the NSNotification tapped.
So in the method
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler
I added this code to save all the notifications
if (self.notifications == nil) {
self.notifications = [[NSMutableArray alloc] init];
}
[notifications addObject:userInfo];
And every time the app becomes active again it does this
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started)
// while the application was inactive.
// If the application was previously in the background,
// optionally refresh the user interface.
[notifications removeAllObjects];
application.applicationIconBadgeNumber = 0;
}
Before removing all the objects and setting the badge to zero, I would like to handle which NSNotification made my app open from the background. And once I have which push NSNotification it was, I would like to pass all the data to a specific view.
Based on your comment about UILocalNotification usage
UILocalNotification has a userInfo property. When you create your local notification from the push notification, set the appropriate information into this property and then when the app delegate receives application:didReceiveLocalNotification: you can use that into to update your UI.
If you not use new iOS7 background fetch notifications.
In - (void)applicationDidBecomeActive:(UIApplication *)application before removing all object from notification array, check for notification
NSDictionary * myNotification = [notifications lastObject];
if (myNotification)
{
// is last notification
}
Is will work because app receive only notification that user tap on it
Send with your push notification, data, and use it when your app receives it.
In this example, i'm using the image file and alert, and sending it to all the views that are registered for this Notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//Posting the notificaiton to the use, if its valid:
NSDictionary *returnDic = [userInfo objectForKey:#"aps"];
if (![returnDic objectForKey:#"alert"]) {
return;
}
NSString *alert = [NSString stringWithFormat:#"%#",[returnDic objectForKey:#"alert"]];
if (![returnDic objectForKey:#"MagnetID"]) {
return;
}
NSString *magnetImage = [returnDic objectForKey:#"MagnetImage"];
NSDictionary *dictionaryToSend = [NSDictionary dictionaryWithObjectsAndKeys:magnetImage,MAGNET_IMAGE,alert,MAGNET_ERROR_MESSEGE, nil];
//Posting to the rest of the views, the messege:
[[NSNotificationCenter defaultCenter] postNotificationName:USER_MESSEGE_RECEAVED object:nil userInfo:dictionaryToSend];
NSLog(#"Notifications - userInfo=%#",userInfo);
}
What you may do, is save the data that you need, by UserDefults or whatever you prefer, and at the "applicationDidBecomeActive" method, use that data to show the right view.
Hope this helps

Resources