Send notification message to app

Asked

Viewed 1,659 times

3

I am developing an APP and I have the need to send warnings to users who use this APP. Example, in Apps of shopping websites , when a promotion appears a message arrives the App about the promotion. The user read and the message erases.

How can I do this?

This APP is for a school, and my idea is that for example, the school management can through a web page write a message and that this message can reach parents who use the APP on their mobile warning about meetings, newsletter deliveries, etc.

Any hint?

2 answers

8

Hello from google IO 2016 the message service has been transferred to Firebase which made it simpler to initial deployment, as it dispenses with the need for a server to send messages.

Add to build.Radle:

dependencies {
   compile 'com.google.firebase:firebase-messaging:9.0.2'
}

You can send messages via the firebase console that would be an initial solution. Then move on to another solution in php, java or other language (which dominates).

Add to your android manifest:

To handle incoming messages use the following class:

package com.google.firebase.quickstart.fcm;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

With this your app will already receive messages via console. There is much to be done follow the Firebase page that has plenty of documentation and where I got this information:

Firebase Cloud Message

I hope I’ve helped.

1

This service is called Push Notification (Cloud Messaging), there’s no way I can explain it to you with codes because it’s a very long service.

In the Android documentation, there is a very efficient explanation: https://developers.google.com/cloud-messaging/

There, you can find examples of how to work with functionality: https://developers.google.com/cloud-messaging/android/start

This way, the direction of the school through an online page, post the title and description of the ad, and will reach everyone who has the app installed.

Any doubt put here that we help you.

Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.