chang thenoob
chang thenoob

Reputation: 103

Firebase notification with user data

I'm using firebase for my android studio app, and I would like to send notifications to users (I use JAVA). I started with FCM, however, it seems that I can only send a text that I write with it. I would rather send a notification to a specific user, containing his user data. How can I do that?

For example, as soon as my user reaches a score of 10, I want to send to him a notification saying congratulation, so a notification that would use the data from the database.

Upvotes: 1

Views: 1718

Answers (1)

azabaluev
azabaluev

Reputation: 41

  1. On server side initialize your FCM
    import com.google.firebase.FirebaseApp;
    import com.google.firebase.FirebaseOptions;

    File configFile = new File("your-project-firebase-adminsdk-123magic-chars456.json");
    String json = FileUtils.readFileToString(configFile, StandardCharsets.UTF_8.name());
    InputStream in = new ByteArrayInputStream(json.getBytes());
    
    FirebaseOptions options = FirebaseOptions.builder()
            .setCredentials(GoogleCredentials.fromStream(in))
            .build();
    
    FirebaseApp.initializeApp(options);     
  1. To send PUSH notification to specific user you need to know his registration token.
    import com.google.firebase.messaging.FirebaseMessaging;
    import com.google.firebase.messaging.Message;
    
    //.......
    
    Message message = Message.builder()
        .putData("userId", ""+yourUser.getId())
        .putData("status", ""+yourUser.getStatus())
        .putData("score", ""+yourUser.getScore())
        .setToken(registrationToken)
        .build();
    String response = FirebaseMessaging.getInstance().send(message);
    
    Log.info(this, "SendPush to: " + yourUser + ", response: " + response);
  1. On android app side. Add this to build.gradle (Module)
    implementation platform('com.google.firebase:firebase-bom:29.0.0')
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-analytics'

And this to your AndroidManifest.xml

    <service
        android:name="my.app.service.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
  1. And now implement FirebaseMessagingService in your app. It does the following:
  • receive PUSH-messages from your server,
  • show notifications on device
  • get registrationToken from FCM
  • send token to server (where you can bind user with its token)
    package my.app.service;

    import android.app.NotificationChannel;
    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.os.Build;

    import com.google.firebase.FirebaseApp;
    import com.google.firebase.iid.FirebaseInstanceIdReceiver;
    import com.google.firebase.iid.internal.FirebaseInstanceIdInternal;
    import com.google.firebase.messaging.FirebaseMessaging;
    import com.google.firebase.messaging.FirebaseMessagingService;
    import com.google.firebase.messaging.RemoteMessage;

    import org.apache.commons.lang3.StringUtils;

    import java.util.Map;

    import androidx.core.app.NotificationCompat;
    import my.app.R;
    import my.app.MainActivity;

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

        @Override
        public void onCreate() {
            super.onCreate();

        }

        @Override
        public void onNewToken(String token) {
            
            // Send token to server here!
            
        }

        /**
         * Called when message is received.
         *
         * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
         */
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            String message = handleNow(remoteMessage.getData());

            // Check if message contains a notification payload.
            if (remoteMessage.getNotification() != null) {
                MyLog.info(this, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            }

            sendNotification(message);
        }

        



        /**
         * Handle time allotted to BroadcastReceivers.
         * @param data
         */
        private String handleNow(Map<String, String> data) {
            String userId = data.get("userId");
            String status = data.get("status");
            String score = data.get("score");
            
            // Here you can process message data as you wish

            String message = "Got data: userId=" + userId + ", status=" + status + ", score=" + score;
            return 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);

            String channelId = getString(R.string.default_notification_channel_id);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, channelId)
                            .setSmallIcon(R.drawable.ic_menu_camera)
                            .setContentTitle(getString(R.string.app_name))
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);

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

            // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        "Channel human readable title",
                        NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(channel);
            }

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

Upvotes: 1

Related Questions