0

This is a general question, but I can't seem to find a straight answer, and I'm not sure what to look for. I have a backend that sends notfications when someone likes something you posted, I am able to send it and the app receives it. However, I'm not sure how to create a UI to handle the notification. My first guess was that, since receiving notifications triggers didReceiveRemoteNotification fetchCompletionHandler in the AppDelegate, I should try to get the current VC in that function and add graphical elements from there. However, I read that this is not something you should do, and that you should use NSNotifications instead. The problem is I'm not sure I understand how I can articulate remote notificattions with the NSNotificationsCenter.

What do you recommend?

Charles Panel
  • 255
  • 2
  • 10
  • Possible duplicate : http://stackoverflow.com/questions/31610896/controlling-which-view-controller-loads-after-receiving-a-push-notification-in-s/31612906#31612906 – Swift Rabbit Jun 07 '16 at 14:42
  • Actually, my issue is less complicated: I am able to receive the info in the app. My question is, how should I display it on the presented View controller? Should I get the presented view controller in app delegate and just add a UIView? – Charles Panel Jun 07 '16 at 17:50
  • Not sure how it is related to remote notification then. – Swift Rabbit Jun 07 '16 at 18:46
  • Because I receive the remote notification from the server in app delegate, but I can't figure out how I can show it without using a UIAlert. – Charles Panel Jun 07 '16 at 20:41

1 Answers1

0

NSNotification is part of Observer design pattern.

It like a "broadcast".

First you have to register every component which should be listening the event :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFonction) name:@"Notif name" object:nil];
    //swift
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(myFonction), name: "notif name", object: nil)

Next, when you have a message to broadcast, post a notification with the same name :

[[NSNotificationCenter defaultCenter] postNotificationName:@"Notif name" object:nil];
//swift
NSNotificationCenter.defaultCenter().postNotificationName("notif name", object: nil)

When you will post the notification, the method myFonction will be called on the target

CZ54
  • 5,488
  • 1
  • 24
  • 39
  • Actually, my issue is less complicated: I am able to receive the info in the app. My question is, how should I display it on the presented View controller? Should I get the presented view controller in app delegate and just add a UIView? I'm trying to get a UIView on top of the screen like in most regular apps instead of a UIAlert... – Charles Panel Jun 07 '16 at 18:00
  • My answer is the way to do that. In the `myFonction` you can do whatever you want : displaying an alert, adding a custom UIView... – CZ54 Jun 08 '16 at 07:03
  • You are right, the UIView I was trying to display was not appearing, so I thought there might be more steps required, but it was a problem of scope, so it does work now. Thanks for your detailed answer! – Charles Panel Jun 08 '16 at 14:10