2

I'm referring to the interpreting messages portion in https://firebase.google.com/docs/cloud-messaging/ios/receive .

Where in my code can I change the text for notifications in Firebase?

1
  • Please first have a look at stackoverflow.com/help/how-to-ask and then provide more details and code that show what you have already tried. Commented May 15, 2018 at 17:00

2 Answers 2

0

In order to send a push notification to a device, You need to have a script (or a piece of code) ideally hosted on a server that would send a push notification on your behalf.
There you can customize the message and even play an audio upon receiving a notification.
Here is a code snippet in java that can be used to send a push notification to a device (or a group of devices).

private Map sendPush(String to, String from, String title, String message,
            String sound) throws IOException {
    sound = (sound != null) ? sound : "default";  // set default audio file name
    // Creating the URL and connection
    URL url = new URL(FCM_URL); // your firebase URL
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Authorization", "key=" + FCM_KEY); // the firebase project key 

    conn.setDoOutput(true);

    // set the notification body
    Map<String, String> notificationBody = new HashMap();
    notificationBody.put("title", title); // notification title
    notificationBody.put("body", message); // notification message
    notificationBody.put("sound", sound);
    notificationBody.put("badge", "1");

    Map<String, String> dataBody = new HashMap();
    dataBody.put("sender", from); // sender id

    Map<String, Object> pushBody = new HashMap();
    pushBody.put("notification", notificationBody);
    pushBody.put("data", dataBody);
    pushBody.put("to", to); // receiver(s) id
    pushBody.put("priority", "high");

    // convert your dictionary to json string using Google Gson library (similar to JsonSerialization class in swift)
    String input = new Gson().toJson(pushBody);

    // write input bytes in request body
    try (OutputStream os = conn.getOutputStream()) {
        os.write(input.getBytes());
        os.flush();
    }

    StringBuilder responseString;
    Reader reader = new InputStreamReader(conn.getInputStream()); // send request and receive response 

    // parse response
    try (BufferedReader in = new BufferedReader(reader)) {
        String inputLine;
        responseString = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            responseString.append(inputLine);
        }
    }

    // using Google Gson to convert json string into Map (similar to JsonSerialization class in swift)
    Map<String, Object> responseObject = new Gson().fromJson(responseString.toString(),
                Map.class);

    return responseObject;
}

Since this is a java code, so i have hosted it in a java application deployed on Apache Tomcat Server.

You can find several similar implementations in various languages like php or node.js etc.

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I'm looking for! D you have any idea what I should do to put this into Swift?
AFAIK you can't send a push directly from your app. You can either send an http request to a server API that would send yhe request on your behalf. or you may explore the Google Cloud Functions. firebase.google.com/docs/functions
0

first create p.12 certificate and upload in firebase ->project settings ->cloud messaging tab ->select your iOS app ->add APNS certificate.

A. Create a (.certSigningRequest) CSR file

Open Keychain Access from Utilities From Keychain Access toolbar select Keychain Access -> Preference In the pop up window select Certificates tab Set both “Online Certificate Status Protocol” and “Certificate Revocation List” to “Off" Close this window Now from toolbar, open Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority Enter email address and common name that you used to register in the iOS Developer Program Keep CA Email blank and select “Saved to disk” and “Let me specify key pair information” Click Continue Choose a filename & destination on your hard drive Click Save In the next window, set “Key Size” value to “2048 bits” Set “Algorithm” to “RSA” Click Continue This will create and save your certSigningRequest file (CSR) to your hard drive. A public and private key will also be created in Keychain Access with the Common Name entered.

B. Create ".cer" file in iOS developer account

Login to apple developer account Click “Certificates, Identifiers & Profiles” Click “Provisioning Profiles” In the “Certificates” section click “Production” Click the “Add” (+) button at the top-right of the main panel Now, choose “App Store and Ad Hoc” Click Continue Click “Choose File” & find CSR file you’ve made from your hard drive Click Generate Click Download to get the file C. Install .cer and generate .p12 certificate

Find .cer file you’ve downloaded and double-click Set Login drop-down to “login" and Click Add Open up KeyChain Access and you'll find profile created in Step A You can expand “private key” profile (shows certificate you added) Select only these two items (not the public key) Right click and click “Export 2 items…” from popup Now make sure file format is “.p12” and choose filename and destination on your hard drive Click Save. Now, you’ll be prompted to set a password but keep these both blank Click OK. Now, you have a .p12 file on your hard drive

and open your Xcode project and select target->capabilities->pusnotification->on

next do this stuff https://firebase.google.com/docs/cloud-messaging/ios/receive

next push message from firebase cloud messaging console with message and title and select your app target->user segment->your app.

then your app will able

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.