Implement push notifications
iOS
Android
To implement push notifications in our app we need to create a .p8 key file from developer.apple.com and setup the settings for the notification in the dashboard.
We also need to create a provisioning profile for the app because push notifications don't work with a wildcard provisioning profile.
In the MBurger dashboard under Settings > Info & Features you can see two fields that you will need to setup your app to receive push notifications from MBurger:
    Push token
    Project topic for push
The first one identifies your MBurger project and will be used to setup the service in your didFinishLaunchingWithOptions, the second one will be used to register the token of your device once you obtain one from APNS.
Add this to your AppDelegate:
1
import UserNotifications
2
3
...
4
5
func application(_ application: UIApplication,
6
didFinishLaunchingWithOptions
7
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
8
MBManager.shared().pushToken = “YOUR_TOKEN”
9
10
11
12
let userNotificationCenter = UNUserNotificationCenter.current()
13
userNotificationCenter.delegate = self
14
registerForPushNotifications()
15
}
16
17
...
18
19
// MARK: – Notifications
20
21
func registerForPushNotifications() {
22
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
23
guard granted else { return }
24
self.getNotificationSettings()
25
}
26
}
27
28
func getNotificationSettings() {
29
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
30
guard settings.authorizationStatus == .authorized else { return }
31
DispatchQueue.main.async {
32
UIApplication.shared.registerForRemoteNotifications()
33
}
34
}
35
}
36
37
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
38
MumblePush.registerDevice(deviceToken: deviceToken, success: {
39
let topics = [“TOPIC_ID”]
40
MumblePush.register(toTopics: topics)
41
})
42
}
43
44
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
45
print(Failed to register: \(error))
46
}
47
48
extension AppDelegate: UNUserNotificationCenterDelegate {
49
func userNotificationCenter(_ center: UNUserNotificationCenter,
50
willPresent notification: UNNotification,
51
withCompletionHandler
52
completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
53
completionHandler(UNNotificationPresentationOptions.alert)
54
}
55
56
func userNotificationCenter(_ center: UNUserNotificationCenter,
57
didReceive response: UNNotificationResponse,
58
withCompletionHandler
59
completionHandler: @escaping () -> Void) {
60
completionHandler()
61
}
62
}
Copied!
If you want more information about how push notifications work on iOS, visit the Apple official guide.
You must create a Firebase project for your application, so that you’ll have the push server key to insert in your MBurger Push project.
This will generate a push API KEY.
To create a new Firebase project, please reference this documentation, then add these dependencies to your project at the latest version available:
1
com.google.firebase:firebase–core
2
com.google.firebase:firebase–messaging
Copied!
Then, create your own class extending FirebaseMessagingService (called FCMReceiver) which will be used to retrieve Firebase token and send it to MBurger Push and to receive and create notifications for your app:
1
public class FCMReceiver extends FirebaseMessagingService {
2
3
@Override
4
public void onNewToken(String token) {
5
MBurgerPushTasks.sendToken(getApplicationContext(), getDeviceID(), token);
6
}
7
8
@Override
9
public void onMessageReceived(RemoteMessage remoteMessage) {
10
Map<String, String> map = remoteMessage.getData();
11
12
//The standard message is inside the “body” field
13
String msg = map.get(“body”);
14
if(map.containsKey(“custom”)) {
15
String customJsonData = map.get(“custom”);
16
if(customJsonData != null){
17
createNofitication(custom);
18
}
19
}
20
}
21
}
Copied!
Remember to also add them also on your manifest.xml file:
1
<service android:name=“.FCMReceiver”>
2
<intent-filter>
3
<action android:name=“com.google.firebase.MESSAGING_EVENT” />
4
</intent-filter>
5
</service>
Copied!
Then as a last thing to do, register your users to a topic, which will be needed to target specific groups of users while sending push notifications.
For this example we would want to send push notifications about flash sales of our store (or important news about a celebrity), so we use the “flash_deals” topic or “flash_news”.
1
JSONArray topics = new JSONArray();
2
topics.put(“flash_deals”); //or “flash_news”
3
MBurgerPushTasks.registerTopics(context, getDeviceID(), topics);
Copied!
Last modified 1yr ago
Copy link