# Implement push notifications

{% tabs %}
{% tab title="iOS" %}
To implement push notifications in our app we need to create a .p8 key file from [developer.apple.com](https://feveloper.apple.com/) and setup the settings for the notification in the dashboard.&#x20;

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:

```java
import UserNotifications

...

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions
        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        MBManager.shared().pushToken = “YOUR_TOKEN”

        …

        let userNotificationCenter = UNUserNotificationCenter.current()
        userNotificationCenter.delegate = self
        registerForPushNotifications()
}

...

// MARK: – Notifications

func registerForPushNotifications() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
        guard granted else { return }
        self.getNotificationSettings()
    }
}

func getNotificationSettings() {
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    MumblePush.registerDevice(deviceToken: deviceToken, success: {
        let topics = [“TOPIC_ID”]
        MumblePush.register(toTopics: topics)
    })
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print(“Failed to register: \(error)“)
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler
        completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler(UNNotificationPresentationOptions.alert)
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler
        completionHandler: @escaping () -> Void) {
        completionHandler()
    }
}
```

If you want more information about how push notifications work on iOS, visit the [Apple official guide](https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1).
{% endtab %}

{% tab title="Android" %}
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.&#x20;

This will generate a push API KEY.

To create a new Firebase project, please reference [this](https://firebase.google.com/docs/android/setup) documentation, then add these dependencies to your project at the latest version available:

```java
com.google.firebase:firebase–core
com.google.firebase:firebase–messaging
```

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:

```java
public class FCMReceiver extends FirebaseMessagingService {

@Override 
public void onNewToken(String token) { 
     MBurgerPushTasks.sendToken(getApplicationContext(), getDeviceID(), token); 
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
     Map<String, String> map = remoteMessage.getData();

     //The standard message is inside the “body” field
     String msg = map.get(“body”);
     if(map.containsKey(“custom”)) {
         String customJsonData = map.get(“custom”);
         if(customJsonData != null){
               createNofitication(custom);
         }
     }
  }
}
```

{% hint style="info" %}
Remember to also add them also on your manifest.xml file:
{% endhint %}

```markup
<service android:name=“.FCMReceiver”>
   <intent-filter>
       <action android:name=“com.google.firebase.MESSAGING_EVENT” />
   </intent-filter>
</service>
```

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.&#x20;

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”.

```java
JSONArray topics = new JSONArray();
topics.put(“flash_deals”); //or “flash_news”
MBurgerPushTasks.registerTopics(context, getDeviceID(), topics);
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mburger.cloud/sample-app-1/implement-push-notifications.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
