Send notification message to application

3

I'm developing an APP and need to send warnings to users using this APP. Example, in APPs of shopping sites, when a promotion appears a message arrives the Application about the promotion. The user reads and the message goes out.

How can I do this?

This APP is for a school, and my idea is that for example, the school's management can write a message through a web page and that this message can reach the parents who use the APP on their cell phone telling them about meetings , bulletin deliveries, etc.

Any tips?

    
asked by anonymous 17.06.2016 / 21:44

2 answers

8

Hello from google IO 2016 the message service was transferred to Firebase which made the initial deployment simpler, as it does not require the need for a server to send the messages.

Add to build.grad:

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

You can send messages through the firebase console that would be an initial solution. Then move on to another solution in php, java, or another language that 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 application will already receive messages via console. There is a lot to be done following the Firebase page that has a lot of documentation and where I got this information:

Firebase Cloud Message

I hope I have helped.

    
18.06.2016 / 04:54
1

This service is called Push Notification (Cloud Messaging) , there is no way I can explain it with codes as it is a very extensive service.

In the documentation for Android, there is a very efficient explanation: link

Here you'll find examples of how to work with the feature: link

In 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 questions put here that help you.

Hugs.

    
17.06.2016 / 21:51