简体   繁体   中英

How do I get the data or message from my service?

My MyFirebaseMessagingService.

public class MyFirebaseMessagingService 
 extends FirebaseMessagingService {

 private static final String TAG = "MyFirebaseMsgService";

    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "FRCM:"+ remoteMessage.getFrom());

         /*Check if the message contains data*/
        if(remoteMessage.getData().size() > 0){
            Log.d(TAG,"Message data: "+ remoteMessage.getData());

        }
        /*Check if the message contains notification*/
        if(remoteMessage.getNotification() != null){
            Log.d(TAG,"Message body: "+ remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());


        }
    }
    /*Display Notification Body*/
    private void sendNotification(String body) {

        Intent intent = new Intent(this, Home.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
        /*Set sound of Notification*/

        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_event_note_black_24dp)
                .setContentTitle("Firebase Cloud Messaging")
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent);


        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0/*ID of notification*/, notifiBuilder.build());
         intent = new Intent("myAction");
        intent.putExtra("title", title);
        intent.putExtra("message", message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}

and My Activity Messaging is.

public class Mess{
}

You send messages from your phone using FCM. You need to make a POST to https://fcm.googleapis.com/fcm/send api with payload that you want to send, and you server key found in Firebase Console project or You can Use POSTMAN google chrome extension

As an exameple of payload sending to a single user with to param:

{ "data": {
      "score": "5x1",
      "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

Also, to param can be used for topics "to": "topics/yourTopic"

In data you can send whatever you want, message is received in onMessageReceived() service from Firebase.

More details you can found in Firebase documentation .

For sending data to another activity

 private void sendNotification(String body) {

    Intent intent = new Intent(this, Home.class);

here you can set intent

 intent.putExtra("word", body);

and to read from activity use

 b = getIntent().getExtras();
 String passed_from = b.getString("word");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM