Add Push Notification to your app
The first thing you have to do is implement Push Notifications using the UserNotifications framework.
You will be guided through all the steps needed to have a functional project with the push notifications, if you have already done it you can skip to the Integrate MPush section of this README.

Create a key

Go to developer.apple.com with an admin account and under Keys -> All click the plus button in the top right corner.
Specify a name for your key and enable Apple Push Notifications service (APNs).
Key creation 2
Key creation 1
Then download the .p8 key file created and upload it in our dashboard.
Note that the key created is valid for all the apps of your profile and can't be re-downloaded, keep it in a safe place because you will likely have to resue it.

Add notifications to your app

Now go to your app settings under Identifier -> AppId and enable the notifications services following the steps. After that, you need to update the Provisioning Profile for your app or create a new one because push notifications don't work for applications signed with a wildcard Provisioning Profile.
Now it's finally time to move to XCode. Open your project and enable push notifications in the capabilities tab.
XCode-capabilities
In AppDelegate.swift add
1
import UserNotifications
2
​
3
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
4
...
5
​
6
let userNotificationCenter = UNUserNotificationCenter.current()
7
userNotificationCenter.delegate = self
8
​
9
self.registerForPushNotifications()
10
...
11
}
12
​
13
// MARK: - Notifications
14
​
15
func registerForPushNotifications() {
16
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
17
guard granted else { return }
18
self.getNotificationSettings()
19
}
20
}
21
​
22
func getNotificationSettings() {
23
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
24
guard settings.authorizationStatus == .authorized else { return }
25
DispatchQueue.main.async {
26
UIApplication.shared.registerForRemoteNotifications()
27
}
28
}
29
}
30
​
31
extension AppDelegate: UNUserNotificationCenterDelegate {
32
func userNotificationCenter(_ center: UNUserNotificationCenter,
33
willPresent notification: UNNotification,
34
withCompletionHandler
35
completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
36
completionHandler(UNNotificationPresentationOptions.alert)
37
}
38
​
39
func userNotificationCenter(_ center: UNUserNotificationCenter,
40
didReceive response: UNNotificationResponse,
41
withCompletionHandler
42
completionHandler: @escaping () -> Void) {
43
completionHandler()
44
}
45
}
Copied!
Last modified 1yr ago