Examples

Full Example

You are in the following repository, you should just clone the repository, have the library at the same level of the project and open the file FullDemonstrationMessagingSDK.xcworkspace, I leave some screenshots of the application.

image3.jpg

Example - Getting MessagingDevice

...
import MessagingSDK
class ExampleViewController: UIViewController { ...
private var device: MessagingDevice?
override func viewDidLoad() { ...
_ = Messaging.shared.observeDeviceFetchResponse(self, do: #selector(workWithDevi ...
}
func onAnyEventToGetDevice(){ Messaging.shared.requestDevice()
}
@objc func workWithDevice( _ notification: Notification){
if let device = notification.object as? MessagingDevice {
           // MessagingDevice availability
self.device = device }
}
override func viewDidDisappear(_ animated: Bool) { ...
_ = Messaging.shared.removeObserveDeviceFetchResponse(self) ...
} }
...
import MessagingSDK
class ExampleViewController: UIViewController { ...
private var user: MessagingUser?
override func viewDidLoad() { ...
_ = Messaging.shared.observeUserFetchResponse(self, do: #selector(workWithUser(_ ...
}
func onAnyEventToGetUser(){ // Need MessagingDevice device?.requestUser()
}
@objc func workWithUser( _ notification: Notification){ if let user = notification.object as? MessagingUser {
           // MessagingUser availability
self.user = user }
}
override func viewDidDisappear(_ animated: Bool) { ...
_ = Messangi.shared.removeObserveUserFetchResponse(self) ...
} }

Example - Getting a MessagingNotification (willPresent)

Process all the Notifications that arrive at the device, for both background and foreground. This is obtained regardless of whether the user interacts with the device or not.

...
import MessagingSDK @UIApplicationMain
class AppDelegate: MessagingAppDelegate or ... MessagingUserNotificationDelegate {
override func messagingReceivedNotification(_ notification: MessagingNotification) - ...
// your code
// notification.actionIdentifier // has your custom action identifier
} }
	

Another way to process Notifications is through an observer:

...
import MessagingSDK
class ExampleViewController: UIViewController { ...
private var ReceivedNotification: MessagingNotification?
override func viewDidLoad() { ...
_ = Messaging.shared.observeReceivedNotification(self, do: #selector(workWithUse ...
}
   // Unlike the previous examples, this case does not require a request for notificati
@objc func workWithNotification( _ notification: Notification){
if let messagingNotification = notification.object as? MessagingNotification {
           // MessagingNotification availability
self.ReceivedNotification = messagingNotification
...
_ = Messangi.shared.removeObserveReceivedNotification(self) ...
} }

Example - To control the interaction of the notification (didReceive)

This method will only run when the user clicks on the received Notification on their device.

...
import MessagingSDK @UIApplicationMain
class AppDelegate: MessagingAppDelegate or ... MessagingUserNotificationDelegate {
override func messagingInteractionNotification(_ notification: MessagingNotification ...
// your code
// notification.actionIdentifier // has your custom action identifier
} }
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.