4

I am developing messaging app for iOS.

I keep users' phone numbers to the server but I don't keep contact names on the backend side.

When the user receives push notification from the server, server sends phone number and message content to the user in notification payload. I implemented application:didReceiveRemoteNotification:fetchCompletionHandler method to show local notification. I look up in local database for phoneContactName:phoneNumber pair and show local notification in the format: %phoneContactName%:%messageContent%

However, the hurdle is that I don't now how to execute this code when the app is not running. I tried to implement application:didFinishLaunchingWithOptions and check there for

NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

but this code is not invoked.

So to solve the problem I would like to ask you what architectural model should I use. Should I keep contact names on the server side to send notification.alert from the server side with the name of contact (so far my server doesn't know the name of contact phone number) or is there any way to execute code when the app is not running?

P.S. Also, I calculate the badge number on the client side when notification is received. So, the code execution solution will be better if possible.

Thank you

Andrew Marin
  • 1,053
  • 1
  • 13
  • 15

2 Answers2

0

I think your architecture is correct. Your issue is down to one of the App states your app is currently in.

If your app is terminated or has never been opened then the above code in application:didFinishLaunchingWithOptions will be exectued, however, if your app is in a background or suspended state then the below method will be called.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo` 

Therefore you also need to implemented this in the app delegate.

I hope this helps

Elliott Minns
  • 2,774
  • 1
  • 17
  • 17
  • My issue is that application:didFinishLaunchingWithOptions is not called in my case. Am I doing something wrong? – Andrew Marin Mar 10 '15 at 12:54
  • `application:didFinishLaunchingWithOptions` is only called when the app has been terminated. This is usually the first time the app is being run when the device is turned on or when the iOS Operating System has killed the process. I assume that the application is being opened from a suspended state which means that `application:didFinishLaunchingWithOptions` will not be called. If the user opens your app from a notification whilst the app is in this state `application:didReceiveRemoteNotification` is the method that is called. – Elliott Minns Mar 10 '15 at 12:56
  • 2
    thank you. As I understood from relevant answers application:didFinishLaunchingWithOptions is called when the users presses the badge. My server doesn't know the phoneContactName and sends only phoneNumber and messageContent in the payload. The application:didReceiveRemoteNotification method is called when the app is in foreground or background but neither this method nor application:didFinishLaunchingWithOptions is called when the app was suspended. How should I replace the phoneNumber with phoneContactName if the app was suspended? – Andrew Marin Mar 11 '15 at 18:28
-1

you have to manage condition for app active or inactive like

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {

    // Below Code Shows Message While Your Application is in Active State



    else { 
//for app recieving notification in background and tap on notification
}